Skip to content

Commit 6b4da18

Browse files
authored
Merge pull request #95 from tgriesser/resolve-type-string
Allow resolve_type to return a type name string
2 parents 97d029c + 0d25124 commit 6b4da18

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

graphql/execution/executor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import sys
55

6+
from six import string_types
67
from promise import Promise, promise_for_dict, promisify, is_thenable
78

89
from ..error import GraphQLError, GraphQLLocatedError
@@ -325,6 +326,9 @@ def complete_abstract_value(exe_context, return_type, field_asts, info, result):
325326
else:
326327
runtime_type = get_default_resolve_type_fn(result, exe_context.context_value, info, return_type)
327328

329+
if isinstance(runtime_type, string_types):
330+
runtime_type = info.schema.get_type(runtime_type)
331+
328332
if not isinstance(runtime_type, GraphQLObjectType):
329333
raise GraphQLError(
330334
('Abstract type {} must resolve to an Object type at runtime ' +

graphql/execution/tests/test_abstract.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,69 @@ def test_resolve_type_on_union_yields_useful_error():
295295
result = graphql(schema, query)
296296
assert result.errors[0].message == 'Runtime Object type "Human" is not a possible type for "Pet".'
297297
assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}, None]}
298+
299+
300+
def test_resolve_type_can_use_type_string():
301+
302+
def type_string_resolver(obj, *_):
303+
if isinstance(obj, Dog):
304+
return 'Dog'
305+
if isinstance(obj, Cat):
306+
return 'Cat'
307+
308+
PetType = GraphQLInterfaceType(
309+
name='Pet',
310+
fields={
311+
'name': GraphQLField(GraphQLString)
312+
},
313+
resolve_type=type_string_resolver
314+
)
315+
316+
DogType = GraphQLObjectType(
317+
name='Dog',
318+
interfaces=[PetType],
319+
fields={
320+
'name': GraphQLField(GraphQLString),
321+
'woofs': GraphQLField(GraphQLBoolean)
322+
}
323+
)
324+
325+
CatType = GraphQLObjectType(
326+
name='Cat',
327+
interfaces=[PetType],
328+
fields={
329+
'name': GraphQLField(GraphQLString),
330+
'meows': GraphQLField(GraphQLBoolean)
331+
}
332+
)
333+
334+
schema = GraphQLSchema(
335+
query=GraphQLObjectType(
336+
name='Query',
337+
fields={
338+
'pets': GraphQLField(
339+
GraphQLList(PetType),
340+
resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False)]
341+
)
342+
}
343+
),
344+
types=[CatType, DogType]
345+
)
346+
347+
query = '''
348+
{
349+
pets {
350+
name
351+
... on Dog {
352+
woofs
353+
}
354+
... on Cat {
355+
meows
356+
}
357+
}
358+
}
359+
'''
360+
361+
result = graphql(schema, query)
362+
assert not result.errors
363+
assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}]}

0 commit comments

Comments
 (0)