File tree 3 files changed +57
-1
lines changed
3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import schema from './fixtures/schema';
17
17
import ComplexityVisitor , { getComplexity } from '../QueryComplexity' ;
18
18
import {
19
19
simpleEstimator ,
20
+ directiveEstimator ,
20
21
fieldConfigEstimator ,
21
22
} from '../index' ;
22
23
@@ -321,4 +322,45 @@ describe('QueryComplexity analysis', () => {
321
322
'At least one complexity estimator has to return a complexity score.'
322
323
) ;
323
324
} ) ;
325
+
326
+ it . only ( 'should return NaN when no astNode available on field when use directiveEstimator' , ( ) => {
327
+ const ast = parse ( `
328
+ query {
329
+ _service {
330
+ sdl
331
+ }
332
+ }
333
+ ` ) ;
334
+
335
+ const complexity = getComplexity ( {
336
+ estimators : [
337
+ directiveEstimator ( ) ,
338
+ ] ,
339
+ schema,
340
+ query : ast
341
+ } ) ;
342
+ expect ( Number . isNaN ( complexity ) ) . to . equal ( true ) ;
343
+ } ) ;
344
+
345
+ it . only ( 'should skip complexity calculation by directiveEstimator when no astNode available on field' , ( ) => {
346
+ const ast = parse ( `
347
+ query {
348
+ _service {
349
+ sdl
350
+ }
351
+ }
352
+ ` ) ;
353
+
354
+ const complexity = getComplexity ( {
355
+ estimators : [
356
+ directiveEstimator ( ) ,
357
+ simpleEstimator ( {
358
+ defaultComplexity : 1
359
+ } )
360
+ ] ,
361
+ schema,
362
+ query : ast
363
+ } ) ;
364
+ expect ( complexity ) . to . equal ( 2 ) ;
365
+ } ) ;
324
366
} ) ;
Original file line number Diff line number Diff line change @@ -83,6 +83,14 @@ const Union = new GraphQLUnionType({
83
83
resolveType : ( ) => Item
84
84
} ) ;
85
85
86
+ const SDLInterface = new GraphQLInterfaceType ( {
87
+ name : 'SDLInterface' ,
88
+ fields : {
89
+ sdl : { type : GraphQLString }
90
+ } ,
91
+ resolveType : ( ) => '"SDL"'
92
+ } ) ;
93
+
86
94
const Query = new GraphQLObjectType ( {
87
95
name : 'Query' ,
88
96
fields : ( ) => ( {
@@ -126,7 +134,8 @@ const Query = new GraphQLObjectType({
126
134
type : new GraphQLNonNull ( GraphQLInt )
127
135
}
128
136
}
129
- }
137
+ } ,
138
+ _service : { type : SDLInterface } ,
130
139
} ) ,
131
140
} ) ;
132
141
Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ export default function (options?: {}): ComplexityEstimator {
28
28
} ) ;
29
29
30
30
return ( args : ComplexityEstimatorArgs ) => {
31
+ // Ignore if astNode is undefined
32
+ if ( ! args . field . astNode ) {
33
+ return ;
34
+ }
35
+
31
36
const values = getDirectiveValues ( directive , args . field . astNode ) ;
32
37
33
38
// Ignore if no directive set
You can’t perform that action at this time.
0 commit comments