Skip to content

Commit 7a54a00

Browse files
committed
remove types for deprecated hooks
1 parent a8a3913 commit 7a54a00

File tree

32 files changed

+598
-512
lines changed

32 files changed

+598
-512
lines changed

.changeset/chilled-moons-walk.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@keystone-6/core": major
3+
---
4+
5+
Removed deprecated list and field hooks.
6+
Fixed field hooks of all the in built types

examples/custom-field/2-stars-field/index.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ export const stars =
3737
...config.hooks,
3838
// We use the `validateInput` hook to ensure that the user doesn't set an out of range value.
3939
// This hook is the key difference on the backend between the stars field type and the integer field type.
40-
async validateInput (args) {
41-
const val = args.resolvedData[meta.fieldKey]
42-
if (!(val == null || (val >= 0 && val <= maxStars))) {
43-
args.addValidationError(`The value must be within the range of 0-${maxStars}`)
40+
validate: {
41+
async create (args) {
42+
const val = args.resolvedData[meta.fieldKey]
43+
if (!(val == null || (val >= 0 && val <= maxStars))) {
44+
args.addValidationError(`The value must be within the range of 0-${maxStars}`)
45+
}
46+
await config.hooks?.validate?.create?.(args)
47+
},
48+
async update (args) {
49+
const val = args.resolvedData[meta.fieldKey]
50+
if (!(val == null || (val >= 0 && val <= maxStars))) {
51+
args.addValidationError(`The value must be within the range of 0-${maxStars}`)
52+
}
53+
await config.hooks?.validate?.update?.(args)
4454
}
45-
await config.hooks?.validateInput?.(args)
46-
},
55+
}
4756
},
4857
// all of these inputs are optional if they don't make sense for a particular field type
4958
input: {

examples/custom-field/schema.ts

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,58 @@ export const lists = {
2020
},
2121

2222
hooks: {
23-
resolveInput: async ({ resolvedData, operation, inputData, item, fieldKey }) => {
24-
console.log('Post.content.hooks.resolveInput', {
23+
resolveInput: {
24+
create: async ({ resolvedData, operation, inputData, item, fieldKey }) => {
25+
console.log('Post.content.hooks.resolveInput.create', {
26+
resolvedData,
27+
operation,
28+
inputData,
29+
item,
30+
fieldKey,
31+
})
32+
return resolvedData[fieldKey]
33+
},
34+
update: async ({ resolvedData, operation, inputData, item, fieldKey }) => {
35+
console.log('Post.content.hooks.resolveInput.update', {
36+
resolvedData,
37+
operation,
38+
inputData,
39+
item,
40+
fieldKey,
41+
})
42+
return resolvedData[fieldKey]
43+
},
44+
},
45+
validate: {
46+
create: async ({
2547
resolvedData,
26-
operation,
2748
inputData,
2849
item,
50+
addValidationError,
2951
fieldKey,
30-
})
31-
return resolvedData[fieldKey]
32-
},
33-
34-
validateInput: async ({
35-
resolvedData,
36-
inputData,
37-
item,
38-
addValidationError,
39-
fieldKey,
40-
}) => {
41-
console.log('Post.content.hooks.validateInput', {
52+
}) => {
53+
console.log('Post.content.hooks.validateInput.create', {
54+
resolvedData,
55+
inputData,
56+
item,
57+
fieldKey,
58+
})
59+
},
60+
update: async ({
4261
resolvedData,
4362
inputData,
4463
item,
64+
addValidationError,
4565
fieldKey,
46-
})
47-
},
66+
}) => {
67+
console.log('Post.content.hooks.validateInput.update', {
68+
resolvedData,
69+
inputData,
70+
item,
71+
fieldKey,
72+
})
73+
},
74+
}
4875
},
4976
}),
5077
rating: stars({
@@ -100,13 +127,22 @@ export const lists = {
100127
},
101128
},
102129

103-
validateInput: async ({ resolvedData, operation, inputData, item, addValidationError }) => {
104-
console.log('Post.hooks.validateInput', { resolvedData, operation, inputData, item })
130+
validate: {
131+
create: async ({ resolvedData, operation, inputData, item, addValidationError }) => {
132+
console.log('Post.hooks.validateInput.create', { resolvedData, operation, inputData, item })
105133

106-
if (Math.random() > 0.95) {
107-
addValidationError('oh oh, try again, this is part of the example')
108-
}
109-
},
134+
if (Math.random() > 0.95) {
135+
addValidationError('oh oh, try again, this is part of the example')
136+
}
137+
},
138+
update: async ({ resolvedData, operation, inputData, item, addValidationError }) => {
139+
console.log('Post.hooks.validateInput.update', { resolvedData, operation, inputData, item })
140+
141+
if (Math.random() > 0.95) {
142+
addValidationError('oh oh, try again, this is part of the example')
143+
}
144+
},
145+
}
110146
},
111147
}),
112148
} satisfies Lists

