@@ -163,7 +163,51 @@ var argsAdvancedProcessing = function (findOptions, args, context, info, model,
163
163
}
164
164
return findOptions ;
165
165
} ;
166
- function createResolver ( graphqlTypeDeclaration , models , globalPreCallback , relation ) {
166
+ function trimAndOptimizeFindOptions ( _a ) {
167
+ var findOptions = _a . findOptions , graphqlTypeDeclaration = _a . graphqlTypeDeclaration , info = _a . info , models = _a . models ;
168
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
169
+ var trimedFindOptions , fetchIdsFindOptions , result ;
170
+ var _b ;
171
+ return __generator ( this , function ( _c ) {
172
+ switch ( _c . label ) {
173
+ case 0 :
174
+ trimedFindOptions = graphqlTypeDeclaration . list &&
175
+ graphqlTypeDeclaration . list . removeUnusedAttributes === false
176
+ ? findOptions
177
+ : ( 0 , removeUnusedAttributes_1 [ "default" ] ) ( findOptions , info , graphqlTypeDeclaration . model , models ) ;
178
+ if ( !
179
+ // If we have a list with a limit and an offset
180
+ ( trimedFindOptions . limit &&
181
+ trimedFindOptions . offset &&
182
+ // And no explicit instructions to not optimize it.
183
+ // In the majority of the case, doubling the number of queries should be either
184
+ // faster OR not significantly slower.
185
+ // As GSG is made to be "easy-to-use", we optimize by default.
186
+ // We expect limit to be small enough to not cause performance issues.
187
+ // If you are in a case where you need to fetch a big offset, you should disable the optimization.
188
+ ( ! graphqlTypeDeclaration . list ||
189
+ typeof graphqlTypeDeclaration . list . disableOptimizationForLimitOffset ===
190
+ 'undefined' ||
191
+ graphqlTypeDeclaration . list . disableOptimizationForLimitOffset !== true ) ) )
192
+ // If we have a list with a limit and an offset
193
+ return [ 3 /*break*/ , 2 ] ;
194
+ fetchIdsFindOptions = __assign ( __assign ( { } , trimedFindOptions ) , {
195
+ // We only fetch the primary attribute
196
+ attributes : [ graphqlTypeDeclaration . model . primaryKeyAttribute ] } ) ;
197
+ return [ 4 /*yield*/ , graphqlTypeDeclaration . model . findAll ( fetchIdsFindOptions ) ] ;
198
+ case 1 :
199
+ result = _c . sent ( ) ;
200
+ return [ 2 /*return*/ , __assign ( __assign ( { } , trimedFindOptions ) , { offset : undefined , limit : undefined ,
201
+ // We override the where to only fetch the rows we want.
202
+ where : ( _b = { } ,
203
+ _b [ graphqlTypeDeclaration . model . primaryKeyAttribute ] = result . map ( function ( r ) { return r [ graphqlTypeDeclaration . model . primaryKeyAttribute ] ; } ) ,
204
+ _b ) } ) ] ;
205
+ case 2 : return [ 2 /*return*/ , trimedFindOptions ] ;
206
+ }
207
+ } ) ;
208
+ } ) ;
209
+ }
210
+ function createListResolver ( graphqlTypeDeclaration , models , globalPreCallback , relation ) {
167
211
var _this = this ;
168
212
var _a ;
169
213
if ( relation === void 0 ) { relation = null ; }
@@ -199,7 +243,7 @@ function createResolver(graphqlTypeDeclaration, models, globalPreCallback, relat
199
243
? graphqlTypeDeclaration . list . contextToOptions
200
244
: undefined ,
201
245
before : function ( findOptions , args , context , info ) { return __awaiter ( _this , void 0 , void 0 , function ( ) {
202
- var processedFindOptions , beforeList , beforeList_1 , beforeList_1_1 , before , handle , e_1_1 , handle , result ;
246
+ var processedFindOptions , beforeList , beforeList_1 , beforeList_1_1 , before , handle , e_1_1 , handle , resultBefore ;
203
247
var e_1 , _a ;
204
248
return __generator ( this , function ( _b ) {
205
249
switch ( _b . label ) {
@@ -261,18 +305,22 @@ function createResolver(graphqlTypeDeclaration, models, globalPreCallback, relat
261
305
handle = globalPreCallback ( 'listBefore' ) ;
262
306
return [ 4 /*yield*/ , listBefore ( processedFindOptions , args , context , info ) ] ;
263
307
case 9 :
264
- result = _b . sent ( ) ;
308
+ resultBefore = _b . sent ( ) ;
309
+ if ( ! resultBefore ) {
310
+ throw new Error ( 'The before hook of the list endpoint must return a value.' ) ;
311
+ }
312
+ // The list overwrite the findOptions
313
+ processedFindOptions = resultBefore ;
265
314
if ( handle ) {
266
315
handle ( ) ;
267
316
}
268
- return [ 2 /*return*/ , graphqlTypeDeclaration . list &&
269
- graphqlTypeDeclaration . list . removeUnusedAttributes === false
270
- ? result
271
- : ( 0 , removeUnusedAttributes_1 [ "default" ] ) ( result , info , graphqlTypeDeclaration . model , models ) ] ;
272
- case 10 : return [ 2 /*return*/ , graphqlTypeDeclaration . list &&
273
- graphqlTypeDeclaration . list . removeUnusedAttributes === false
274
- ? processedFindOptions
275
- : ( 0 , removeUnusedAttributes_1 [ "default" ] ) ( processedFindOptions , info , graphqlTypeDeclaration . model , models ) ] ;
317
+ _b . label = 10 ;
318
+ case 10 : return [ 2 /*return*/ , trimAndOptimizeFindOptions ( {
319
+ findOptions : processedFindOptions ,
320
+ graphqlTypeDeclaration : graphqlTypeDeclaration ,
321
+ info : info ,
322
+ models : models
323
+ } ) ] ;
276
324
}
277
325
} ) ;
278
326
} ) ; } ,
@@ -296,4 +344,4 @@ function createResolver(graphqlTypeDeclaration, models, globalPreCallback, relat
296
344
} ) ; }
297
345
} ) ;
298
346
}
299
- exports [ "default" ] = createResolver ;
347
+ exports [ "default" ] = createListResolver ;
0 commit comments