Skip to content

Commit

Permalink
Add listKey argument to hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Aug 19, 2020
1 parent d29a348 commit 9b3b4fa
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-jeans-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': minor
---

Added `listKey` as an argument to all hooks.
39 changes: 39 additions & 0 deletions docs/api/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,20 @@ The result is passed to [the next function in the execution order](/docs/guides/
| `originalInput` | `Object` | The data received by the GraphQL mutation |
| `resolvedData` | `Object` | The data received by the GraphQL mutation plus defaults values |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const resolveInput = ({
operation,
existingItem,
originalInput,
resolvedData,
context,
listKey,
}) => {
// Input resolution logic. Object returned is used in place of `resolvedData`.
return resolvedData;
Expand All @@ -194,10 +197,12 @@ Return values are ignored.
| `resolvedData` | `Object` | The data received by the GraphQL mutation plus defaults values |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `addFieldValidationError` | `Function` | Used to set a field validation error; accepts a `String` |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const validateInput = ({
operation,
Expand All @@ -206,6 +211,7 @@ const validateInput = ({
resolvedData,
context,
addFieldValidationError,
listKey,
}) => {
// Throw error objects or register validation errors with addFieldValidationError(<String>)
// Return values ignored
Expand All @@ -231,17 +237,20 @@ Return values are ignored.
| `originalInput` | `Object` | The data received by the GraphQL mutation |
| `resolvedData` | `Object` | The data received by the GraphQL mutation plus defaults values |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const beforeChange = ({
operation,
existingItem,
originalInput,
resolvedData,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand Down Expand Up @@ -271,17 +280,20 @@ Return values are ignored.
| `originalInput` | `Object` | The data received by the GraphQL mutation |
| `updatedItem` | `Object` | The new/currently stored item |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const afterChange = ({
operation,
existingItem,
originalInput,
updatedItem,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand All @@ -305,16 +317,19 @@ Should throw or register errors with `addFieldValidationError(<String>)` if the
| `existingItem` | `Object` | The current stored item |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `addFieldValidationError` | `Function` | Used to set a field validation error; accepts a `String` |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const validateDelete = ({
operation,
existingItem,
context,
addFieldValidationError,
listKey,
}) => {
// Throw error objects or register validation errors with addFieldValidationError(<String>)
// Return values ignored
Expand All @@ -338,15 +353,18 @@ Return values are ignored.
| `operation` | `String` | The operation being performed (`delete` in this case) |
| `existingItem` | `Object` | The current stored item |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const beforeDelete = ({
operation,
existingItem,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand All @@ -372,15 +390,18 @@ Return values are ignored.
| `operation` | `String` | The operation being performed (`delete` in this case) |
| `existingItem` | `Object` | The previously stored item, now deleted |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const afterDelete = ({
operation,
existingItem,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand All @@ -405,15 +426,18 @@ The result is passed to [the next function in the execution order](/docs/guides/
| `operation` | `String` | The operation being performed (`authenticate` in this case) |
| `originalInput` | `Object` | The data received by the GraphQL mutation |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const resolveAuthInput = ({
operation,
originalInput,
context,
listKey,
}) => {
// Input resolution logic
// Object returned is used in place of resolvedData
Expand All @@ -440,17 +464,20 @@ Return values are ignored.
| `resolvedData` | `Object` | The data received by the GraphQL mutation or returned by `resolveAuthInput`, if defined |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
| `addValidationError` | `Function` | Used to set a validation error; accepts a message `String` |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const validateAuthInput = ({
operation,
originalInput,
resolvedData,
context,
addFieldValidationError,
listKey,
}) => {
// Throw error objects or register validation errors with addValidationError(<String>)
// Return values ignored
Expand All @@ -475,16 +502,19 @@ Return values are ignored.
| `originalInput` | `Object` | The data received by the GraphQL mutation |
| `resolvedData` | `Object` | The data received by the GraphQL mutation or returned by `resolveAuthInput`, if defined |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const beforeAuth = ({
operation,
originalInput,
resolvedData,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand Down Expand Up @@ -515,10 +545,12 @@ Return values are ignored.
| `originalInput` | `Object` | The data received by the GraphQL mutation |
| `resolvedData` | `Object` | The data received by the GraphQL mutation or returned by `resolveAuthInput`, if defined |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const afterAuth = ({
operation,
Expand All @@ -529,6 +561,7 @@ const afterAuth = ({
originalInput,
resolvedData,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand All @@ -551,14 +584,17 @@ Return values are ignored.
| :---------- | :--------------- | :---------------------------------------------------------------------------------------------------------------------------- |
| `operation` | `String` | The operation being performed (`authenticate` in this case) |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const beforeUnauth = ({
operation,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand Down Expand Up @@ -586,10 +622,12 @@ Return values are ignored.
| `listKey` | `String` | The list key of the unauthenticated user (if there was one) |
| `itemid` | `String` | The item ID of the unauthenticated user (if there was one) |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
| `listKey` | `String` | The key for the list being operated on |

#### Usage

<!-- prettier-ignore -->

```js
const afterAuth = ({
operation,
Expand All @@ -600,6 +638,7 @@ const afterAuth = ({
originalInput,
resolvedData,
context,
listKey,
}) => {
// Perform side effects
// Return values ignored
Expand Down
21 changes: 14 additions & 7 deletions packages/keystone/lib/ListTypes/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class HookManager {
}

async resolveInput({ resolvedData, existingItem, context, operation, originalInput }) {
const args = { resolvedData, existingItem, context, originalInput, operation };
const { listKey } = this;
const args = { resolvedData, existingItem, context, originalInput, operation, listKey };

// First we run the field type hooks
// NOTE: resolveInput is run on _every_ field, regardless if it has a value
Expand Down Expand Up @@ -70,7 +71,8 @@ class HookManager {
}

async validateInput({ resolvedData, existingItem, context, operation, originalInput }) {
const args = { resolvedData, existingItem, context, originalInput, operation };
const { listKey } = this;
const args = { resolvedData, existingItem, context, originalInput, operation, listKey };
// Check for isRequired
const fieldValidationErrors = this.fields
.filter(
Expand All @@ -97,7 +99,8 @@ class HookManager {
}

async validateDelete({ existingItem, context, operation }) {
const args = { existingItem, context, operation };
const { listKey } = this;
const args = { existingItem, context, operation, listKey };
const fields = this.fields;
await this._validateHook({ args, fields, operation, hookName: 'validateDelete' });
}
Expand Down Expand Up @@ -131,22 +134,26 @@ class HookManager {
}

async beforeChange({ resolvedData, existingItem, context, operation, originalInput }) {
const args = { resolvedData, existingItem, context, originalInput, operation };
const { listKey } = this;
const args = { resolvedData, existingItem, context, originalInput, operation, listKey };
await this._runHook({ args, fieldObject: resolvedData, hookName: 'beforeChange' });
}

async beforeDelete({ existingItem, context, operation }) {
const args = { existingItem, context, operation };
const { listKey } = this;
const args = { existingItem, context, operation, listKey };
await this._runHook({ args, fieldObject: existingItem, hookName: 'beforeDelete' });
}

async afterChange({ updatedItem, existingItem, context, operation, originalInput }) {
const args = { updatedItem, originalInput, existingItem, context, operation };
const { listKey } = this;
const args = { updatedItem, originalInput, existingItem, context, operation, listKey };
await this._runHook({ args, fieldObject: updatedItem, hookName: 'afterChange' });
}

async afterDelete({ existingItem, context, operation }) {
const args = { existingItem, context, operation };
const { listKey } = this;
const args = { existingItem, context, operation, listKey };
await this._runHook({ args, fieldObject: existingItem, hookName: 'afterDelete' });
}

Expand Down
Loading

0 comments on commit 9b3b4fa

Please sign in to comment.