Skip to content

Commit 978b834

Browse files
committed
Move getTypeOf to execute.js and rename to defaultResolveTypeFn to mirror defaultResolveFn
Related GraphQL-js commit: graphql/graphql-js@edc405a
1 parent 5fc4cdb commit 978b834

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

graphql/execution/executor.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,23 +301,33 @@ def complete_abstract_value(exe_context, return_type, field_asts, info, result):
301301
Complete an value of an abstract type by determining the runtime type of that value, then completing based
302302
on that type.
303303
"""
304-
# Field type must be Object, Interface or Union and expect sub-selections.
305304
runtime_type = None
306305

306+
# Field type must be Object, Interface or Union and expect sub-selections.
307307
if isinstance(return_type, (GraphQLInterfaceType, GraphQLUnionType)):
308-
runtime_type = return_type.resolve_type(result, info)
309-
if runtime_type and not return_type.is_possible_type(runtime_type):
310-
raise GraphQLError(
311-
u'Runtime Object type "{}" is not a possible type for "{}".'.format(runtime_type, return_type),
312-
field_asts
313-
)
308+
if return_type.resolve_type:
309+
runtime_type = return_type.resolve_type(result, info)
310+
if runtime_type and not return_type.is_possible_type(runtime_type):
311+
raise GraphQLError(
312+
u'Runtime Object type "{}" is not a possible type for "{}".'.format(runtime_type, return_type),
313+
field_asts
314+
)
315+
else:
316+
runtime_type = get_default_resolve_type_fn(result, info, return_type)
314317

315318
if not runtime_type:
316319
return None
317320

318321
return complete_object_value(exe_context, runtime_type, field_asts, info, result)
319322

320323

324+
def get_default_resolve_type_fn(value, info, abstract_type):
325+
possible_types = abstract_type.get_possible_types()
326+
for type in possible_types:
327+
if callable(type.is_type_of) and type.is_type_of(value, info):
328+
return type
329+
330+
321331
def complete_object_value(exe_context, return_type, field_asts, info, result):
322332
"""
323333
Complete an Object value by evaluating all sub-selections.

graphql/type/definition.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def define_interfaces(type, interfaces):
278278
'{} may only implement Interface types, it cannot implement: {}.'.format(type, interface)
279279
)
280280

281-
if not callable(interface.type_resolver):
281+
if not callable(interface.resolve_type):
282282
assert callable(type.is_type_of), (
283283
'Interface Type {} does not provide a "resolve_type" function '
284284
'and implementing Type {} does not provide a "is_type_of" '
@@ -360,7 +360,7 @@ class GraphQLInterfaceType(GraphQLType):
360360
'name': GraphQLField(GraphQLString),
361361
})
362362
"""
363-
__slots__ = 'name', 'description', 'type_resolver', '_fields', '_impls', '_field_map', '_possible_type_names'
363+
__slots__ = 'name', 'description', 'resolve_type', '_fields', '_impls', '_field_map', '_possible_type_names'
364364

365365
def __init__(self, name, fields=None, resolve_type=None, description=None):
366366
assert name, 'Type must be named.'
@@ -371,7 +371,7 @@ def __init__(self, name, fields=None, resolve_type=None, description=None):
371371
if resolve_type is not None:
372372
assert callable(resolve_type), '{} must provide "resolve_type" as a function.'.format(self)
373373

374-
self.type_resolver = resolve_type
374+
self.resolve_type = resolve_type
375375
self._fields = fields
376376

377377
self._impls = []
@@ -394,19 +394,6 @@ def is_possible_type(self, type):
394394
)
395395
return type.name in self._possible_type_names
396396

397-
def resolve_type(self, value, info):
398-
if self.type_resolver:
399-
return self.type_resolver(value, info)
400-
401-
return get_type_of(value, info, self)
402-
403-
404-
def get_type_of(value, info, abstract_type):
405-
possible_types = abstract_type.get_possible_types()
406-
for type in possible_types:
407-
if callable(type.is_type_of) and type.is_type_of(value, info):
408-
return type
409-
410397

411398
class GraphQLUnionType(GraphQLType):
412399
"""Union Type Definition
@@ -426,7 +413,7 @@ def resolve_type(self, value):
426413
if isinstance(value, Cat):
427414
return CatType()
428415
"""
429-
__slots__ = 'name', 'description', '_resolve_type', '_types', '_possible_type_names', '_possible_types'
416+
__slots__ = 'name', 'description', 'resolve_type', '_types', '_possible_type_names', '_possible_types'
430417

431418
def __init__(self, name, types=None, resolve_type=None, description=None):
432419
assert name, 'Type must be named.'
@@ -437,7 +424,7 @@ def __init__(self, name, types=None, resolve_type=None, description=None):
437424
if resolve_type is not None:
438425
assert callable(resolve_type), '{} must provide "resolve_type" as a function.'.format(self)
439426

440-
self._resolve_type = resolve_type
427+
self.resolve_type = resolve_type
441428
self._types = types
442429
self._possible_types = None
443430
self._possible_type_names = None
@@ -456,20 +443,14 @@ def is_possible_type(self, type):
456443

457444
return type.name in self._possible_type_names
458445

459-
def resolve_type(self, value, info):
460-
if self._resolve_type:
461-
return self._resolve_type(value, info)
462-
463-
return get_type_of(value, info, self)
464-
465446

466447
def define_types(union_type, types):
467448
if callable(types):
468449
types = types()
469450

470451
assert isinstance(types, (list, tuple)) and len(
471452
types) > 0, 'Must provide types for Union {}.'.format(union_type.name)
472-
has_resolve_type_fn = callable(union_type._resolve_type)
453+
has_resolve_type_fn = callable(union_type.resolve_type)
473454

474455
for type in types:
475456
assert isinstance(type, GraphQLObjectType), (

0 commit comments

Comments
 (0)