Skip to content

Commit 3b51194

Browse files
author
Sam Cooke
committed
Extract completeObjectValue from CompleteValue
Related GraphQL-js commit: graphql/graphql-js@5a13648
1 parent 6cf4407 commit 3b51194

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

graphql/execution/executor.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ def complete_value(self, ctx, return_type, field_asts, info, result):
257257
if isinstance(return_type, (GraphQLScalarType, GraphQLEnumType)):
258258
return self.complete_leaf_value(ctx, return_type, field_asts, info, result)
259259

260-
runtime_type = None
260+
if isinstance(return_type, GraphQLObjectType):
261+
return self.complete_object_value(ctx, return_type, field_asts, info, result)
261262

262263
# Field type must be Object, Interface or Union and expect sub-selections.
263-
if isinstance(return_type, GraphQLObjectType):
264-
runtime_type = return_type
264+
runtime_type = None
265265

266-
elif isinstance(return_type, (GraphQLInterfaceType, GraphQLUnionType)):
266+
if isinstance(return_type, (GraphQLInterfaceType, GraphQLUnionType)):
267267
runtime_type = return_type.resolve_type(result, info)
268268
if runtime_type and not return_type.is_possible_type(runtime_type):
269269
raise GraphQLError(
@@ -274,24 +274,7 @@ def complete_value(self, ctx, return_type, field_asts, info, result):
274274
if not runtime_type:
275275
return None
276276

277-
if runtime_type.is_type_of and not runtime_type.is_type_of(result, info):
278-
raise GraphQLError(
279-
u'Expected value of type "{}" but got {}.'.format(return_type, type(result).__name__),
280-
field_asts
281-
)
282-
283-
# Collect sub-fields to execute to complete this value.
284-
subfield_asts = DefaultOrderedDict(list) if self._enforce_strict_ordering else collections.defaultdict(list)
285-
visited_fragment_names = set()
286-
for field_ast in field_asts:
287-
selection_set = field_ast.selection_set
288-
if selection_set:
289-
subfield_asts = collect_fields(
290-
ctx, runtime_type, selection_set,
291-
subfield_asts, visited_fragment_names
292-
)
293-
294-
return self._execute_fields(ctx, runtime_type, result, subfield_asts)
277+
return self.complete_object_value(ctx, runtime_type, field_asts, info, result)
295278

296279
def complete_list_value(self, ctx, return_type, field_asts, info, result):
297280
"""
@@ -324,6 +307,29 @@ def complete_leaf_value(self, ctx, return_type, field_asts, info, result):
324307

325308
return serialized_result
326309

310+
def complete_object_value(self, ctx, return_type, field_asts, info, result):
311+
"""
312+
Complete an Object value by evaluating all sub-selections.
313+
"""
314+
if return_type.is_type_of and not return_type.is_type_of(result, info):
315+
raise GraphQLError(
316+
u'Expected value of type "{}" but got {}.'.format(return_type, type(result).__name__),
317+
field_asts
318+
)
319+
320+
# Collect sub-fields to execute to complete this value.
321+
subfield_asts = DefaultOrderedDict(list) if self._enforce_strict_ordering else collections.defaultdict(list)
322+
visited_fragment_names = set()
323+
for field_ast in field_asts:
324+
selection_set = field_ast.selection_set
325+
if selection_set:
326+
subfield_asts = collect_fields(
327+
ctx, return_type, selection_set,
328+
subfield_asts, visited_fragment_names
329+
)
330+
331+
return self._execute_fields(ctx, return_type, result, subfield_asts)
332+
327333
def resolve_or_error(self, resolve_fn, source, args, info):
328334
curried_resolve_fn = functools.partial(resolve_fn, source, args, info)
329335

0 commit comments

Comments
 (0)