Skip to content

Commit 7c94710

Browse files
fix(hooks): Fix a regression on the custom resolve for mutations.
1 parent 13a2215 commit 7c94710

File tree

3 files changed

+113
-32
lines changed

3 files changed

+113
-32
lines changed

lib/injectHooks.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
exports.__esModule = true;
33
exports.injectHooks = void 0;
44
function injectListHooks(declaration, injectFunctions) {
5+
var _a;
56
if (!declaration.list) {
67
declaration.list = {};
78
}
89
// If the list is resolved by a custom function, we don't need to inject hooks
9-
if (declaration.list.resolve) {
10+
if ((_a = declaration === null || declaration === void 0 ? void 0 : declaration.list) === null || _a === void 0 ? void 0 : _a.resolve) {
1011
return;
1112
}
1213
var beforeList = Array.isArray(declaration.list.before)
@@ -27,16 +28,16 @@ function injectListHooks(declaration, injectFunctions) {
2728
: afterList;
2829
}
2930
function injectUpdateHooks(declaration, injectFunctions) {
30-
var _a;
31+
var _a, _b;
3132
if (!((_a = declaration.actions) === null || _a === void 0 ? void 0 : _a.includes('update')))
3233
return;
33-
if (!declaration.update || 'type' in declaration.update) {
34-
declaration.update = {};
35-
}
3634
// If the update is resolved by a custom function, we don't need to inject hooks
37-
if (declaration.update.resolve) {
35+
if ((_b = declaration === null || declaration === void 0 ? void 0 : declaration.update) === null || _b === void 0 ? void 0 : _b.resolve) {
3836
return;
3937
}
38+
if (!declaration.update) {
39+
declaration.update = {};
40+
}
4041
var beforeUpdate = Array.isArray(declaration.update.before)
4142
? declaration.update.before
4243
: declaration.update.before
@@ -55,16 +56,16 @@ function injectUpdateHooks(declaration, injectFunctions) {
5556
: afterUpdate;
5657
}
5758
function injectCreateHooks(declaration, injectFunctions) {
58-
var _a;
59+
var _a, _b;
5960
if (!((_a = declaration.actions) === null || _a === void 0 ? void 0 : _a.includes('create')))
6061
return;
61-
if (!declaration.create || 'type' in declaration.create) {
62-
declaration.create = {};
63-
}
6462
// If the create is resolved by a custom function, we don't need to inject hooks
65-
if (declaration.list.create) {
63+
if ((_b = declaration === null || declaration === void 0 ? void 0 : declaration.create) === null || _b === void 0 ? void 0 : _b.resolve) {
6664
return;
6765
}
66+
if (!declaration.create) {
67+
declaration.create = {};
68+
}
6869
var beforeCreate = Array.isArray(declaration.create.before)
6970
? declaration.create.before
7071
: declaration.create.before
@@ -83,16 +84,16 @@ function injectCreateHooks(declaration, injectFunctions) {
8384
: afterCreate;
8485
}
8586
function injectDeleteHooks(declaration, injectFunctions) {
86-
var _a;
87+
var _a, _b;
8788
if (!((_a = declaration.actions) === null || _a === void 0 ? void 0 : _a.includes('delete')))
8889
return;
89-
if (!declaration["delete"] || 'type' in declaration["delete"]) {
90-
declaration["delete"] = {};
91-
}
9290
// If the delete is resolved by a custom function, we don't need to inject hooks
93-
if (declaration["delete"].resolve) {
91+
if ((_b = declaration === null || declaration === void 0 ? void 0 : declaration["delete"]) === null || _b === void 0 ? void 0 : _b.resolve) {
9492
return;
9593
}
94+
if (!declaration["delete"]) {
95+
declaration["delete"] = {};
96+
}
9697
var beforeDelete = Array.isArray(declaration["delete"].before)
9798
? declaration["delete"].before
9899
: declaration["delete"].before

src/injectHooks.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function injectListHooks<T extends Model<any, any>>(
2424
}
2525

2626
// If the list is resolved by a custom function, we don't need to inject hooks
27-
if (declaration.list.resolve) {
27+
if (declaration?.list?.resolve) {
2828
return
2929
}
3030

@@ -57,15 +57,15 @@ function injectUpdateHooks<T extends Model<any, any>>(
5757
) {
5858
if (!declaration.actions?.includes('update')) return
5959

60-
if (!declaration.update || 'type' in declaration.update) {
61-
declaration.update = {} as UpdateFieldDeclarationType<T>
62-
}
63-
6460
// If the update is resolved by a custom function, we don't need to inject hooks
65-
if (declaration.update.resolve) {
61+
if (declaration?.update?.resolve) {
6662
return
6763
}
6864

65+
if (!declaration.update) {
66+
declaration.update = {} as UpdateFieldDeclarationType<T>
67+
}
68+
6969
const beforeUpdate: UpdateBeforeHook<T>[] = Array.isArray(
7070
declaration.update.before
7171
)
@@ -97,15 +97,15 @@ function injectCreateHooks<T extends Model<any, any>>(
9797
) {
9898
if (!declaration.actions?.includes('create')) return
9999

100-
if (!declaration.create || 'type' in declaration.create) {
101-
declaration.create = {} as CreateFieldDeclarationType<T>
102-
}
103-
104100
// If the create is resolved by a custom function, we don't need to inject hooks
105-
if (declaration.list.create) {
101+
if (declaration?.create?.resolve) {
106102
return
107103
}
108104

105+
if (!declaration.create) {
106+
declaration.create = {} as CreateFieldDeclarationType<T>
107+
}
108+
109109
const beforeCreate: CreateBeforeHook<T>[] = Array.isArray(
110110
declaration.create.before
111111
)
@@ -137,15 +137,15 @@ function injectDeleteHooks<T extends Model<any, any>>(
137137
) {
138138
if (!declaration.actions?.includes('delete')) return
139139

140-
if (!declaration.delete || 'type' in declaration.delete) {
141-
declaration.delete = {} as DeleteFieldDeclarationType<T>
142-
}
143-
144140
// If the delete is resolved by a custom function, we don't need to inject hooks
145-
if (declaration.delete.resolve) {
141+
if (declaration?.delete?.resolve) {
146142
return
147143
}
148144

145+
if (!declaration.delete) {
146+
declaration.delete = {} as DeleteFieldDeclarationType<T>
147+
}
148+
149149
const beforeDelete: DeleteBeforeHook<T>[] = Array.isArray(
150150
declaration.delete.before
151151
)

src/tests/injectHooks.test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const { injectHooks } = require('../../lib')
2+
const { GraphQLError } = require('graphql')
3+
4+
describe('injectHooks', () => {
5+
it('should not overwrite custom update hooks', () => {
6+
// Create a custom update hook that throws an error
7+
const customUpdateHook = () => {
8+
throw new GraphQLError('Custom update hook should be called')
9+
}
10+
11+
// Create a schema declaration with custom update hooks
12+
const schemaDeclaration = {
13+
user: {
14+
model: 'User',
15+
actions: ['update'],
16+
update: {
17+
before: [customUpdateHook],
18+
after: [customUpdateHook],
19+
},
20+
},
21+
}
22+
23+
// Create inject functions that would normally add hooks
24+
const injectFunctions = {
25+
updateBefore: (model, existingHooks) => {
26+
return [...existingHooks, () => {}]
27+
},
28+
updateAfter: (model, existingHooks) => {
29+
return [...existingHooks, () => {}]
30+
},
31+
}
32+
33+
// Inject hooks
34+
const result = injectHooks({
35+
graphqlSchemaDeclaration: schemaDeclaration,
36+
injectFunctions,
37+
})
38+
39+
// Verify that the custom hooks are still present and in the correct order
40+
expect(result.user.update.before).toContain(customUpdateHook)
41+
expect(result.user.update.after).toContain(customUpdateHook)
42+
expect(result.user.update.before[0]).toBe(customUpdateHook)
43+
expect(result.user.update.after[0]).toBe(customUpdateHook)
44+
})
45+
46+
it('should not inject hooks when custom resolve function is provided', () => {
47+
const customResolve = () => {
48+
return 'custom resolve result'
49+
}
50+
51+
const schemaDeclaration = {
52+
user: {
53+
model: 'User',
54+
actions: ['update'],
55+
update: {
56+
resolve: customResolve,
57+
},
58+
},
59+
}
60+
61+
const injectFunctions = {
62+
updateBefore: (model, existingHooks) => {
63+
return [...existingHooks, () => {}]
64+
},
65+
updateAfter: (model, existingHooks) => {
66+
return [...existingHooks, () => {}]
67+
},
68+
}
69+
70+
const result = injectHooks({
71+
graphqlSchemaDeclaration: schemaDeclaration,
72+
injectFunctions,
73+
})
74+
75+
// Verify that no hooks were injected
76+
expect(result.user.update.before).toBeUndefined()
77+
expect(result.user.update.after).toBeUndefined()
78+
expect(result.user.update.resolve).toBe(customResolve)
79+
})
80+
})

0 commit comments

Comments
 (0)