Skip to content

Commit

Permalink
add server-side graphQL client
Browse files Browse the repository at this point in the history
  • Loading branch information
MadeByMike authored and singhArmani committed Jul 30, 2020
1 parent 086b6ba commit c74d1d8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/server-side-graphql-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
updateItems,
deleteItem,
deleteItems,
} = require('./lib/server-side-graphql-client');
} = require("./lib/server-side-graphql-client");

module.exports = {
runCustomQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand All @@ -27,25 +38,40 @@ 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 })
)
);

/*
* The result is of the format: [{createUsers: [{id: '123', name: 'aman'}]}, {createUsers: [{id: '456', name: 'mike'}]}].
* 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];
};

Expand All @@ -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} }
Expand All @@ -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];
};

Expand Down Expand Up @@ -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} }
Expand All @@ -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} }
Expand All @@ -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];
};

Expand Down
20 changes: 13 additions & 7 deletions packages/server-side-graphql-client/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) =>
Expand All @@ -26,7 +29,7 @@ const seedDb = ({ keystone }) =>
function setupKeystone(adapterName) {
return setupServer({
adapterName,
createLists: keystone => {
createLists: (keystone) => {
keystone.createList('Test', {
fields: {
name: { type: Text },
Expand Down Expand Up @@ -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));
})
);
});
Expand Down Expand Up @@ -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 }),
});
Expand Down Expand Up @@ -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({
Expand All @@ -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(
Expand Down

0 comments on commit c74d1d8

Please sign in to comment.