Skip to content

Commit

Permalink
Rename listName to listKey in server-side-graphql-client (keystonejs#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Jul 28, 2020
1 parent 24af20b commit a8cb4e4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
6 changes: 3 additions & 3 deletions .changeset/two-dogs-push.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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' } }]
})
```
60 changes: 30 additions & 30 deletions packages/server-side-graphql-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
});
Expand All @@ -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 });
};
```

Expand All @@ -67,7 +67,7 @@ keystone.createList('Page', {
const pageToCopy = resolvedData.copy
? await getItem({
keystone,
listName: 'Page',
listKey: 'Page',
itemId: resolvedData.copy,
returnFields: 'name, content',
})
Expand Down Expand Up @@ -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. |

Expand All @@ -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`,
});
Expand All @@ -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`

Expand Down Expand Up @@ -168,7 +168,7 @@ const dummyUsers = [
const addUsers = async () => {
const users = await createItems({
keystone,
listName: 'User',
listKey: 'User',
items: dummyUsers,
returnFields: `name`,
});
Expand All @@ -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`

Expand All @@ -205,7 +205,7 @@ keystone.createList('User', {
const getUser = async ({ itemId }) => {
const user = await getItem({
keystone,
listName: 'User',
listKey: 'User',
itemId,
returnFields: 'id, name',
});
Expand Down Expand Up @@ -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' },
});
Expand All @@ -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`

Expand All @@ -280,7 +280,7 @@ keystone.createList('User', {
const updateUser = async updateUser => {
const updatedUser = await updateItem({
keystone,
listName: 'User',
listKey: 'User',
item: updateUser,
returnFields: 'name',
});
Expand All @@ -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`

Expand All @@ -316,7 +316,7 @@ keystone.createList('User', {
const updateUsers = async (updateUsers) => {
const users = await updateItems({
keystone,
listName: 'User',
listKey: 'User',
items: updateUsers,
returnFields: 'name'
});
Expand All @@ -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`
Expand All @@ -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');
Expand Down Expand Up @@ -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']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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} }
Expand All @@ -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} }
Expand All @@ -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 });
Expand All @@ -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;
Expand All @@ -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} }
Expand All @@ -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} }
Expand All @@ -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} }
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit a8cb4e4

Please sign in to comment.