Skip to content

Commit 2ef0e23

Browse files
committed
Set unions as unmounted types
1 parent a749d02 commit 2ef0e23

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

graphene/types/tests/test_union.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
22

3+
from ..field import Field
34
from ..objecttype import ObjectType
45
from ..union import Union
6+
from ..unmountedtype import UnmountedType
57

68

79
class MyObjectType1(ObjectType):
@@ -41,3 +43,15 @@ class MyUnion(Union):
4143
pass
4244

4345
assert str(exc_info.value) == 'Must provide types for Union MyUnion.'
46+
47+
48+
def test_union_can_be_mounted():
49+
class MyUnion(Union):
50+
class Meta:
51+
types = (MyObjectType1, MyObjectType2)
52+
53+
my_union_instance = MyUnion()
54+
assert isinstance(my_union_instance, UnmountedType)
55+
my_union_field = my_union_instance.mount_as(Field)
56+
assert isinstance(my_union_field, Field)
57+
assert my_union_field.type == MyUnion

graphene/types/union.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from ..utils.is_base_type import is_base_type
44
from .options import Options
5+
from .unmountedtype import UnmountedType
56

67

78
class UnionMeta(type):
@@ -30,7 +31,7 @@ def __str__(cls): # noqa: N805
3031
return cls._meta.name
3132

3233

33-
class Union(six.with_metaclass(UnionMeta)):
34+
class Union(six.with_metaclass(UnionMeta, UnmountedType)):
3435
'''
3536
Union Type Definition
3637
@@ -39,11 +40,16 @@ class Union(six.with_metaclass(UnionMeta)):
3940
to determine which type is actually used when the field is resolved.
4041
'''
4142

43+
@classmethod
44+
def get_type(cls):
45+
'''
46+
This function is called when the unmounted type (Union instance)
47+
is mounted (as a Field, InputField or Argument)
48+
'''
49+
return cls
50+
4251
@classmethod
4352
def resolve_type(cls, instance, context, info):
4453
from .objecttype import ObjectType
4554
if isinstance(instance, ObjectType):
4655
return type(instance)
47-
48-
def __init__(self, *args, **kwargs):
49-
raise Exception("A Union cannot be intitialized")

0 commit comments

Comments
 (0)