Skip to content

Commit bae8017

Browse files
feat(hooks): Change the signature of all the hooks to take an object instead of an array.
BREAKING CHANGE: Change the signature of all the hooks to take an object instead of an array.
1 parent b15061b commit bae8017

File tree

15 files changed

+348
-231
lines changed

15 files changed

+348
-231
lines changed

lib/createListResolver.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ var argsAdvancedProcessing = function (findOptions, args, context, info, model,
166166
if (info.parentType &&
167167
models[info.parentType.name] &&
168168
models[info.parentType.name].associations[info.fieldName].scope) {
169-
findOptions.where = __assign(__assign({}, (findOptions.where ? findOptions.where : {})), models[info.parentType.name].associations[info.fieldName].scope);
169+
findOptions.where = __assign(__assign({}, (findOptions.where ? findOptions.where : {})), models[info.parentType.name].associations[info.fieldName]
170+
.scope);
170171
}
171172
return findOptions;
172173
};
@@ -352,7 +353,12 @@ function createListResolver(graphqlTypeDeclaration, models, globalPreCallback, r
352353
case 8:
353354
if (!listBefore) return [3 /*break*/, 10];
354355
handle = globalPreCallback('listBefore');
355-
return [4 /*yield*/, listBefore(processedFindOptions, args, context, info)];
356+
return [4 /*yield*/, listBefore({
357+
findOptions: processedFindOptions,
358+
args: args,
359+
context: context,
360+
info: info
361+
})];
356362
case 9:
357363
resultBefore = _b.sent();
358364
if (!resultBefore) {
@@ -381,7 +387,12 @@ function createListResolver(graphqlTypeDeclaration, models, globalPreCallback, r
381387
case 0:
382388
if (!listAfter) return [3 /*break*/, 2];
383389
handle = globalPreCallback('listAfter');
384-
return [4 /*yield*/, listAfter(result, args, context, info)];
390+
return [4 /*yield*/, listAfter({
391+
result: result,
392+
args: args,
393+
context: context,
394+
info: info
395+
})];
385396
case 1:
386397
modifiedResult = _a.sent();
387398
if (handle) {

lib/mutationResolvers/create.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ function generateMutationCreate(modelName, inputType, outputType, model, graphql
132132
'before' in graphqlModelDeclaration.create &&
133133
graphqlModelDeclaration.create.before)) return [3 /*break*/, 10];
134134
beforeHandle = globalPreCallback('createBefore');
135-
return [4 /*yield*/, graphqlModelDeclaration.create.before(source, args, context, info)];
135+
return [4 /*yield*/, graphqlModelDeclaration.create.before({
136+
source: source,
137+
args: args,
138+
context: context,
139+
info: info
140+
})];
136141
case 9:
137142
attributes = _d.sent();
138143
if (!attributes) {
@@ -188,7 +193,14 @@ function generateMutationCreate(modelName, inputType, outputType, model, graphql
188193
graphqlModelDeclaration.create.after)) return [3 /*break*/, 20];
189194
afterHandle = globalPreCallback('createAfter');
190195
hookData = { data: newEntity.get({ plain: true }) };
191-
return [4 /*yield*/, graphqlModelDeclaration.create.after(newEntity, source, args, context, info, (0, setWebhookData_1["default"])(hookData))];
196+
return [4 /*yield*/, graphqlModelDeclaration.create.after({
197+
newEntity: newEntity,
198+
source: source,
199+
args: args,
200+
context: context,
201+
info: info,
202+
setWebhookData: (0, setWebhookData_1["default"])(hookData)
203+
})];
192204
case 18:
193205
updatedEntity = _d.sent();
194206
if (afterHandle) {

lib/mutationResolvers/delete.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6262
};
6363
exports.__esModule = true;
6464
var graphql_1 = require("graphql");
65-
var setWebhookData_1 = __importDefault(require("../webhook/setWebhookData"));
6665
var callModelWebhook_1 = __importDefault(require("./callModelWebhook"));
6766
/**
6867
* Generates a delete mutation operation
@@ -131,7 +130,13 @@ function generateMutationDelete(modelName, graphqlModelDeclaration, models, glob
131130
'before' in graphqlModelDeclaration["delete"] &&
132131
graphqlModelDeclaration["delete"].before)) return [3 /*break*/, 10];
133132
beforeHandle = globalPreCallback('deleteBefore');
134-
return [4 /*yield*/, graphqlModelDeclaration["delete"].before(where, source, args, context, info)];
133+
return [4 /*yield*/, graphqlModelDeclaration["delete"].before({
134+
where: where,
135+
source: source,
136+
args: args,
137+
context: context,
138+
info: info
139+
})];
135140
case 9:
136141
where = _c.sent();
137142
if (beforeHandle) {
@@ -161,7 +166,13 @@ function generateMutationDelete(modelName, graphqlModelDeclaration, models, glob
161166
graphqlModelDeclaration["delete"].after)) return [3 /*break*/, 15];
162167
hookData = { data: __assign({}, snapshotBeforeDelete) };
163168
afterHandle = globalPreCallback('deleteAfter');
164-
return [4 /*yield*/, graphqlModelDeclaration["delete"].after(entity, source, args, context, info, (0, setWebhookData_1["default"])(hookData))];
169+
return [4 /*yield*/, graphqlModelDeclaration["delete"].after({
170+
deletedEntity: entity,
171+
source: source,
172+
args: args,
173+
context: context,
174+
info: info
175+
})];
165176
case 13:
166177
_c.sent();
167178
if (afterHandle) {

lib/mutationResolvers/update.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6262
};
6363
exports.__esModule = true;
6464
var graphql_1 = require("graphql");
65-
var setWebhookData_1 = __importDefault(require("../webhook/setWebhookData"));
6665
var callModelWebhook_1 = __importDefault(require("./callModelWebhook"));
6766
/**
6867
* Generates a update mutation operation
@@ -133,7 +132,12 @@ function generateMutationUpdate(modelName, inputType, outputType, graphqlModelDe
133132
'before' in graphqlModelDeclaration.update &&
134133
graphqlModelDeclaration.update.before)) return [3 /*break*/, 10];
135134
beforeHandle = globalPreCallback('updateBefore');
136-
return [4 /*yield*/, graphqlModelDeclaration.update.before(source, args, context, info)];
135+
return [4 /*yield*/, graphqlModelDeclaration.update.before({
136+
source: source,
137+
args: args,
138+
context: context,
139+
info: info
140+
})];
137141
case 9:
138142
data = _c.sent();
139143
if (beforeHandle) {
@@ -163,7 +167,14 @@ function generateMutationUpdate(modelName, inputType, outputType, graphqlModelDe
163167
}
164168
};
165169
afterHandle = globalPreCallback('updateAfter');
166-
return [4 /*yield*/, graphqlModelDeclaration.update.after(entity, snapshotBeforeUpdate, source, args, context, info, (0, setWebhookData_1["default"])(hookData))];
170+
return [4 /*yield*/, graphqlModelDeclaration.update.after({
171+
updatedEntity: entity,
172+
entitySnapshot: snapshotBeforeUpdate,
173+
source: source,
174+
args: args,
175+
context: context,
176+
info: info
177+
})];
167178
case 14:
168179
updatedEntity = _c.sent();
169180
if (afterHandle) {

lib/queryResolvers/count.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function countResolver(model, schemaDeclaration, globalPreCallback) {
6464
? schemaDeclaration.count.before
6565
: listBefore;
6666
return function (source, args, context, info) { return __awaiter(_this, void 0, void 0, function () {
67-
var beforeList, beforeList_1, beforeList_1_1, before, handle, e_1_1, handle, countOptions;
67+
var beforeList, beforeList_1, beforeList_1_1, before, handle, e_1_1, findOptions, handle, resultBefore;
6868
var e_1, _a;
6969
return __generator(this, function (_b) {
7070
switch (_b.label) {
@@ -105,16 +105,26 @@ function countResolver(model, schemaDeclaration, globalPreCallback) {
105105
finally { if (e_1) throw e_1.error; }
106106
return [7 /*endfinally*/];
107107
case 8:
108-
if (!(typeof countBefore !== 'undefined')) return [3 /*break*/, 10];
108+
findOptions = graphql_sequelize_1.argsToFindOptions["default"](args, Object.keys(model.getAttributes()));
109+
if (!countBefore) return [3 /*break*/, 10];
109110
handle = globalPreCallback('countBefore');
110-
return [4 /*yield*/, countBefore(graphql_sequelize_1.argsToFindOptions["default"](args, Object.keys(model.getAttributes())), args, context, info)];
111+
return [4 /*yield*/, countBefore({
112+
findOptions: findOptions,
113+
args: args,
114+
context: context,
115+
info: info
116+
})];
111117
case 9:
112-
countOptions = _b.sent();
118+
resultBefore = _b.sent();
119+
if (!resultBefore) {
120+
throw new Error('The before hook of the count endpoint must return a value.');
121+
}
122+
findOptions = resultBefore;
113123
if (handle) {
114124
handle();
115125
}
116-
return [2 /*return*/, model.count(countOptions)];
117-
case 10: return [2 /*return*/, model.count(graphql_sequelize_1.argsToFindOptions["default"](args, Object.keys(model.getAttributes())))];
126+
return [2 /*return*/, model.count(findOptions)];
127+
case 10: return [2 /*return*/, model.count(findOptions)];
118128
}
119129
});
120130
}); };

src/createListResolver.ts

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import { resolver } from 'graphql-sequelize'
2-
import { FindOptions, Model, Op } from 'sequelize'
2+
import { FindOptions, Model, ModelStatic, Op } from 'sequelize'
33
import removeUnusedAttributes from './removeUnusedAttributes'
4-
import { GlobalBeforeHook, ModelDeclarationType } from './types/types'
4+
import {
5+
GlobalBeforeHook,
6+
ModelDeclarationType,
7+
SequelizeModels,
8+
} from './types/types'
59

6-
function allowOrderOnAssociations(findOptions: FindOptions<any>, model: any) {
10+
interface ModelInclude {
11+
model: ModelStatic<Model<any>>
12+
as?: string
13+
}
14+
15+
interface ModelSort {
16+
model: ModelStatic<Model<any>>
17+
as?: string
18+
}
19+
20+
function allowOrderOnAssociations<M extends Model<any>>(
21+
findOptions: FindOptions<M>,
22+
model: ModelStatic<M>
23+
) {
724
if (typeof findOptions.order === 'undefined') {
825
return findOptions
926
}
10-
const processedOrder: any = []
27+
const processedOrder: any[] = []
1128

1229
const checkForAssociationSort = (singleOrder: any, index: any) => {
1330
// When the comas is used, graphql-sequelize will not handle the 'reverse:' command.
@@ -43,7 +60,7 @@ function allowOrderOnAssociations(findOptions: FindOptions<any>, model: any) {
4360
findOptions.include = []
4461
}
4562

46-
const modelInclude: any = {
63+
const modelInclude: ModelInclude = {
4764
model: model.associations[associationName].target,
4865
}
4966

@@ -56,7 +73,7 @@ function allowOrderOnAssociations(findOptions: FindOptions<any>, model: any) {
5673
findOptions.include.push(modelInclude)
5774
}
5875

59-
const modelSort: any = {
76+
const modelSort: ModelSort = {
6077
model: model.associations[associationName].target,
6178
}
6279
// When sorting by a associated table, the alias must be specified
@@ -72,12 +89,12 @@ function allowOrderOnAssociations(findOptions: FindOptions<any>, model: any) {
7289
if (
7390
field &&
7491
model.rawAttributes[field] &&
75-
model.rawAttributes[field].type.key === 'VIRTUAL'
92+
(model.rawAttributes[field].type as any).key === 'VIRTUAL'
7693
) {
7794
// When a virtual field is used, we must sort with the expression and not
7895
// the name of the field, as it is not compatible with multiple database engines.
7996
// IE : Sorting by virtual field is inefficient if using sub-queries.
80-
field = model.rawAttributes[field].type.fields[0][0]
97+
field = (model.rawAttributes[field].type as any).fields[0][0]
8198
}
8299
processedOrder.push([field, direction])
83100
}
@@ -113,42 +130,43 @@ function allowOrderOnAssociations(findOptions: FindOptions<any>, model: any) {
113130
return findOptions
114131
}
115132

116-
const argsAdvancedProcessing = (
117-
findOptions: FindOptions<any>,
133+
const argsAdvancedProcessing = <M extends Model<any>>(
134+
findOptions: FindOptions<M>,
118135
args: any,
119136
context: any,
120137
info: any,
121-
model: any,
122-
models: any
138+
model: ModelStatic<M>,
139+
models: SequelizeModels
123140
) => {
124141
findOptions = allowOrderOnAssociations(findOptions, model)
125142

126143
// When an association uses a scope, we have to add it to the where condition by default.
127144
if (
128145
info.parentType &&
129146
models[info.parentType.name] &&
130-
models[info.parentType.name].associations[info.fieldName].scope
147+
(models[info.parentType.name].associations[info.fieldName] as any).scope
131148
) {
132149
findOptions.where = {
133150
...(findOptions.where ? findOptions.where : {}),
134-
...models[info.parentType.name].associations[info.fieldName].scope,
151+
...(models[info.parentType.name].associations[info.fieldName] as any)
152+
.scope,
135153
}
136154
}
137155

138156
return findOptions
139157
}
140158

141-
async function trimAndOptimizeFindOptions({
159+
async function trimAndOptimizeFindOptions<M extends Model<any>>({
142160
findOptions,
143161
graphqlTypeDeclaration,
144162
info,
145163
models,
146164
args,
147165
}: {
148-
findOptions: FindOptions<any>
149-
graphqlTypeDeclaration: any
166+
findOptions: FindOptions<M>
167+
graphqlTypeDeclaration: ModelDeclarationType<M>
150168
info: any
151-
models: any
169+
models: SequelizeModels
152170
args: any
153171
}) {
154172
const trimedFindOptions =
@@ -213,7 +231,7 @@ async function trimAndOptimizeFindOptions({
213231
...trimedFindOptions,
214232
// We only fetch the primary attribute
215233
attributes: graphqlTypeDeclaration.model.primaryKeyAttributes,
216-
}
234+
} as FindOptions<M>
217235
const result = await graphqlTypeDeclaration.model.findAll(
218236
fetchIdsMultiColumnsFindOptions
219237
)
@@ -268,11 +286,11 @@ async function trimAndOptimizeFindOptions({
268286
return trimedFindOptions
269287
}
270288

271-
export default function createListResolver(
272-
graphqlTypeDeclaration: ModelDeclarationType<any>,
273-
models: any,
289+
export default function createListResolver<M extends Model<any>>(
290+
graphqlTypeDeclaration: ModelDeclarationType<M>,
291+
models: SequelizeModels,
274292
globalPreCallback: any,
275-
relation = null
293+
relation: ModelStatic<M> | null = null
276294
) {
277295
if (graphqlTypeDeclaration?.list?.resolver) {
278296
return async (source: any, args: any, context: any, info: any) => {
@@ -305,8 +323,13 @@ export default function createListResolver(
305323
contextToOptions: graphqlTypeDeclaration.list
306324
? graphqlTypeDeclaration.list.contextToOptions
307325
: undefined,
308-
before: async (findOptions: any, args: any, context: any, info: any) => {
309-
let processedFindOptions: FindOptions<any> = argsAdvancedProcessing(
326+
before: async (
327+
findOptions: FindOptions<M>,
328+
args: any,
329+
context: any,
330+
info: any
331+
) => {
332+
let processedFindOptions: FindOptions<M> = argsAdvancedProcessing(
310333
findOptions,
311334
args,
312335
context,
@@ -355,12 +378,12 @@ export default function createListResolver(
355378
// before hook, can mutate the findOptions
356379
if (listBefore) {
357380
const handle = globalPreCallback('listBefore')
358-
const resultBefore = await listBefore(
359-
processedFindOptions,
381+
const resultBefore = await listBefore({
382+
findOptions: processedFindOptions,
360383
args,
361384
context,
362-
info
363-
)
385+
info,
386+
})
364387
if (!resultBefore) {
365388
throw new Error(
366389
'The before hook of the list endpoint must return a value.'
@@ -383,15 +406,15 @@ export default function createListResolver(
383406
args,
384407
})
385408
},
386-
after: async (
387-
result: Model<any> | Model<any>[],
388-
args: any,
389-
context: any,
390-
info: any
391-
) => {
409+
after: async (result: M | M[], args: any, context: any, info: any) => {
392410
if (listAfter) {
393411
const handle = globalPreCallback('listAfter')
394-
const modifiedResult = await listAfter(result, args, context, info)
412+
const modifiedResult = await listAfter({
413+
result,
414+
args,
415+
context,
416+
info,
417+
})
395418
if (handle) {
396419
handle()
397420
}

0 commit comments

Comments
 (0)