Skip to content

Commit 9b3b4fa

Browse files
committed
Add listKey argument to hooks
1 parent d29a348 commit 9b3b4fa

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed

.changeset/big-jeans-itch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@keystonejs/keystone': minor
3+
---
4+
5+
Added `listKey` as an argument to all hooks.

docs/api/hooks.md

+39
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,20 @@ The result is passed to [the next function in the execution order](/docs/guides/
157157
| `originalInput` | `Object` | The data received by the GraphQL mutation |
158158
| `resolvedData` | `Object` | The data received by the GraphQL mutation plus defaults values |
159159
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
160+
| `listKey` | `String` | The key for the list being operated on |
160161

161162
#### Usage
162163

163164
<!-- prettier-ignore -->
165+
164166
```js
165167
const resolveInput = ({
166168
operation,
167169
existingItem,
168170
originalInput,
169171
resolvedData,
170172
context,
173+
listKey,
171174
}) => {
172175
// Input resolution logic. Object returned is used in place of `resolvedData`.
173176
return resolvedData;
@@ -194,10 +197,12 @@ Return values are ignored.
194197
| `resolvedData` | `Object` | The data received by the GraphQL mutation plus defaults values |
195198
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
196199
| `addFieldValidationError` | `Function` | Used to set a field validation error; accepts a `String` |
200+
| `listKey` | `String` | The key for the list being operated on |
197201

198202
#### Usage
199203

200204
<!-- prettier-ignore -->
205+
201206
```js
202207
const validateInput = ({
203208
operation,
@@ -206,6 +211,7 @@ const validateInput = ({
206211
resolvedData,
207212
context,
208213
addFieldValidationError,
214+
listKey,
209215
}) => {
210216
// Throw error objects or register validation errors with addFieldValidationError(<String>)
211217
// Return values ignored
@@ -231,17 +237,20 @@ Return values are ignored.
231237
| `originalInput` | `Object` | The data received by the GraphQL mutation |
232238
| `resolvedData` | `Object` | The data received by the GraphQL mutation plus defaults values |
233239
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
240+
| `listKey` | `String` | The key for the list being operated on |
234241

235242
#### Usage
236243

237244
<!-- prettier-ignore -->
245+
238246
```js
239247
const beforeChange = ({
240248
operation,
241249
existingItem,
242250
originalInput,
243251
resolvedData,
244252
context,
253+
listKey,
245254
}) => {
246255
// Perform side effects
247256
// Return values ignored
@@ -271,17 +280,20 @@ Return values are ignored.
271280
| `originalInput` | `Object` | The data received by the GraphQL mutation |
272281
| `updatedItem` | `Object` | The new/currently stored item |
273282
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
283+
| `listKey` | `String` | The key for the list being operated on |
274284

275285
#### Usage
276286

277287
<!-- prettier-ignore -->
288+
278289
```js
279290
const afterChange = ({
280291
operation,
281292
existingItem,
282293
originalInput,
283294
updatedItem,
284295
context,
296+
listKey,
285297
}) => {
286298
// Perform side effects
287299
// Return values ignored
@@ -305,16 +317,19 @@ Should throw or register errors with `addFieldValidationError(<String>)` if the
305317
| `existingItem` | `Object` | The current stored item |
306318
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
307319
| `addFieldValidationError` | `Function` | Used to set a field validation error; accepts a `String` |
320+
| `listKey` | `String` | The key for the list being operated on |
308321

309322
#### Usage
310323

311324
<!-- prettier-ignore -->
325+
312326
```js
313327
const validateDelete = ({
314328
operation,
315329
existingItem,
316330
context,
317331
addFieldValidationError,
332+
listKey,
318333
}) => {
319334
// Throw error objects or register validation errors with addFieldValidationError(<String>)
320335
// Return values ignored
@@ -338,15 +353,18 @@ Return values are ignored.
338353
| `operation` | `String` | The operation being performed (`delete` in this case) |
339354
| `existingItem` | `Object` | The current stored item |
340355
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
356+
| `listKey` | `String` | The key for the list being operated on |
341357

342358
#### Usage
343359

344360
<!-- prettier-ignore -->
361+
345362
```js
346363
const beforeDelete = ({
347364
operation,
348365
existingItem,
349366
context,
367+
listKey,
350368
}) => {
351369
// Perform side effects
352370
// Return values ignored
@@ -372,15 +390,18 @@ Return values are ignored.
372390
| `operation` | `String` | The operation being performed (`delete` in this case) |
373391
| `existingItem` | `Object` | The previously stored item, now deleted |
374392
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
393+
| `listKey` | `String` | The key for the list being operated on |
375394

376395
#### Usage
377396

378397
<!-- prettier-ignore -->
398+
379399
```js
380400
const afterDelete = ({
381401
operation,
382402
existingItem,
383403
context,
404+
listKey,
384405
}) => {
385406
// Perform side effects
386407
// Return values ignored
@@ -405,15 +426,18 @@ The result is passed to [the next function in the execution order](/docs/guides/
405426
| `operation` | `String` | The operation being performed (`authenticate` in this case) |
406427
| `originalInput` | `Object` | The data received by the GraphQL mutation |
407428
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
429+
| `listKey` | `String` | The key for the list being operated on |
408430

409431
#### Usage
410432

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

444469
#### Usage
445470

446471
<!-- prettier-ignore -->
472+
447473
```js
448474
const validateAuthInput = ({
449475
operation,
450476
originalInput,
451477
resolvedData,
452478
context,
453479
addFieldValidationError,
480+
listKey,
454481
}) => {
455482
// Throw error objects or register validation errors with addValidationError(<String>)
456483
// Return values ignored
@@ -475,16 +502,19 @@ Return values are ignored.
475502
| `originalInput` | `Object` | The data received by the GraphQL mutation |
476503
| `resolvedData` | `Object` | The data received by the GraphQL mutation or returned by `resolveAuthInput`, if defined |
477504
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
505+
| `listKey` | `String` | The key for the list being operated on |
478506

479507
#### Usage
480508

481509
<!-- prettier-ignore -->
510+
482511
```js
483512
const beforeAuth = ({
484513
operation,
485514
originalInput,
486515
resolvedData,
487516
context,
517+
listKey,
488518
}) => {
489519
// Perform side effects
490520
// Return values ignored
@@ -515,10 +545,12 @@ Return values are ignored.
515545
| `originalInput` | `Object` | The data received by the GraphQL mutation |
516546
| `resolvedData` | `Object` | The data received by the GraphQL mutation or returned by `resolveAuthInput`, if defined |
517547
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
548+
| `listKey` | `String` | The key for the list being operated on |
518549

519550
#### Usage
520551

521552
<!-- prettier-ignore -->
553+
522554
```js
523555
const afterAuth = ({
524556
operation,
@@ -529,6 +561,7 @@ const afterAuth = ({
529561
originalInput,
530562
resolvedData,
531563
context,
564+
listKey,
532565
}) => {
533566
// Perform side effects
534567
// Return values ignored
@@ -551,14 +584,17 @@ Return values are ignored.
551584
| :---------- | :--------------- | :---------------------------------------------------------------------------------------------------------------------------- |
552585
| `operation` | `String` | The operation being performed (`authenticate` in this case) |
553586
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
587+
| `listKey` | `String` | The key for the list being operated on |
554588

555589
#### Usage
556590

557591
<!-- prettier-ignore -->
592+
558593
```js
559594
const beforeUnauth = ({
560595
operation,
561596
context,
597+
listKey,
562598
}) => {
563599
// Perform side effects
564600
// Return values ignored
@@ -586,10 +622,12 @@ Return values are ignored.
586622
| `listKey` | `String` | The list key of the unauthenticated user (if there was one) |
587623
| `itemid` | `String` | The item ID of the unauthenticated user (if there was one) |
588624
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/essentials/data.html#context) for this request |
625+
| `listKey` | `String` | The key for the list being operated on |
589626

590627
#### Usage
591628

592629
<!-- prettier-ignore -->
630+
593631
```js
594632
const afterAuth = ({
595633
operation,
@@ -600,6 +638,7 @@ const afterAuth = ({
600638
originalInput,
601639
resolvedData,
602640
context,
641+
listKey,
603642
}) => {
604643
// Perform side effects
605644
// Return values ignored

packages/keystone/lib/ListTypes/hooks.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class HookManager {
2929
}
3030

3131
async resolveInput({ resolvedData, existingItem, context, operation, originalInput }) {
32-
const args = { resolvedData, existingItem, context, originalInput, operation };
32+
const { listKey } = this;
33+
const args = { resolvedData, existingItem, context, originalInput, operation, listKey };
3334

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

7273
async validateInput({ resolvedData, existingItem, context, operation, originalInput }) {
73-
const args = { resolvedData, existingItem, context, originalInput, operation };
74+
const { listKey } = this;
75+
const args = { resolvedData, existingItem, context, originalInput, operation, listKey };
7476
// Check for isRequired
7577
const fieldValidationErrors = this.fields
7678
.filter(
@@ -97,7 +99,8 @@ class HookManager {
9799
}
98100

99101
async validateDelete({ existingItem, context, operation }) {
100-
const args = { existingItem, context, operation };
102+
const { listKey } = this;
103+
const args = { existingItem, context, operation, listKey };
101104
const fields = this.fields;
102105
await this._validateHook({ args, fields, operation, hookName: 'validateDelete' });
103106
}
@@ -131,22 +134,26 @@ class HookManager {
131134
}
132135

133136
async beforeChange({ resolvedData, existingItem, context, operation, originalInput }) {
134-
const args = { resolvedData, existingItem, context, originalInput, operation };
137+
const { listKey } = this;
138+
const args = { resolvedData, existingItem, context, originalInput, operation, listKey };
135139
await this._runHook({ args, fieldObject: resolvedData, hookName: 'beforeChange' });
136140
}
137141

138142
async beforeDelete({ existingItem, context, operation }) {
139-
const args = { existingItem, context, operation };
143+
const { listKey } = this;
144+
const args = { existingItem, context, operation, listKey };
140145
await this._runHook({ args, fieldObject: existingItem, hookName: 'beforeDelete' });
141146
}
142147

143148
async afterChange({ updatedItem, existingItem, context, operation, originalInput }) {
144-
const args = { updatedItem, originalInput, existingItem, context, operation };
149+
const { listKey } = this;
150+
const args = { updatedItem, originalInput, existingItem, context, operation, listKey };
145151
await this._runHook({ args, fieldObject: updatedItem, hookName: 'afterChange' });
146152
}
147153

148154
async afterDelete({ existingItem, context, operation }) {
149-
const args = { existingItem, context, operation };
155+
const { listKey } = this;
156+
const args = { existingItem, context, operation, listKey };
150157
await this._runHook({ args, fieldObject: existingItem, hookName: 'afterDelete' });
151158
}
152159

0 commit comments

Comments
 (0)