@@ -19,7 +19,7 @@ import {
1919 GraphQLField , isCompositeType , GraphQLCompositeType , GraphQLFieldMap ,
2020 GraphQLSchema , DocumentNode , TypeInfo ,
2121 visit , visitWithTypeInfo ,
22- GraphQLDirective , isAbstractType ,
22+ GraphQLDirective , isAbstractType , GraphQLNamedType ,
2323} from 'graphql' ;
2424import {
2525 GraphQLUnionType ,
@@ -278,35 +278,40 @@ export default class QueryComplexity {
278278 }
279279 case Kind . FRAGMENT_SPREAD : {
280280 const fragment = this . context . getFragment ( childNode . name . value ) ;
281- if ( fragment ) {
282- const fragmentType = assertCompositeType (
283- this . context . getSchema ( ) . getType ( fragment . typeCondition . name . value )
281+ // Unknown fragment, should be caught by other validation rules
282+ if ( ! fragment ) {
283+ break ;
284+ }
285+ const fragmentType = this . context . getSchema ( ) . getType ( fragment . typeCondition . name . value ) ;
286+ // Invalid fragment type, ignore. Should be caught by other validation rules
287+ if ( ! isCompositeType ( fragmentType ) ) {
288+ break ;
289+ }
290+ const nodeComplexity = this . nodeComplexity ( fragment , fragmentType ) ;
291+ if ( isAbstractType ( fragmentType ) ) {
292+ // Add fragment complexity for all possible types
293+ complexities = addComplexities (
294+ nodeComplexity ,
295+ complexities ,
296+ this . context . getSchema ( ) . getPossibleTypes ( fragmentType ) . map ( t => t . name ) ,
297+ ) ;
298+ } else {
299+ // Add complexity for object type
300+ complexities = addComplexities (
301+ nodeComplexity ,
302+ complexities ,
303+ [ fragmentType . name ] ,
284304 ) ;
285- const nodeComplexity = this . nodeComplexity ( fragment , fragmentType ) ;
286- if ( isAbstractType ( fragmentType ) ) {
287- // Add fragment complexity for all possible types
288- complexities = addComplexities (
289- nodeComplexity ,
290- complexities ,
291- this . context . getSchema ( ) . getPossibleTypes ( fragmentType ) . map ( t => t . name ) ,
292- ) ;
293- } else {
294- // Add complexity for object type
295- complexities = addComplexities (
296- nodeComplexity ,
297- complexities ,
298- [ fragmentType . name ] ,
299- ) ;
300- }
301305 }
302306 break ;
303307 }
304308 case Kind . INLINE_FRAGMENT : {
305- let inlineFragmentType = typeDef ;
309+ let inlineFragmentType : GraphQLNamedType = typeDef ;
306310 if ( childNode . typeCondition && childNode . typeCondition . name ) {
307- inlineFragmentType = assertCompositeType (
308- this . context . getSchema ( ) . getType ( childNode . typeCondition . name . value )
309- ) ;
311+ inlineFragmentType = this . context . getSchema ( ) . getType ( childNode . typeCondition . name . value ) ;
312+ if ( ! isCompositeType ( inlineFragmentType ) ) {
313+ break ;
314+ }
310315 }
311316
312317 const nodeComplexity = this . nodeComplexity ( childNode , inlineFragmentType ) ;
0 commit comments