11from ...error import GraphQLError
2- from ...type .definition import (GraphQLInterfaceType , GraphQLObjectType ,
3- GraphQLUnionType )
42from ...utils .type_from_ast import type_from_ast
3+ from ...utils .type_comparators import do_types_overlap
54from .base import ValidationRule
65
76
@@ -10,7 +9,7 @@ class PossibleFragmentSpreads(ValidationRule):
109 def enter_InlineFragment (self , node , key , parent , path , ancestors ):
1110 frag_type = self .context .get_type ()
1211 parent_type = self .context .get_parent_type ()
13- if frag_type and parent_type and not self . do_types_overlap (frag_type , parent_type ):
12+ if frag_type and parent_type and not do_types_overlap (frag_type , parent_type ):
1413 self .context .report_error (GraphQLError (
1514 self .type_incompatible_anon_spread_message (parent_type , frag_type ),
1615 [node ]
@@ -20,7 +19,7 @@ def enter_FragmentSpread(self, node, key, parent, path, ancestors):
2019 frag_name = node .name .value
2120 frag_type = self .get_fragment_type (self .context , frag_name )
2221 parent_type = self .context .get_parent_type ()
23- if frag_type and parent_type and not self . do_types_overlap (frag_type , parent_type ):
22+ if frag_type and parent_type and not do_types_overlap (frag_type , parent_type ):
2423 self .context .report_error (GraphQLError (
2524 self .type_incompatible_spread_message (frag_name , parent_type , frag_type ),
2625 [node ]
@@ -31,21 +30,6 @@ def get_fragment_type(context, name):
3130 frag = context .get_fragment (name )
3231 return frag and type_from_ast (context .get_schema (), frag .type_condition )
3332
34- @staticmethod
35- def do_types_overlap (t1 , t2 ):
36- if t1 == t2 :
37- return True
38- if isinstance (t1 , GraphQLObjectType ):
39- if isinstance (t2 , GraphQLObjectType ):
40- return False
41- return t1 in t2 .get_possible_types ()
42- if isinstance (t1 , GraphQLInterfaceType ) or isinstance (t1 , GraphQLUnionType ):
43- if isinstance (t2 , GraphQLObjectType ):
44- return t2 in t1 .get_possible_types ()
45-
46- t1_type_names = {possible_type .name : possible_type for possible_type in t1 .get_possible_types ()}
47- return any (t .name in t1_type_names for t in t2 .get_possible_types ())
48-
4933 @staticmethod
5034 def type_incompatible_spread_message (frag_name , parent_type , frag_type ):
5135 return 'Fragment {} cannot be spread here as objects of type {} can never be of type {}' .format (frag_name ,
0 commit comments