Skip to content

Allow resolve_type to return a type name string #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions graphql/execution/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import sys

from six import string_types
from promise import Promise, promise_for_dict, promisify, is_thenable

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

if isinstance(runtime_type, string_types):
runtime_type = info.schema.get_type(runtime_type)

if not isinstance(runtime_type, GraphQLObjectType):
raise GraphQLError(
('Abstract type {} must resolve to an Object type at runtime ' +
Expand Down
66 changes: 66 additions & 0 deletions graphql/execution/tests/test_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,69 @@ def test_resolve_type_on_union_yields_useful_error():
result = graphql(schema, query)
assert result.errors[0].message == 'Runtime Object type "Human" is not a possible type for "Pet".'
assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}, None]}


def test_resolve_type_can_use_type_string():

def type_string_resolver(obj, *_):
if isinstance(obj, Dog):
return 'Dog'
if isinstance(obj, Cat):
return 'Cat'

PetType = GraphQLInterfaceType(
name='Pet',
fields={
'name': GraphQLField(GraphQLString)
},
resolve_type=type_string_resolver
)

DogType = GraphQLObjectType(
name='Dog',
interfaces=[PetType],
fields={
'name': GraphQLField(GraphQLString),
'woofs': GraphQLField(GraphQLBoolean)
}
)

CatType = GraphQLObjectType(
name='Cat',
interfaces=[PetType],
fields={
'name': GraphQLField(GraphQLString),
'meows': GraphQLField(GraphQLBoolean)
}
)

schema = GraphQLSchema(
query=GraphQLObjectType(
name='Query',
fields={
'pets': GraphQLField(
GraphQLList(PetType),
resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False)]
)
}
),
types=[CatType, DogType]
)

query = '''
{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}
'''

result = graphql(schema, query)
assert not result.errors
assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}]}