diff --git a/.changeset/two-dogs-push.md b/.changeset/two-dogs-push.md index 0fa086e1a92..c22023316ca 100644 --- a/.changeset/two-dogs-push.md +++ b/.changeset/two-dogs-push.md @@ -2,11 +2,11 @@ '@keystonejs/server-side-graphql-client': major --- -This is the initial release of `@keystonejs/server-side-graphql-client,` a library for running server-side graphQL queries and mutations in Keystone. +This is the initial release of `@keystonejs/server-side-graphql-client,` a library for running server-side graphQL queries and mutations in Keystone. It is intended to replace the `keystone.createItems` method with a set of utility functions to generate and execute graphQL queries. -Note: In a future change we will remove the `keystone.createItems` method. You will need to update code that used `createItems`. +Note: In a future change we will remove the `keystone.createItems` method. You will need to update code that used `createItems`. If you have examples like: @@ -23,7 +23,7 @@ const { createItems } = require('@keystonejs/server-side-graphql-client'); createItems({ keystone, - listName: 'User', + listKey: 'User', items: [{ data: { name: 'Ticiana' } }, {data: { name: 'Lauren' } }] }) ``` diff --git a/packages/server-side-graphql-client/README.md b/packages/server-side-graphql-client/README.md index fae69e7700e..f404848e02d 100644 --- a/packages/server-side-graphql-client/README.md +++ b/packages/server-side-graphql-client/README.md @@ -29,7 +29,7 @@ const { createItem } = require('@keystonejs/server-side-graphql-client'); const user = await createItem({ keystone, - listName: 'User', + listKey: 'User', item: { name: 'Alice' }, returnFields: `id name`, }); @@ -47,7 +47,7 @@ These utilities can be used for a wide range of specific use-cases, some more co ```js const seedUsers = async usersData => { - await createItems({ keystone, listName: 'User', items: usersData }); + await createItems({ keystone, listKey: 'User', items: usersData }); }; ``` @@ -67,7 +67,7 @@ keystone.createList('Page', { const pageToCopy = resolvedData.copy ? await getItem({ keystone, - listName: 'Page', + listKey: 'Page', itemId: resolvedData.copy, returnFields: 'name, content', }) @@ -101,7 +101,7 @@ The following config options are common to all server-side graphQL functions. | Properties | Type | Default | Description | | -------------- | ---------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `keystone` | `Keystone` | (required) | Keystone instance. | -| `listName` | `String` | (required) | Keystone list name. | +| `listKey` | `String` | (required) | Keystone list name. | | `returnFields` | `String` | `id` | A graphQL fragment of fields to return. Must match the graphQL return type. | | `context` | `Object` | N/A | An Apollo [`context` object](https://www.apollographql.com/docs/apollo-server/data/resolvers/#the-context-argument). See the [server side graphQL docs](/docs/discussions/server-side-graphql.md) for more details. | @@ -124,7 +124,7 @@ keystone.createList('User', { const addUser = async userInput => { const user = await createItem({ keystone, - listName: 'User', + listKey: 'User', item: userInput, returnFields: `name, email`, }); @@ -138,9 +138,9 @@ addUser({ name: 'keystone user', email: 'keystone@test.com' }); [Shared Config Options](#shared-config-options) apply to this function. -| Properties | Type | Default | Description | -| ---------- | ------------------------------- | ---------- | ----------------------- | -| `item` | GraphQL `[listName]CreateInput` | (required) | The item to be created. | +| Properties | Type | Default | Description | +| ---------- | ------------------------------ | ---------- | ----------------------- | +| `item` | GraphQL `[listKey]CreateInput` | (required) | The item to be created. | ### `createItems` @@ -168,7 +168,7 @@ const dummyUsers = [ const addUsers = async () => { const users = await createItems({ keystone, - listName: 'User', + listKey: 'User', items: dummyUsers, returnFields: `name`, }); @@ -181,10 +181,10 @@ addUsers(); [Shared Config Options](#shared-config-options) apply to this function. -| Properties | Type | Default | Description | -| ---------- | -------------------------------- | ---------- | ---------------------------------------------------------------------------------------------- | -| `items` | GraphQL `[listName]sCreateInput` | (required) | The array of objects to be created. | -| `pageSize` | `Number` | 500 | The create mutation batch size. This is useful when you have large set of data to be inserted. | +| Properties | Type | Default | Description | +| ---------- | ------------------------------- | ---------- | ---------------------------------------------------------------------------------------------- | +| `items` | GraphQL `[listKey]sCreateInput` | (required) | The array of objects to be created. | +| `pageSize` | `Number` | 500 | The create mutation batch size. This is useful when you have large set of data to be inserted. | ### `getItem` @@ -205,7 +205,7 @@ keystone.createList('User', { const getUser = async ({ itemId }) => { const user = await getItem({ keystone, - listName: 'User', + listKey: 'User', itemId, returnFields: 'id, name', }); @@ -239,10 +239,10 @@ keystone.createList('User', { }); const getUsers = async () => { - const allUsers = await getItems({ keystone, listName: 'User', returnFields: 'name' }); + const allUsers = await getItems({ keystone, listKey: 'User', returnFields: 'name' }); const someUsers = await getItems({ keystone, - listName: 'User', + listKey: 'User', returnFields: 'name', where: { name: 'user1' }, }); @@ -256,10 +256,10 @@ getUsers(); [Shared Config Options](#shared-config-options) apply to this function. -| Properties | Type | Default | Description | -| ---------- | ------------------------------ | ------- | ---------------------------------------------------------------------------------------------------------- | -| `where` | GraphQL `[listName]WhereInput` | `{}` | Limit results to items matching [where clause](https://www.keystonejs.com/guides/intro-to-graphql/#where). | -| `pageSize` | `Number` | 500 | The query batch size. Useful when retrieving a large set of data. | +| Properties | Type | Default | Description | +| ---------- | ----------------------------- | ------- | ---------------------------------------------------------------------------------------------------------- | +| `where` | GraphQL `[listKey]WhereInput` | `{}` | Limit results to items matching [where clause](https://www.keystonejs.com/guides/intro-to-graphql/#where). | +| `pageSize` | `Number` | 500 | The query batch size. Useful when retrieving a large set of data. | ### `updateItem` @@ -280,7 +280,7 @@ keystone.createList('User', { const updateUser = async updateUser => { const updatedUser = await updateItem({ keystone, - listName: 'User', + listKey: 'User', item: updateUser, returnFields: 'name', }); @@ -293,9 +293,9 @@ updateUser({ id: '123', data: { name: 'newName' } }); [Shared Config Options](#shared-config-options) apply to this function. -| Properties | Type | Default | Description | -| ---------- | ------------------------------- | ---------- | ----------------------- | -| `item` | GraphQL `[listName]UpdateInput` | (required) | The item to be updated. | +| Properties | Type | Default | Description | +| ---------- | ------------------------------ | ---------- | ----------------------- | +| `item` | GraphQL `[listKey]UpdateInput` | (required) | The item to be updated. | ### `updateItems` @@ -316,7 +316,7 @@ keystone.createList('User', { const updateUsers = async (updateUsers) => { const users = await updateItems({ keystone, - listName: 'User', + listKey: 'User', items: updateUsers, returnFields: 'name' }); @@ -334,9 +334,9 @@ updateUsers([ [Shared Config Options](#shared-config-options) apply to this function. -| Properties | Type | Default | Description | -| ---------- | -------------------------------- | ---------- | ----------------------------- | -| `items` | GraphQL `[listName]sUpdateInput` | (required) | Array of items to be updated. | +| Properties | Type | Default | Description | +| ---------- | ------------------------------- | ---------- | ----------------------------- | +| `items` | GraphQL `[listKey]sUpdateInput` | (required) | Array of items to be updated. | ### `deleteItem` @@ -355,7 +355,7 @@ keystone.createList('User', { }); const deleteUser = async itemId => { - const user = await deleteItem({ keystone, listName: 'User', itemId }); + const user = await deleteItem({ keystone, listKey: 'User', itemId }); console.log(user); // { id: '123' } }; deleteUser('123'); @@ -386,7 +386,7 @@ keystone.createList('User', { }); const deletedUsers = async items => { - const users = await deleteItems({ keystone, listName: 'User', items }); + const users = await deleteItems({ keystone, listKey: 'User', items }); console.log(users); // [{id: '123'}, {id: '456'}] }; deletedUsers(['123', '456']); diff --git a/packages/server-side-graphql-client/lib/server-side-graphql-client.js b/packages/server-side-graphql-client/lib/server-side-graphql-client.js index 32b130a78f1..f10cc5c4865 100644 --- a/packages/server-side-graphql-client/lib/server-side-graphql-client.js +++ b/packages/server-side-graphql-client/lib/server-side-graphql-client.js @@ -38,8 +38,8 @@ const _runChunkedMutation = async ({ keystone, query, gqlName, pageSize, items, return [].concat(...result.map(item => item[gqlName])); }; -const createItem = async ({ keystone, listName, item, returnFields = `id`, context }) => { - const { createMutationName, createInputName } = keystone.lists[listName].gqlNames; +const createItem = async ({ keystone, listKey, item, returnFields = `id`, context }) => { + const { createMutationName, createInputName } = keystone.lists[listKey].gqlNames; const query = `mutation ($item: ${createInputName}){ ${createMutationName}(data: $item) { ${returnFields} } @@ -51,13 +51,13 @@ const createItem = async ({ keystone, listName, item, returnFields = `id`, conte const createItems = async ({ keystone, - listName, + listKey, items, pageSize = 500, returnFields = `id`, context, }) => { - const { createManyMutationName, createManyInputName } = keystone.lists[listName].gqlNames; + const { createManyMutationName, createManyInputName } = keystone.lists[listKey].gqlNames; const query = `mutation ($items: [${createManyInputName}]){ ${createManyMutationName}(data: $items) { ${returnFields} } @@ -73,8 +73,8 @@ const createItems = async ({ }); }; -const getItem = async ({ keystone, listName, itemId, returnFields = `id`, context }) => { - const { itemQueryName } = keystone.lists[listName].gqlNames; +const getItem = async ({ keystone, listKey, itemId, returnFields = `id`, context }) => { + const { itemQueryName } = keystone.lists[listKey].gqlNames; const query = `query ($id: ID!) { ${itemQueryName}(where: { id: $id }) { ${returnFields} } }`; const result = await runQuery({ keystone, query, variables: { id: itemId }, context }); @@ -83,13 +83,13 @@ const getItem = async ({ keystone, listName, itemId, returnFields = `id`, contex const getItems = async ({ keystone, - listName, + listKey, where = {}, pageSize = 500, returnFields = `id`, context, }) => { - const { listQueryName, whereInputName } = keystone.lists[listName].gqlNames; + const { listQueryName, whereInputName } = keystone.lists[listKey].gqlNames; const query = `query ($first: Int!, $skip: Int!, $where: ${whereInputName}) { ${listQueryName}(first: $first, skip: $skip, where: $where) { ${returnFields} } }`; let skip = 0; @@ -114,8 +114,8 @@ const getItems = async ({ return allItems; }; -const updateItem = async ({ keystone, listName, item, returnFields = `id`, context }) => { - const { updateMutationName, updateInputName } = keystone.lists[listName].gqlNames; +const updateItem = async ({ keystone, listKey, item, returnFields = `id`, context }) => { + const { updateMutationName, updateInputName } = keystone.lists[listKey].gqlNames; const query = `mutation ($id: ID!, $data: ${updateInputName}){ ${updateMutationName}(id: $id, data: $data) { ${returnFields} } @@ -133,13 +133,13 @@ const updateItem = async ({ keystone, listName, item, returnFields = `id`, conte const updateItems = async ({ keystone, - listName, + listKey, items, pageSize = 500, returnFields = `id`, context, }) => { - const { updateManyMutationName, updateManyInputName } = keystone.lists[listName].gqlNames; + const { updateManyMutationName, updateManyInputName } = keystone.lists[listKey].gqlNames; const query = `mutation ($items: [${updateManyInputName}]){ ${updateManyMutationName}(data: $items) { ${returnFields} } @@ -154,8 +154,8 @@ const updateItems = async ({ }); }; -const deleteItem = async ({ keystone, listName, itemId, returnFields = `id`, context }) => { - const { deleteMutationName } = keystone.lists[listName].gqlNames; +const deleteItem = async ({ keystone, listKey, itemId, returnFields = `id`, context }) => { + const { deleteMutationName } = keystone.lists[listKey].gqlNames; const query = `mutation ($id: ID!){ ${deleteMutationName}(id: $id) { ${returnFields} } @@ -167,13 +167,13 @@ const deleteItem = async ({ keystone, listName, itemId, returnFields = `id`, con const deleteItems = async ({ keystone, - listName, + listKey, items, pageSize = 500, returnFields = `id`, context, }) => { - const { deleteManyMutationName } = keystone.lists[listName].gqlNames; + const { deleteManyMutationName } = keystone.lists[listKey].gqlNames; const query = `mutation ($items: [ID!]){ ${deleteManyMutationName}(ids: $items) { diff --git a/packages/server-side-graphql-client/tests/index.test.js b/packages/server-side-graphql-client/tests/index.test.js index ce37b2ac180..49a73878dae 100644 --- a/packages/server-side-graphql-client/tests/index.test.js +++ b/packages/server-side-graphql-client/tests/index.test.js @@ -18,7 +18,7 @@ const schemaName = 'testing'; const seedDb = ({ keystone }) => createItems({ keystone, - listName: 'Test', + listKey: 'Test', items: testData, context: keystone.createContext({ schemaName }), }); @@ -46,7 +46,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Seed the db const item = await createItem({ keystone, - listName: 'Test', + listKey: 'Test', item: testData[0].data, context: keystone.createContext({ schemaName }), }); @@ -55,7 +55,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Get single item from db const singleItem = await getItem({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name, age', itemId: item.id, context: keystone.createContext({ schemaName }), @@ -70,14 +70,14 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Seed the db await createItems({ keystone, - listName: 'Test', + listKey: 'Test', items: testData, context: keystone.createContext({ schemaName }), }); // Get all the items back from db const allItems = await getItems({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name, age', context: keystone.createContext({ schemaName }), }); @@ -95,7 +95,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Update a single item const item = await updateItem({ keystone, - listName: 'Test', + listKey: 'Test', item: { id: seedItems[0].id, data: { name: 'updateTest' } }, returnFields: 'name, age', context: keystone.createContext({ schemaName }), @@ -112,7 +112,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Update multiple items const items = await updateItems({ keystone, - listName: 'Test', + listKey: 'Test', items: seedItems.map((item, i) => ({ id: item.id, data: { name: `update-${i}` } })), returnFields: 'name, age', context: keystone.createContext({ schemaName }), @@ -134,7 +134,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Delete a single item await deleteItem({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name age', itemId: items[0].id, context: keystone.createContext({ schemaName }), @@ -143,7 +143,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Retrieve items const allItems = await getItems({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name, age', context: keystone.createContext({ schemaName }), }); @@ -159,7 +159,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Delete multiple items const deletedItems = await deleteItems({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name age', items: items.map(item => item.id), context: keystone.createContext({ schemaName }), @@ -170,7 +170,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => // Get all the items back from db const allItems = await getItems({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name, age', context: keystone.createContext({ schemaName }), }); @@ -187,7 +187,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => await seedDb({ keystone }); const allItems = await getItems({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name, age', context: keystone.createContext({ schemaName }), }); @@ -202,7 +202,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => await seedDb({ keystone }); const allItems = await getItems({ keystone, - listName: 'Test', + listKey: 'Test', returnFields: 'name', where: { name: 'test' }, context: keystone.createContext({ schemaName }),