examples/custom-output-paths/schema.ts

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,46 @@ export const lists = {
1313
},
1414

1515
hooks: {
16-
afterOperation: async ({ context }) => {
17-
const posts = (await context.db.Post.findMany({
18-
where: {
19-
title: { equals: 'Home' },
20-
},
21-
22-
// we use Typescript's satisfies here as way to ensure that
23-
// this is the contextualised type - you don't need this
24-
//
25-
// it is helpful for us to check that the example is not
26-
// broken by code changes
27-
//
28-
29-
// TODO: FIXME, babel and pnpm issues
30-
})) as readonly { title: string, content: string }[]
31-
// })) satisfies readonly { title: string; content: string }[];
32-
33-
console.log(posts)
34-
},
16+
afterOperation: {
17+
create: async ({ context }) => {
18+
const posts = (await context.db.Post.findMany({
19+
where: {
20+
title: { equals: 'Home' },
21+
},
22+
23+
// we use Typescript's satisfies here as way to ensure that
24+
// this is the contextualised type - you don't need this
25+
//
26+
// it is helpful for us to check that the example is not
27+
// broken by code changes
28+
//
29+
30+
// TODO: FIXME, babel and pnpm issues
31+
})) as readonly { title: string, content: string }[]
32+
// })) satisfies readonly { title: string; content: string }[];
33+
34+
console.log(posts)
35+
},
36+
update: async ({ context }) => {
37+
const posts = (await context.db.Post.findMany({
38+
where: {
39+
title: { equals: 'Home' },
40+
},
41+
42+
// we use Typescript's satisfies here as way to ensure that
43+
// this is the contextualised type - you don't need this
44+
//
45+
// it is helpful for us to check that the example is not
46+
// broken by code changes
47+
//
48+
49+
// TODO: FIXME, babel and pnpm issues
50+
})) as readonly { title: string, content: string }[]
51+
// })) satisfies readonly { title: string; content: string }[];
52+
53+
console.log(posts)
54+
},
55+
}
3556
},
3657
}),
3758
} satisfies Lists

