3
3
*/
4
4
5
5
import {
6
- GraphQLError ,
7
6
parse ,
8
7
TypeInfo ,
9
- ValidationContext ,
10
8
visit ,
11
9
visitWithTypeInfo ,
12
10
} from 'graphql' ;
@@ -21,6 +19,7 @@ import {
21
19
directiveEstimator ,
22
20
fieldExtensionsEstimator ,
23
21
} from '../index' ;
22
+ import { CompatibleValidationContext } from './fixtures/CompatibleValidationContext' ;
24
23
25
24
describe ( 'QueryComplexity analysis' , ( ) => {
26
25
const typeInfo = new TypeInfo ( schema ) ;
@@ -155,7 +154,7 @@ describe('QueryComplexity analysis', () => {
155
154
}
156
155
` ) ;
157
156
158
- const context = new ValidationContext ( schema , ast , typeInfo , ( ) => null ) ;
157
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
159
158
const visitor = new ComplexityVisitor ( context , {
160
159
maximumComplexity : 100 ,
161
160
estimators : [
@@ -174,8 +173,7 @@ describe('QueryComplexity analysis', () => {
174
173
}
175
174
` ) ;
176
175
177
- const validationErrors : GraphQLError [ ] = [ ] ;
178
- const context = new ValidationContext ( schema , ast , typeInfo , err => validationErrors . push ( err ) ) ;
176
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
179
177
const visitor = new ComplexityVisitor ( context , {
180
178
maximumComplexity : 100 ,
181
179
estimators : [
@@ -188,8 +186,8 @@ describe('QueryComplexity analysis', () => {
188
186
189
187
visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
190
188
expect ( visitor . complexity ) . to . equal ( 1000 ) ;
191
- expect ( validationErrors . length ) . to . equal ( 1 ) ;
192
- expect ( validationErrors [ 0 ] . message ) . to . equal (
189
+ expect ( context . getErrors ( ) . length ) . to . equal ( 1 ) ;
190
+ expect ( context . getErrors ( ) [ 0 ] . message ) . to . equal (
193
191
'The query exceeds the maximum complexity of 100. Actual complexity is 1000'
194
192
) ;
195
193
} ) ;
@@ -205,7 +203,7 @@ describe('QueryComplexity analysis', () => {
205
203
}
206
204
` ) ;
207
205
208
- const context = new ValidationContext ( schema , ast , typeInfo , ( ) => null ) ;
206
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
209
207
const visitor = new ComplexityVisitor ( context , {
210
208
maximumComplexity : 100 ,
211
209
estimators : [
@@ -232,7 +230,7 @@ describe('QueryComplexity analysis', () => {
232
230
}
233
231
` ) ;
234
232
235
- const context = new ValidationContext ( schema , ast , typeInfo , ( ) => null ) ;
233
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
236
234
const visitor = new ComplexityVisitor ( context , {
237
235
maximumComplexity : 100 ,
238
236
estimators : [
@@ -259,7 +257,7 @@ describe('QueryComplexity analysis', () => {
259
257
}
260
258
` ) ;
261
259
262
- const context = new ValidationContext ( schema , ast , typeInfo , ( ) => null ) ;
260
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
263
261
const visitor = new ComplexityVisitor ( context , {
264
262
maximumComplexity : 100 ,
265
263
estimators : [
@@ -286,7 +284,7 @@ describe('QueryComplexity analysis', () => {
286
284
}
287
285
` ) ;
288
286
289
- const context = new ValidationContext ( schema , ast , typeInfo , ( ) => null ) ;
287
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
290
288
const visitor = new ComplexityVisitor ( context , {
291
289
maximumComplexity : 100 ,
292
290
estimators : [
@@ -312,7 +310,7 @@ describe('QueryComplexity analysis', () => {
312
310
}
313
311
` ) ;
314
312
315
- const context = new ValidationContext ( schema , ast , typeInfo , ( ) => null ) ;
313
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
316
314
const visitor = new ComplexityVisitor ( context , {
317
315
maximumComplexity : 100 ,
318
316
estimators : [
@@ -334,7 +332,7 @@ describe('QueryComplexity analysis', () => {
334
332
}
335
333
` ) ;
336
334
337
- const context = new ValidationContext ( schema , ast , typeInfo , ( ) => null ) ;
335
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
338
336
const visitor = new ComplexityVisitor ( context , {
339
337
maximumComplexity : 100 ,
340
338
estimators : [
@@ -355,8 +353,7 @@ describe('QueryComplexity analysis', () => {
355
353
requiredArgs
356
354
}
357
355
` ) ;
358
- const validationErrors : GraphQLError [ ] = [ ] ;
359
- const context = new ValidationContext ( schema , ast , typeInfo , err => validationErrors . push ( err ) ) ;
356
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
360
357
const visitor = new ComplexityVisitor ( context , {
361
358
maximumComplexity : 100 ,
362
359
estimators : [
@@ -367,8 +364,8 @@ describe('QueryComplexity analysis', () => {
367
364
]
368
365
} ) ;
369
366
visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
370
- expect ( validationErrors . length ) . to . equal ( 1 ) ;
371
- expect ( validationErrors [ 0 ] . message ) . to . equal ( 'Argument "count" of required type "Int!" was not provided.' ) ;
367
+ expect ( context . getErrors ( ) . length ) . to . equal ( 1 ) ;
368
+ expect ( context . getErrors ( ) [ 0 ] . message ) . to . equal ( 'Argument "count" of required type "Int!" was not provided.' ) ;
372
369
} ) ;
373
370
374
371
it ( 'should report error when no estimator is configured' , ( ) => {
@@ -377,15 +374,14 @@ describe('QueryComplexity analysis', () => {
377
374
scalar
378
375
}
379
376
` ) ;
380
- const validationErrors : GraphQLError [ ] = [ ] ;
381
- const context = new ValidationContext ( schema , ast , typeInfo , err => validationErrors . push ( err ) ) ;
377
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
382
378
const visitor = new ComplexityVisitor ( context , {
383
379
maximumComplexity : 100 ,
384
380
estimators : [ ]
385
381
} ) ;
386
382
visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
387
- expect ( validationErrors . length ) . to . equal ( 1 ) ;
388
- expect ( validationErrors [ 0 ] . message ) . to . equal (
383
+ expect ( context . getErrors ( ) . length ) . to . equal ( 1 ) ;
384
+ expect ( context . getErrors ( ) [ 0 ] . message ) . to . equal (
389
385
'No complexity could be calculated for field Query.scalar. ' +
390
386
'At least one complexity estimator has to return a complexity score.'
391
387
) ;
@@ -397,17 +393,16 @@ describe('QueryComplexity analysis', () => {
397
393
scalar
398
394
}
399
395
` ) ;
400
- const validationErrors : GraphQLError [ ] = [ ] ;
401
- const context = new ValidationContext ( schema , ast , typeInfo , err => validationErrors . push ( err ) ) ;
396
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
402
397
const visitor = new ComplexityVisitor ( context , {
403
398
maximumComplexity : 100 ,
404
399
estimators : [
405
400
fieldExtensionsEstimator ( )
406
401
]
407
402
} ) ;
408
403
visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
409
- expect ( validationErrors . length ) . to . equal ( 1 ) ;
410
- expect ( validationErrors [ 0 ] . message ) . to . equal (
404
+ expect ( context . getErrors ( ) . length ) . to . equal ( 1 ) ;
405
+ expect ( context . getErrors ( ) [ 0 ] . message ) . to . equal (
411
406
'No complexity could be calculated for field Query.scalar. ' +
412
407
'At least one complexity estimator has to return a complexity score.'
413
408
) ;
0 commit comments