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 {
179179 nodeComplexity (
180180 node : FieldNode | FragmentDefinitionNode | InlineFragmentNode | OperationDefinitionNode ,
181181 typeDef : GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType ,
182- complexity : number = 0
183182 ) : number {
184183 if ( node . selectionSet ) {
185184 let fields :GraphQLFieldMap < any , any > = { } ;
186185 if ( typeDef instanceof GraphQLObjectType || typeDef instanceof GraphQLInterfaceType ) {
187186 fields = typeDef . getFields ( ) ;
188187 }
189- return complexity + node . selectionSet . selections . reduce (
188+ return node . selectionSet . selections . reduce (
190189 ( total : number , childNode : FieldNode | FragmentSpreadNode | InlineFragmentNode ) => {
191190 let nodeComplexity = 0 ;
192191
@@ -275,7 +274,6 @@ export default class QueryComplexity {
275274 case Kind . INLINE_FRAGMENT : {
276275 let inlineFragmentType = typeDef ;
277276 if ( childNode . typeCondition && childNode . typeCondition . name ) {
278- // $FlowFixMe: Not sure why flow thinks this can still be NULL
279277 inlineFragmentType = assertCompositeType (
280278 this . context . getSchema ( ) . getType ( childNode . typeCondition . name . value )
281279 ) ;
@@ -290,9 +288,9 @@ export default class QueryComplexity {
290288 }
291289 }
292290 return Math . max ( nodeComplexity , 0 ) + total ;
293- } , complexity ) ;
291+ } , 0 ) ;
294292 }
295- return complexity ;
293+ return 0 ;
296294 }
297295
298296 createError ( ) : GraphQLError {
Original file line number Diff line number Diff line change @@ -482,4 +482,32 @@ describe('QueryComplexity analysis', () => {
482482 } ) ;
483483 expect ( complexity2 ) . to . equal ( 20 ) ;
484484 } ) ;
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+ } ) ;
485513} ) ;
You can’t perform that action at this time.
0 commit comments