Skip to content

Commit a033eae

Browse files
author
Sam Cooke
committed
Extract completeAbstractValue from CompleteValue
Related GraphQL-js commit: graphql/graphql-js@d484f31
1 parent 3b51194 commit a033eae

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

graphql/execution/executor.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ def complete_value(self, ctx, return_type, field_asts, info, result):
214214
If the field type is a Scalar or Enum, ensures the completed value is a legal value of the type by calling the
215215
`serialize` method of GraphQL type definition.
216216
217+
If the field is an abstract type, determine the runtime type of the value and then complete based on that type.
218+
217219
Otherwise, the field type expects a sub-selection set, and will complete the value by evaluating all
218220
sub-selections.
219221
"""
@@ -260,21 +262,11 @@ def complete_value(self, ctx, return_type, field_asts, info, result):
260262
if isinstance(return_type, GraphQLObjectType):
261263
return self.complete_object_value(ctx, return_type, field_asts, info, result)
262264

263-
# Field type must be Object, Interface or Union and expect sub-selections.
264-
runtime_type = None
265-
266265
if isinstance(return_type, (GraphQLInterfaceType, GraphQLUnionType)):
267-
runtime_type = return_type.resolve_type(result, info)
268-
if runtime_type and not return_type.is_possible_type(runtime_type):
269-
raise GraphQLError(
270-
u'Runtime Object type "{}" is not a possible type for "{}".'.format(runtime_type, return_type),
271-
field_asts
272-
)
273-
274-
if not runtime_type:
275-
return None
266+
return self.complete_abstract_value(ctx, return_type, field_asts, info, result)
276267

277-
return self.complete_object_value(ctx, runtime_type, field_asts, info, result)
268+
# Not reachable
269+
return None
278270

279271
def complete_list_value(self, ctx, return_type, field_asts, info, result):
280272
"""
@@ -330,6 +322,27 @@ def complete_object_value(self, ctx, return_type, field_asts, info, result):
330322

331323
return self._execute_fields(ctx, return_type, result, subfield_asts)
332324

325+
def complete_abstract_value(self, ctx, return_type, field_asts, info, result):
326+
"""
327+
Complete an value of an abstract type by determining the runtime type of that value, then completing based
328+
on that type.
329+
"""
330+
# Field type must be Object, Interface or Union and expect sub-selections.
331+
runtime_type = None
332+
333+
if isinstance(return_type, (GraphQLInterfaceType, GraphQLUnionType)):
334+
runtime_type = return_type.resolve_type(result, info)
335+
if runtime_type and not return_type.is_possible_type(runtime_type):
336+
raise GraphQLError(
337+
u'Runtime Object type "{}" is not a possible type for "{}".'.format(runtime_type, return_type),
338+
field_asts
339+
)
340+
341+
if not runtime_type:
342+
return None
343+
344+
return self.complete_object_value(ctx, runtime_type, field_asts, info, result)
345+
333346
def resolve_or_error(self, resolve_fn, source, args, info):
334347
curried_resolve_fn = functools.partial(resolve_fn, source, args, info)
335348

0 commit comments

Comments
 (0)