Fixed Union resolve_type. Fixed #313

This commit is contained in:
Syrus Akbary 2016-11-14 20:14:21 -08:00
parent 2e58f53f18
commit 0efee6be5d
4 changed files with 57 additions and 1 deletions

View File

View File

View File

@ -0,0 +1,52 @@
# https://github.com/graphql-python/graphene/issues/313
import graphene
from graphene import resolve_only_args
class Success(graphene.ObjectType):
yeah = graphene.String()
class Error(graphene.ObjectType):
message = graphene.String()
class CreatePostResult(graphene.Union):
class Meta:
types = [Success, Error]
class CreatePost(graphene.Mutation):
class Input:
text = graphene.String(required=True)
result = graphene.Field(CreatePostResult)
@resolve_only_args
def mutate(self, text):
result = Success(yeah='yeah')
return CreatePost(result=result)
class Mutations(graphene.ObjectType):
create_post = CreatePost.Field()
# tests.py
def test_create_post():
query_string = '''
mutation {
createPost(text: "Try this out") {
result {
__typename
}
}
}
'''
schema = graphene.Schema(mutation=Mutations)
result = schema.execute(query_string)
assert not result.errors
assert result.data['createPost']['result']['__typename'] == 'Success'

View File

@ -39,7 +39,11 @@ class Union(six.with_metaclass(UnionMeta)):
to determine which type is actually used when the field is resolved.
'''
resolve_type = None
@classmethod
def resolve_type(cls, instance, context, info):
from .objecttype import ObjectType
if isinstance(instance, ObjectType):
return type(instance)
def __init__(self, *args, **kwargs):
raise Exception("A Union cannot be intitialized")