Skip to content

Commit

Permalink
Add fieldPath argument to field hooks (keystonejs#3849)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamsi authored Oct 8, 2020
1 parent eff9f10 commit 20c921c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-parents-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': minor
---

Added `fieldPath` as an argument to all field hooks.
14 changes: 14 additions & 0 deletions docs/api/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ The result is passed to [the next function in the execution order](/docs/guides/
| `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 |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

#### Usage

Expand All @@ -171,6 +172,7 @@ const resolveInput = ({
resolvedData,
context,
listKey,
fieldPath, // exists only for field hooks
}) => {
// Input resolution logic. Object returned is used in place of `resolvedData`.
return resolvedData;
Expand Down Expand Up @@ -198,6 +200,7 @@ Return values are ignored.
| `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 |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

#### Usage

Expand All @@ -212,6 +215,7 @@ const validateInput = ({
context,
addFieldValidationError,
listKey,
fieldPath, // exists only for field hooks
}) => {
// Throw error objects or register validation errors with addFieldValidationError(<String>)
// Return values ignored
Expand All @@ -238,6 +242,7 @@ 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 |
| `listKey` | `String` | The key for the list being operated on |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

#### Usage

Expand All @@ -251,6 +256,7 @@ const beforeChange = ({
resolvedData,
context,
listKey,
fieldPath, // exists only for field hooks
}) => {
// Perform side effects
// Return values ignored
Expand Down Expand Up @@ -281,6 +287,7 @@ Return values are ignored.
| `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 |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

#### Usage

Expand All @@ -294,6 +301,7 @@ const afterChange = ({
updatedItem,
context,
listKey,
fieldPath, // exists only for field hooks
}) => {
// Perform side effects
// Return values ignored
Expand All @@ -318,6 +326,7 @@ Should throw or register errors with `addFieldValidationError(<String>)` if the
| `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 |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

#### Usage

Expand All @@ -330,6 +339,7 @@ const validateDelete = ({
context,
addFieldValidationError,
listKey,
fieldPath, // exists only for field hooks
}) => {
// Throw error objects or register validation errors with addFieldValidationError(<String>)
// Return values ignored
Expand All @@ -354,6 +364,7 @@ Return values are ignored.
| `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 |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

#### Usage

Expand All @@ -365,6 +376,7 @@ const beforeDelete = ({
existingItem,
context,
listKey,
fieldPath, // exists only for field hooks
}) => {
// Perform side effects
// Return values ignored
Expand All @@ -391,6 +403,7 @@ Return values are ignored.
| `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 |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

#### Usage

Expand All @@ -402,6 +415,7 @@ const afterDelete = ({
existingItem,
context,
listKey,
fieldPath, // exists only for field hooks
}) => {
// Perform side effects
// Return values ignored
Expand Down
6 changes: 3 additions & 3 deletions packages/keystone/lib/ListTypes/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class HookManager {
...resolvedData,
...(await mapToFields(
this.fields.filter(field => field.hooks.resolveInput),
field => field.hooks.resolveInput({ ...args, resolvedData })
field => field.hooks.resolveInput({ ...args, fieldPath: field.path, resolvedData })
)),
};

Expand Down Expand Up @@ -114,7 +114,7 @@ class HookManager {
await mapToFields(fields, field => field[hookName](args));
await mapToFields(
fields.filter(field => field.hooks[hookName]),
field => field.hooks[hookName](args)
field => field.hooks[hookName]({ fieldPath: field.path, ...args })
);
if (fieldValidationErrors.length) {
this._throwValidationFailure({ errors: fieldValidationErrors, operation, originalInput });
Expand Down Expand Up @@ -163,7 +163,7 @@ class HookManager {
await mapToFields(fields, field => field[hookName](args));
await mapToFields(
fields.filter(field => field.hooks[hookName]),
field => field.hooks[hookName](args)
field => field.hooks[hookName]({ fieldPath: field.path, ...args })
);

if (this.hooks[hookName]) await this.hooks[hookName](args);
Expand Down

0 comments on commit 20c921c

Please sign in to comment.