diff --git a/packages/server-side-graphql-client/index.js b/packages/server-side-graphql-client/index.js index daa104234d8..7b4d2a6b4a3 100644 --- a/packages/server-side-graphql-client/index.js +++ b/packages/server-side-graphql-client/index.js @@ -8,7 +8,7 @@ const { updateItems, deleteItem, deleteItems, -} = require('./lib/server-side-graphql-client'); +} = require("./lib/server-side-graphql-client"); module.exports = { runCustomQuery, 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 f10cc5c4865..c81c11210d7 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 @@ -2,7 +2,11 @@ const runQuery = async ({ keystone, query, variables, context }) => { const { data, errors } = await keystone.executeGraphQL({ context: context || - keystone.createContext({ schemaName: 'public', authentication: {}, skipAccessControl: true }), + keystone.createContext({ + schemaName: "public", + authentication: {}, + skipAccessControl: true, + }), query, variables, @@ -11,7 +15,14 @@ const runQuery = async ({ keystone, query, variables, context }) => { return data; }; -const _runChunkedMutation = async ({ keystone, query, gqlName, pageSize, items, context }) => { +const _runChunkedMutation = async ({ + keystone, + query, + gqlName, + pageSize, + items, + context, +}) => { if (pageSize <= 0) pageSize = 1; const chunks = items.reduce((accum, item, index) => { @@ -27,7 +38,9 @@ const _runChunkedMutation = async ({ keystone, query, gqlName, pageSize, items, }, []); const result = await Promise.all( - chunks.map(chunk => runQuery({ query, variables: { items: chunk }, keystone, context })) + chunks.map((chunk) => + runQuery({ query, variables: { items: chunk }, keystone, context }) + ) ); /* @@ -35,17 +48,30 @@ const _runChunkedMutation = async ({ keystone, query, gqlName, pageSize, items, * We need to combine all objects into one array keyed by the `createUsers`, such that, the output is: [{id: '123', name: 'aman'}, {id: '456', name: 'Mike'}] */ - return [].concat(...result.map(item => item[gqlName])); + return [].concat(...result.map((item) => item[gqlName])); }; -const createItem = async ({ keystone, listKey, item, returnFields = `id`, context }) => { - const { createMutationName, createInputName } = keystone.lists[listKey].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} } }`; - const result = await runQuery({ keystone, query, variables: { item }, context }); + const result = await runQuery({ + keystone, + query, + variables: { item }, + context, + }); return result[createMutationName]; }; @@ -57,7 +83,9 @@ const createItems = async ({ returnFields = `id`, context, }) => { - const { createManyMutationName, createManyInputName } = keystone.lists[listKey].gqlNames; + const { createManyMutationName, createManyInputName } = keystone.lists[ + listKey + ].gqlNames; const query = `mutation ($items: [${createManyInputName}]){ ${createManyMutationName}(data: $items) { ${returnFields} } @@ -73,11 +101,22 @@ const createItems = async ({ }); }; -const getItem = async ({ keystone, listKey, itemId, returnFields = `id`, context }) => { +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 }); + const result = await runQuery({ + keystone, + query, + variables: { id: itemId }, + context, + }); return result[itemQueryName]; }; @@ -114,8 +153,16 @@ const getItems = async ({ return allItems; }; -const updateItem = async ({ keystone, listKey, item, returnFields = `id`, context }) => { - const { updateMutationName, updateInputName } = keystone.lists[listKey].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} } @@ -139,7 +186,9 @@ const updateItems = async ({ returnFields = `id`, context, }) => { - const { updateManyMutationName, updateManyInputName } = keystone.lists[listKey].gqlNames; + const { updateManyMutationName, updateManyInputName } = keystone.lists[ + listKey + ].gqlNames; const query = `mutation ($items: [${updateManyInputName}]){ ${updateManyMutationName}(data: $items) { ${returnFields} } @@ -154,14 +203,25 @@ const updateItems = async ({ }); }; -const deleteItem = async ({ keystone, listKey, itemId, returnFields = `id`, context }) => { +const deleteItem = async ({ + keystone, + listKey, + itemId, + returnFields = `id`, + context, +}) => { const { deleteMutationName } = keystone.lists[listKey].gqlNames; const query = `mutation ($id: ID!){ ${deleteMutationName}(id: $id) { ${returnFields} } }`; - const result = await runQuery({ keystone, query, variables: { id: itemId }, context }); + const result = await runQuery({ + keystone, + query, + variables: { id: itemId }, + context, + }); return result[deleteMutationName]; }; diff --git a/packages/server-side-graphql-client/tests/index.test.js b/packages/server-side-graphql-client/tests/index.test.js index 49a73878dae..3714cc2f789 100644 --- a/packages/server-side-graphql-client/tests/index.test.js +++ b/packages/server-side-graphql-client/tests/index.test.js @@ -12,7 +12,10 @@ const { updateItems, } = require('../index'); -const testData = [{ data: { name: 'test', age: 30 } }, { data: { name: 'test2', age: 40 } }]; +const testData = [ + { data: { name: 'test', age: 30 } }, + { data: { name: 'test2', age: 40 } }, +]; const schemaName = 'testing'; const seedDb = ({ keystone }) => @@ -26,7 +29,7 @@ const seedDb = ({ keystone }) => function setupKeystone(adapterName) { return setupServer({ adapterName, - createLists: keystone => { + createLists: (keystone) => { keystone.createList('Test', { fields: { name: { type: Text }, @@ -82,7 +85,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => context: keystone.createContext({ schemaName }), }); - expect(allItems).toEqual(testData.map(x => x.data)); + expect(allItems).toEqual(testData.map((x) => x.data)); }) ); }); @@ -113,7 +116,10 @@ multiAdapterRunners().map(({ runner, adapterName }) => const items = await updateItems({ keystone, listKey: 'Test', - items: seedItems.map((item, i) => ({ id: item.id, data: { name: `update-${i}` } })), + items: seedItems.map((item, i) => ({ + id: item.id, + data: { name: `update-${i}` }, + })), returnFields: 'name, age', context: keystone.createContext({ schemaName }), }); @@ -161,11 +167,11 @@ multiAdapterRunners().map(({ runner, adapterName }) => keystone, listKey: 'Test', returnFields: 'name age', - items: items.map(item => item.id), + items: items.map((item) => item.id), context: keystone.createContext({ schemaName }), }); - expect(deletedItems).toEqual(testData.map(d => d.data)); + expect(deletedItems).toEqual(testData.map((d) => d.data)); // Get all the items back from db const allItems = await getItems({ @@ -192,7 +198,7 @@ multiAdapterRunners().map(({ runner, adapterName }) => context: keystone.createContext({ schemaName }), }); - expect(allItems).toEqual(testData.map(x => x.data)); + expect(allItems).toEqual(testData.map((x) => x.data)); }) ); test(