11import { resolver } from 'graphql-sequelize'
2- import { FindOptions , Model , Op } from 'sequelize'
2+ import { FindOptions , Model , ModelStatic , Op } from 'sequelize'
33import 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