Skip to content

Commit 0efee6b

Browse files
committed
Fixed Union resolve_type. Fixed #313
1 parent 2e58f53 commit 0efee6b

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

graphene/tests/__init__.py

Whitespace-only changes.

graphene/tests/issues/__init__.py

Whitespace-only changes.

graphene/tests/issues/test_313.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# https://github.com/graphql-python/graphene/issues/313
2+
3+
import graphene
4+
from graphene import resolve_only_args
5+
6+
class Success(graphene.ObjectType):
7+
yeah = graphene.String()
8+
9+
10+
class Error(graphene.ObjectType):
11+
message = graphene.String()
12+
13+
14+
class CreatePostResult(graphene.Union):
15+
class Meta:
16+
types = [Success, Error]
17+
18+
19+
class CreatePost(graphene.Mutation):
20+
class Input:
21+
text = graphene.String(required=True)
22+
23+
result = graphene.Field(CreatePostResult)
24+
25+
@resolve_only_args
26+
def mutate(self, text):
27+
result = Success(yeah='yeah')
28+
29+
return CreatePost(result=result)
30+
31+
32+
class Mutations(graphene.ObjectType):
33+
create_post = CreatePost.Field()
34+
35+
# tests.py
36+
37+
def test_create_post():
38+
query_string = '''
39+
mutation {
40+
createPost(text: "Try this out") {
41+
result {
42+
__typename
43+
}
44+
}
45+
}
46+
'''
47+
48+
schema = graphene.Schema(mutation=Mutations)
49+
result = schema.execute(query_string)
50+
51+
assert not result.errors
52+
assert result.data['createPost']['result']['__typename'] == 'Success'

graphene/types/union.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ class Union(six.with_metaclass(UnionMeta)):
3939
to determine which type is actually used when the field is resolved.
4040
'''
4141

42-
resolve_type = None
42+
@classmethod
43+
def resolve_type(cls, instance, context, info):
44+
from .objecttype import ObjectType
45+
if isinstance(instance, ObjectType):
46+
return type(instance)
4347

4448
def __init__(self, *args, **kwargs):
4549
raise Exception("A Union cannot be intitialized")

0 commit comments

Comments
 (0)