9
9
GraphQLObjectType , GraphQLUnionType )
10
10
from ..base import ResolveInfo , Undefined , collect_fields , get_field_def
11
11
from ..values import get_argument_values
12
-
12
+ from ... error import GraphQLError
13
13
14
14
def is_promise (obj ):
15
15
return isinstance (obj , Promise )
@@ -55,6 +55,7 @@ def get_resolvers(context, type, selection_set):
55
55
field_fragment = Fragment (
56
56
type = field_base_type ,
57
57
selection_set = field_ast .selection_set ,
58
+ info = info ,
58
59
context = context
59
60
)
60
61
elif isinstance (field_base_type , (GraphQLInterfaceType , GraphQLUnionType )):
@@ -76,10 +77,11 @@ def get_resolvers(context, type, selection_set):
76
77
77
78
class Fragment (object ):
78
79
79
- def __init__ (self , type , selection_set , context = None ):
80
+ def __init__ (self , type , selection_set , context = None , info = None ):
80
81
self .type = type
81
82
self .selection_set = selection_set
82
83
self .context = context
84
+ self .info = info
83
85
84
86
@cached_property
85
87
def partial_resolvers (self ):
@@ -89,7 +91,16 @@ def partial_resolvers(self):
89
91
self .selection_set
90
92
)
91
93
94
+ def have_type (self , root ):
95
+ return not self .type .is_type_of or self .type .is_type_of (root , self .context .context_value , self .info )
96
+
92
97
def resolve (self , root ):
98
+ if root and not self .have_type (root ):
99
+ raise GraphQLError (
100
+ u'Expected value of type "{}" but got: {}.' .format (self .type , type (root ).__name__ ),
101
+ self .info .field_asts
102
+ )
103
+
93
104
contains_promise = False
94
105
95
106
final_results = OrderedDict ()
@@ -98,6 +109,7 @@ def resolve(self, root):
98
109
# for field_name, field_resolver, field_args, context, info in self.partial_resolvers)
99
110
# )
100
111
for response_name , field_resolver , field_args , context , info in self .partial_resolvers :
112
+
101
113
result = field_resolver (root , field_args , context , info )
102
114
if result is Undefined :
103
115
continue
@@ -144,7 +156,8 @@ def __eq__(self, other):
144
156
return isinstance (other , Fragment ) and (
145
157
other .type == self .type and
146
158
other .selection_set == self .selection_set and
147
- other .context == self .context
159
+ other .context == self .context and
160
+ other .info == self .info
148
161
)
149
162
150
163
@@ -169,7 +182,7 @@ def get_fragment(self, type):
169
182
assert type in self .possible_types , (
170
183
'Runtime Object type "{}" is not a possible type for "{}".'
171
184
).format (type , self .abstract_type )
172
- self ._fragments [type ] = Fragment (type , self .selection_set , self .context )
185
+ self ._fragments [type ] = Fragment (type , self .selection_set , self .context , self . info )
173
186
return self ._fragments [type ]
174
187
175
188
def resolve_type (self , result ):
0 commit comments