diff --git a/graphene/tests/__init__.py b/graphene/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/graphene/tests/issues/__init__.py b/graphene/tests/issues/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/graphene/tests/issues/test_313.py b/graphene/tests/issues/test_313.py new file mode 100644 index 00000000..bed87290 --- /dev/null +++ b/graphene/tests/issues/test_313.py @@ -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' \ No newline at end of file diff --git a/graphene/types/union.py b/graphene/types/union.py index 622f465e..3d236000 100644 --- a/graphene/types/union.py +++ b/graphene/types/union.py @@ -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")