File tree 2 files changed +24
-4
lines changed
2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change 1
1
import pytest
2
2
3
+ from ..field import Field
3
4
from ..objecttype import ObjectType
4
5
from ..union import Union
6
+ from ..unmountedtype import UnmountedType
5
7
6
8
7
9
class MyObjectType1 (ObjectType ):
@@ -41,3 +43,15 @@ class MyUnion(Union):
41
43
pass
42
44
43
45
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
Original file line number Diff line number Diff line change 2
2
3
3
from ..utils .is_base_type import is_base_type
4
4
from .options import Options
5
+ from .unmountedtype import UnmountedType
5
6
6
7
7
8
class UnionMeta (type ):
@@ -30,7 +31,7 @@ def __str__(cls): # noqa: N805
30
31
return cls ._meta .name
31
32
32
33
33
- class Union (six .with_metaclass (UnionMeta )):
34
+ class Union (six .with_metaclass (UnionMeta , UnmountedType )):
34
35
'''
35
36
Union Type Definition
36
37
@@ -39,11 +40,16 @@ class Union(six.with_metaclass(UnionMeta)):
39
40
to determine which type is actually used when the field is resolved.
40
41
'''
41
42
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
+
42
51
@classmethod
43
52
def resolve_type (cls , instance , context , info ):
44
53
from .objecttype import ObjectType
45
54
if isinstance (instance , ObjectType ):
46
55
return type (instance )
47
-
48
- def __init__ (self , * args , ** kwargs ):
49
- raise Exception ("A Union cannot be intitialized" )
You can’t perform that action at this time.
0 commit comments