File tree Expand file tree Collapse file tree 2 files changed +31
-5
lines changed Expand file tree Collapse file tree 2 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -179,14 +179,13 @@ export default class QueryComplexity {
179
179
nodeComplexity (
180
180
node : FieldNode | FragmentDefinitionNode | InlineFragmentNode | OperationDefinitionNode ,
181
181
typeDef : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
182
- complexity : number = 0
183
182
) : number {
184
183
if ( node . selectionSet ) {
185
184
let fields :GraphQLFieldMap < any , any > = { } ;
186
185
if ( typeDef instanceof GraphQLObjectType || typeDef instanceof GraphQLInterfaceType ) {
187
186
fields = typeDef . getFields ( ) ;
188
187
}
189
- return complexity + node . selectionSet . selections . reduce (
188
+ return node . selectionSet . selections . reduce (
190
189
( total : number , childNode : FieldNode | FragmentSpreadNode | InlineFragmentNode ) => {
191
190
let nodeComplexity = 0 ;
192
191
@@ -275,7 +274,6 @@ export default class QueryComplexity {
275
274
case Kind . INLINE_FRAGMENT : {
276
275
let inlineFragmentType = typeDef ;
277
276
if ( childNode . typeCondition && childNode . typeCondition . name ) {
278
- // $FlowFixMe: Not sure why flow thinks this can still be NULL
279
277
inlineFragmentType = assertCompositeType (
280
278
this . context . getSchema ( ) . getType ( childNode . typeCondition . name . value )
281
279
) ;
@@ -290,9 +288,9 @@ export default class QueryComplexity {
290
288
}
291
289
}
292
290
return Math . max ( nodeComplexity , 0 ) + total ;
293
- } , complexity ) ;
291
+ } , 0 ) ;
294
292
}
295
- return complexity ;
293
+ return 0 ;
296
294
}
297
295
298
296
createError ( ) : GraphQLError {
Original file line number Diff line number Diff line change @@ -482,4 +482,32 @@ describe('QueryComplexity analysis', () => {
482
482
} ) ;
483
483
expect ( complexity2 ) . to . equal ( 20 ) ;
484
484
} ) ;
485
+
486
+ it ( 'should calculate max complexity for fragment on union type' , ( ) => {
487
+ const query = parse ( `
488
+ query Primary {
489
+ union {
490
+ ...on Item {
491
+ scalar
492
+ }
493
+ ...on SecondItem {
494
+ scalar
495
+ }
496
+ ...on SecondItem {
497
+ scalar
498
+ }
499
+ }
500
+ }
501
+ ` ) ;
502
+
503
+ const complexity = getComplexity ( {
504
+ estimators : [
505
+ fieldExtensionsEstimator ( ) ,
506
+ simpleEstimator ( { defaultComplexity : 1 } )
507
+ ] ,
508
+ schema,
509
+ query,
510
+ } ) ;
511
+ expect ( complexity ) . to . equal ( 3 ) ;
512
+ } ) ;
485
513
} ) ;
You can’t perform that action at this time.
0 commit comments