@@ -271,6 +271,93 @@ describe('QueryComplexity analysis', () => {
271
271
expect ( visitor . complexity ) . to . equal ( 10 ) ;
272
272
} ) ;
273
273
274
+ it ( 'should ignore unused variables with include' , ( ) => {
275
+ const ast = parse ( `
276
+ query ($unusedVar: ID!, $shouldInclude: Boolean! = false) {
277
+ variableScalar(count: 100) @include(if: $shouldInclude)
278
+ }
279
+ ` ) ;
280
+
281
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
282
+ const visitor = new ComplexityVisitor ( context , {
283
+ maximumComplexity : 100 ,
284
+ estimators : [ simpleEstimator ( { defaultComplexity : 10 } ) ] ,
285
+ variables : { shouldInclude : true } ,
286
+ } ) ;
287
+
288
+ visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
289
+ expect ( visitor . complexity ) . to . equal ( 10 ) ;
290
+ } ) ;
291
+
292
+ it ( 'should ignore unused variables with skip' , ( ) => {
293
+ const ast = parse ( `
294
+ query ($unusedVar: ID!, $shouldSkip: Boolean! = false) {
295
+ variableScalar(count: 100) @skip(if: $shouldSkip)
296
+ }
297
+ ` ) ;
298
+
299
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
300
+ const visitor = new ComplexityVisitor ( context , {
301
+ maximumComplexity : 100 ,
302
+ estimators : [ simpleEstimator ( { defaultComplexity : 10 } ) ] ,
303
+ variables : { shouldSkip : false } ,
304
+ } ) ;
305
+
306
+ visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
307
+ expect ( visitor . complexity ) . to . equal ( 10 ) ;
308
+ } ) ;
309
+
310
+ it ( 'should ignore unused variables with unused include' , ( ) => {
311
+ const ast = parse ( `
312
+ query ($unusedVar: ID!, $shouldInclude: Boolean! = false) {
313
+ variableScalar(count: 100) @include(if: $shouldInclude)
314
+ }
315
+ ` ) ;
316
+
317
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
318
+ const visitor = new ComplexityVisitor ( context , {
319
+ maximumComplexity : 100 ,
320
+ estimators : [ simpleEstimator ( { defaultComplexity : 10 } ) ] ,
321
+ } ) ;
322
+
323
+ visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
324
+ expect ( visitor . complexity ) . to . equal ( 10 ) ;
325
+ } ) ;
326
+
327
+ it ( 'should ignore unused variables with unused skip' , ( ) => {
328
+ const ast = parse ( `
329
+ query ($unusedVar: ID!, $shouldSkip: Boolean! = false) {
330
+ variableScalar(count: 100) @skip(if: $shouldSkip)
331
+ }
332
+ ` ) ;
333
+
334
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
335
+ const visitor = new ComplexityVisitor ( context , {
336
+ maximumComplexity : 100 ,
337
+ estimators : [ simpleEstimator ( { defaultComplexity : 10 } ) ] ,
338
+ } ) ;
339
+
340
+ visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
341
+ expect ( visitor . complexity ) . to . equal ( 10 ) ;
342
+ } ) ;
343
+
344
+ it ( 'should ignore multiple unused variables' , ( ) => {
345
+ const ast = parse ( `
346
+ query ($unusedVar: ID!, $anotherUnusedVar: Boolean!) {
347
+ variableScalar(count: 100)
348
+ }
349
+ ` ) ;
350
+
351
+ const context = new CompatibleValidationContext ( schema , ast , typeInfo ) ;
352
+ const visitor = new ComplexityVisitor ( context , {
353
+ maximumComplexity : 100 ,
354
+ estimators : [ simpleEstimator ( { defaultComplexity : 10 } ) ] ,
355
+ } ) ;
356
+
357
+ visit ( ast , visitWithTypeInfo ( typeInfo , visitor ) ) ;
358
+ expect ( visitor . complexity ) . to . equal ( 10 ) ;
359
+ } ) ;
360
+
274
361
it ( 'should ignore unknown field' , ( ) => {
275
362
const ast = parse ( `
276
363
query {
@@ -639,7 +726,7 @@ describe('QueryComplexity analysis', () => {
639
726
...on Union {
640
727
...on Item {
641
728
complexScalar1: complexScalar
642
- }
729
+ }
643
730
}
644
731
...on SecondItem {
645
732
scalar
@@ -678,7 +765,7 @@ describe('QueryComplexity analysis', () => {
678
765
fragment F on Union {
679
766
...on Item {
680
767
complexScalar1: complexScalar
681
- }
768
+ }
682
769
}
683
770
` ) ;
684
771
@@ -759,7 +846,7 @@ describe('QueryComplexity analysis', () => {
759
846
}
760
847
}
761
848
}
762
-
849
+
763
850
fragment F on Query {
764
851
complexScalar
765
852
...on Query {
0 commit comments