Skip to content

Commit df10069

Browse files
fix(count): Fix where and includes not initialized in the count hook like the list one.
1 parent dc96c26 commit df10069

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

lib/queryResolvers/count.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ function countResolver(model, schemaDeclaration, globalPreCallback) {
106106
return [7 /*endfinally*/];
107107
case 8:
108108
findOptions = graphql_sequelize_1.argsToFindOptions["default"](args, Object.keys(model.getAttributes()));
109+
if (!findOptions.where) {
110+
findOptions.where = {};
111+
}
112+
if (typeof findOptions.include === 'undefined') {
113+
findOptions.include = [];
114+
}
109115
if (!countBefore) return [3 /*break*/, 10];
110116
handle = globalPreCallback('countBefore');
111117
return [4 /*yield*/, countBefore({

src/queryResolvers/count.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ModelDeclarationType,
77
} from '../types/types'
88

9-
export default function countResolver<M extends Model<any>>(
9+
export default function countResolver<M extends Model<any>, TContext = any>(
1010
model: ModelStatic<M>,
1111
schemaDeclaration: ModelDeclarationType<M>,
1212
globalPreCallback: GlobalPreCallback
@@ -31,7 +31,7 @@ export default function countResolver<M extends Model<any>>(
3131
? schemaDeclaration.count.before
3232
: listBefore
3333

34-
return async (source: any, args: any, context: any, info: any) => {
34+
return async (source: any, args: any, context: TContext, info: any) => {
3535
if (schemaDeclaration.before) {
3636
const beforeList: GlobalBeforeHook[] =
3737
schemaDeclaration.before &&
@@ -53,6 +53,14 @@ export default function countResolver<M extends Model<any>>(
5353
Object.keys(model.getAttributes())
5454
)
5555

56+
if (!findOptions.where) {
57+
findOptions.where = {}
58+
}
59+
60+
if (typeof findOptions.include === 'undefined') {
61+
findOptions.include = []
62+
}
63+
5664
if (countBefore) {
5765
const handle = globalPreCallback('countBefore')
5866

src/queryResolvers/createListResolver.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,17 @@ async function trimAndOptimizeFindOptions<M extends Model<any>>({
288288
return trimedFindOptions
289289
}
290290

291-
export default function createListResolver<M extends Model<any>>(
292-
graphqlTypeDeclaration: ModelDeclarationType<M>,
291+
export default function createListResolver<
292+
M extends Model<any>,
293+
TContext = any
294+
>(
295+
graphqlTypeDeclaration: ModelDeclarationType<M, TContext>,
293296
models: SequelizeModels,
294297
globalPreCallback: any,
295298
relation: ModelStatic<M> | null = null
296299
) {
297300
if (graphqlTypeDeclaration?.list?.resolver) {
298-
return async (source: any, args: any, context: any, info: any) => {
301+
return async (source: any, args: any, context: TContext, info: any) => {
299302
const customResolverHandle = globalPreCallback('customListBefore')
300303
if (graphqlTypeDeclaration?.list?.resolver) {
301304
const customResult = await graphqlTypeDeclaration.list.resolver(
@@ -328,7 +331,7 @@ export default function createListResolver<M extends Model<any>>(
328331
before: async (
329332
findOptions: FindOptions<M>,
330333
args: any,
331-
context: any,
334+
context: TContext,
332335
info: any
333336
) => {
334337
if (!findOptions.where) {
@@ -370,7 +373,7 @@ export default function createListResolver<M extends Model<any>>(
370373

371374
// Global hooks, cannot impact the findOptions
372375
if (graphqlTypeDeclaration.before) {
373-
const beforeList: GlobalBeforeHook[] =
376+
const beforeList: GlobalBeforeHook<TContext>[] =
374377
typeof graphqlTypeDeclaration.before.length !== 'undefined'
375378
? (graphqlTypeDeclaration.before as GlobalBeforeHook[])
376379
: ([
@@ -418,7 +421,7 @@ export default function createListResolver<M extends Model<any>>(
418421
args,
419422
})
420423
},
421-
after: async (result: M | M[], args: any, context: any, info: any) => {
424+
after: async (result: M | M[], args: any, context: TContext, info: any) => {
422425
if (listAfter) {
423426
const handle = globalPreCallback('listAfter')
424427
const modifiedResult = await listAfter({

src/root/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function getModelsFields(
6767
? schemaDeclaration.list.extraArg
6868
: {}),
6969
},
70-
resolve: generateCountResolver(
70+
resolve: generateCountResolver<any, any>(
7171
schemaDeclaration.model,
7272
schemaDeclaration,
7373
globalPreCallback

0 commit comments

Comments
 (0)