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