examples/custom-session-invalidation/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ We add one new field, `passwordChangedAt`, to the `Person` list. Setting the `pa
3737
passwordChangedAt: timestamp({
3838
access: () => false,
3939
hooks: {
40-
resolveInput: ({ resolvedData }) => {
41-
if (resolvedData.password) {
42-
return new Date();
43-
}
44-
return;
40+
resolveInput: {
41+
create: ({ resolvedData }) => {
42+
if (resolvedData.password) {
43+
return new Date();
44+
}
45+
return;
46+
},
47+
update: ({ resolvedData }) => {
48+
if (resolvedData.password) {
49+
return new Date();
50+
}
51+
return;
52+
},
4553
},
4654
},
4755
ui: {

examples/default-values/schema.ts

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,30 @@ export const lists = {
1717
{ label: 'High', value: 'high' },
1818
],
1919
hooks: {
20-
resolveInput ({ resolvedData, inputData }) {
21-
if (inputData.priority === null) {
22-
// default to high if "urgent" is in the label
23-
if (inputData.label && inputData.label.toLowerCase().includes('urgent')) {
24-
return 'high'
25-
} else {
26-
return 'low'
20+
resolveInput: {
21+
create ({ resolvedData, inputData }) {
22+
if (inputData.priority === null) {
23+
// default to high if "urgent" is in the label
24+
if (inputData.label && inputData.label.toLowerCase().includes('urgent')) {
25+
return 'high'
26+
} else {
27+
return 'low'
28+
}
2729
}
28-
}
29-
return resolvedData.priority
30-
},
30+
return resolvedData.priority
31+
},
32+
update ({ resolvedData, inputData }) {
33+
if (inputData.priority === null) {
34+
// default to high if "urgent" is in the label
35+
if (inputData.label && inputData.label.toLowerCase().includes('urgent')) {
36+
return 'high'
37+
} else {
38+
return 'low'
39+
}
40+
}
41+
return resolvedData.priority
42+
},
43+
}
3144
},
3245
}),
3346

@@ -39,33 +52,58 @@ export const lists = {
3952
many: false,
4053
hooks: {
4154
// dynamic default: if unassigned, find an anonymous user and assign the task to them
42-
async resolveInput ({ context, operation, resolvedData }) {
43-
if (resolvedData.assignedTo === null) {
44-
const [user] = await context.db.Person.findMany({
45-
where: { name: { equals: 'Anonymous' } },
46-
})
47-
48-
if (user) {
49-
return { connect: { id: user.id } }
55+
resolveInput: {
56+
async create ({ context, operation, resolvedData }) {
57+
if (resolvedData.assignedTo === null) {
58+
const [user] = await context.db.Person.findMany({
59+
where: { name: { equals: 'Anonymous' } },
60+
})
61+
62+
if (user) {
63+
return { connect: { id: user.id } }
64+
}
5065
}
51-
}
52-
53-
return resolvedData.assignedTo
54-
},
66+
67+
return resolvedData.assignedTo
68+
},
69+
async update ({ context, operation, resolvedData }) {
70+
if (resolvedData.assignedTo === null) {
71+
const [user] = await context.db.Person.findMany({
72+
where: { name: { equals: 'Anonymous' } },
73+
})
74+
75+
if (user) {
76+
return { connect: { id: user.id } }
77+
}
78+
}
79+
80+
return resolvedData.assignedTo
81+
},
82+
}
5583
},
5684
}),
5785

5886
// dynamic default: we set the due date to be 7 days in the future
5987
finishBy: timestamp({
6088
hooks: {
61-
resolveInput ({ resolvedData, inputData, operation }) {
62-
if (inputData.finishBy == null) {
63-
const date = new Date()
64-
date.setUTCDate(new Date().getUTCDate() + 7)
65-
return date
66-
}
67-
return resolvedData.finishBy
68-
},
89+
resolveInput: {
90+
create ({ resolvedData, inputData, operation }) {
91+
if (inputData.finishBy == null) {
92+
const date = new Date()
93+
date.setUTCDate(new Date().getUTCDate() + 7)
94+
return date
95+
}
96+
return resolvedData.finishBy
97+
},
98+
update ({ resolvedData, inputData, operation }) {
99+
if (inputData.finishBy == null) {
100+
const date = new Date()
101+
date.setUTCDate(new Date().getUTCDate() + 7)
102+
return date
103+
}
104+
return resolvedData.finishBy
105+
},
106+
}
69107
},
70108
}),
71109

examples/extend-graphql-subscriptions/schema.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,30 @@ export const lists = {
1212
access: allowAll,
1313
hooks: {
1414
// this hook publishes posts to the 'POST_UPDATED' channel when a post mutated
15-
afterOperation: async ({ item }) => {
16-
// WARNING: passing this item directly to pubSub bypasses any contextual access control
17-
// if you want access control, you need to use a different architecture
18-
//
19-
// tl;dr Keystone access filters are not respected in this scenario
20-
console.log('POST_UPDATED', { id: item?.id })
21-
22-
pubSub.publish('POST_UPDATED', {
23-
postUpdated: item,
24-
})
25-
},
15+
afterOperation: {
16+
create: async ({ item }) => {
17+
// WARNING: passing this item directly to pubSub bypasses any contextual access control
18+
// if you want access control, you need to use a different architecture
19+
//
20+
// tl;dr Keystone access filters are not respected in this scenario
21+
console.log('POST_UPDATED', { id: item?.id })
22+
23+
pubSub.publish('POST_UPDATED', {
24+
postUpdated: item,
25+
})
26+
},
27+
update: async ({ item }) => {
28+
// WARNING: passing this item directly to pubSub bypasses any contextual access control
29+
// if you want access control, you need to use a different architecture
30+
//
31+
// tl;dr Keystone access filters are not respected in this scenario
32+
console.log('POST_UPDATED', { id: item?.id })
33+
34+
pubSub.publish('POST_UPDATED', {
35+
postUpdated: item,
36+
})
37+
},
38+
}
2639
},
2740
fields: {
2841
title: text({ validation: { isRequired: true } }),

0 commit comments

Comments
 (0)