4
4
from graphql import Source , execute , parse , GraphQLError
5
5
6
6
from ..field import Field
7
+ from ..interface import Interface
7
8
from ..inputfield import InputField
8
9
from ..inputobjecttype import InputObjectType
9
10
from ..objecttype import ObjectType
10
11
from ..scalars import Int , String
11
12
from ..schema import Schema
12
13
from ..structures import List
14
+ from ..union import Union
13
15
from ..dynamic import Dynamic
14
16
15
17
@@ -24,6 +26,99 @@ class Query(ObjectType):
24
26
assert executed .data == {'hello' : 'World' }
25
27
26
28
29
+ def test_query_union ():
30
+ class one_object (object ):
31
+ pass
32
+
33
+ class two_object (object ):
34
+ pass
35
+
36
+ class One (ObjectType ):
37
+ one = String ()
38
+
39
+ @classmethod
40
+ def is_type_of (cls , root , context , info ):
41
+ return isinstance (root , one_object )
42
+
43
+ class Two (ObjectType ):
44
+ two = String ()
45
+
46
+ @classmethod
47
+ def is_type_of (cls , root , context , info ):
48
+ return isinstance (root , two_object )
49
+
50
+ class MyUnion (Union ):
51
+ class Meta :
52
+ types = (One , Two )
53
+
54
+ class Query (ObjectType ):
55
+ unions = List (MyUnion )
56
+
57
+ def resolve_unions (self , args , context , info ):
58
+ return [one_object (), two_object ()]
59
+
60
+ hello_schema = Schema (Query )
61
+
62
+ executed = hello_schema .execute ('{ unions { __typename } }' )
63
+ assert not executed .errors
64
+ assert executed .data == {
65
+ 'unions' : [{
66
+ '__typename' : 'One'
67
+ }, {
68
+ '__typename' : 'Two'
69
+ }]
70
+ }
71
+
72
+
73
+ def test_query_interface ():
74
+ class one_object (object ):
75
+ pass
76
+
77
+ class two_object (object ):
78
+ pass
79
+
80
+ class MyInterface (Interface ):
81
+ base = String ()
82
+
83
+ class One (ObjectType ):
84
+ class Meta :
85
+ interfaces = (MyInterface , )
86
+
87
+ one = String ()
88
+
89
+ @classmethod
90
+ def is_type_of (cls , root , context , info ):
91
+ return isinstance (root , one_object )
92
+
93
+ class Two (ObjectType ):
94
+ class Meta :
95
+ interfaces = (MyInterface , )
96
+
97
+ two = String ()
98
+
99
+ @classmethod
100
+ def is_type_of (cls , root , context , info ):
101
+ return isinstance (root , two_object )
102
+
103
+ class Query (ObjectType ):
104
+ interfaces = List (MyInterface )
105
+
106
+ def resolve_interfaces (self , args , context , info ):
107
+ return [one_object (), two_object ()]
108
+
109
+ hello_schema = Schema (Query , types = [One , Two ])
110
+
111
+ executed = hello_schema .execute ('{ interfaces { __typename } }' )
112
+ assert not executed .errors
113
+ assert executed .data == {
114
+ 'interfaces' : [{
115
+ '__typename' : 'One'
116
+ }, {
117
+ '__typename' : 'Two'
118
+ }]
119
+ }
120
+
121
+
27
122
def test_query_dynamic ():
28
123
class Query (ObjectType ):
29
124
hello = Dynamic (lambda : String (resolver = lambda * _ : 'World' ))
0 commit comments