5
5
from ...types .scalars import String
6
6
from ..mutation import ClientIDMutation
7
7
from ..node import Node
8
+ from promise import Promise
8
9
9
10
10
11
class SharedFields (AbstractType ):
11
12
shared = String ()
12
13
13
14
14
15
class MyNode (ObjectType ):
15
-
16
16
class Meta :
17
17
interfaces = (Node , )
18
18
19
19
name = String ()
20
20
21
21
22
22
class SaySomething (ClientIDMutation ):
23
-
24
23
class Input :
25
24
what = String ()
25
+
26
26
phrase = String ()
27
27
28
28
@staticmethod
@@ -31,8 +31,19 @@ def mutate_and_get_payload(args, context, info):
31
31
return SaySomething (phrase = str (what ))
32
32
33
33
34
- class OtherMutation (ClientIDMutation ):
34
+ class SaySomethingPromise (ClientIDMutation ):
35
+ class Input :
36
+ what = String ()
37
+
38
+ phrase = String ()
39
+
40
+ @staticmethod
41
+ def mutate_and_get_payload (args , context , info ):
42
+ what = args .get ('what' )
43
+ return Promise .resolve (SaySomething (phrase = str (what )))
44
+
35
45
46
+ class OtherMutation (ClientIDMutation ):
36
47
class Input (SharedFields ):
37
48
additional_field = String ()
38
49
@@ -44,9 +55,9 @@ def mutate_and_get_payload(cls, args, context, info):
44
55
shared = args .get ('shared' , '' )
45
56
additionalField = args .get ('additionalField' , '' )
46
57
edge_type = MyNode .Connection .Edge
47
- return OtherMutation (name = shared + additionalField ,
48
- my_node_edge = edge_type (
49
- cursor = '1' , node = MyNode (name = 'name' )))
58
+ return OtherMutation (
59
+ name = shared + additionalField ,
60
+ my_node_edge = edge_type ( cursor = '1' , node = MyNode (name = 'name' )))
50
61
51
62
52
63
class RootQuery (ObjectType ):
@@ -55,13 +66,16 @@ class RootQuery(ObjectType):
55
66
56
67
class Mutation (ObjectType ):
57
68
say = SaySomething .Field ()
69
+ say_promise = SaySomethingPromise .Field ()
58
70
other = OtherMutation .Field ()
59
71
72
+
60
73
schema = Schema (query = RootQuery , mutation = Mutation )
61
74
62
75
63
76
def test_no_mutate_and_get_payload ():
64
77
with pytest .raises (AssertionError ) as excinfo :
78
+
65
79
class MyMutation (ClientIDMutation ):
66
80
pass
67
81
@@ -97,7 +111,9 @@ def test_mutation_input():
97
111
98
112
def test_subclassed_mutation ():
99
113
fields = OtherMutation ._meta .fields
100
- assert list (fields .keys ()) == ['name' , 'my_node_edge' , 'client_mutation_id' ]
114
+ assert list (fields .keys ()) == [
115
+ 'name' , 'my_node_edge' , 'client_mutation_id'
116
+ ]
101
117
assert isinstance (fields ['name' ], Field )
102
118
field = OtherMutation .Field ()
103
119
assert field .type == OtherMutation
@@ -111,7 +127,9 @@ def test_subclassed_mutation_input():
111
127
Input = OtherMutation .Input
112
128
assert issubclass (Input , InputObjectType )
113
129
fields = Input ._meta .fields
114
- assert list (fields .keys ()) == ['shared' , 'additional_field' , 'client_mutation_id' ]
130
+ assert list (fields .keys ()) == [
131
+ 'shared' , 'additional_field' , 'client_mutation_id'
132
+ ]
115
133
assert isinstance (fields ['shared' ], InputField )
116
134
assert fields ['shared' ].type == String
117
135
assert isinstance (fields ['additional_field' ], InputField )
@@ -120,16 +138,35 @@ def test_subclassed_mutation_input():
120
138
assert fields ['client_mutation_id' ].type == String
121
139
122
140
123
- # def test_node_query():
124
- # executed = schema.execute(
125
- # 'mutation a { say(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
126
- # )
127
- # assert not executed.errors
128
- # assert executed.data == {'say': {'phrase': 'hello'}}
141
+ def test_node_query ():
142
+ executed = schema .execute (
143
+ 'mutation a { say(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
144
+ )
145
+ assert not executed .errors
146
+ assert executed .data == {'say' : {'phrase' : 'hello' }}
147
+
148
+
149
+ def test_node_query ():
150
+ executed = schema .execute (
151
+ 'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
152
+ )
153
+ assert not executed .errors
154
+ assert executed .data == {'sayPromise' : {'phrase' : 'hello' }}
155
+
129
156
130
157
def test_edge_query ():
131
158
executed = schema .execute (
132
159
'mutation a { other(input: {clientMutationId:"1"}) { clientMutationId, myNodeEdge { cursor node { name }} } }'
133
160
)
134
161
assert not executed .errors
135
- assert dict (executed .data ) == {'other' : {'clientMutationId' : '1' , 'myNodeEdge' : {'cursor' : '1' , 'node' : {'name' : 'name' }}}}
162
+ assert dict (executed .data ) == {
163
+ 'other' : {
164
+ 'clientMutationId' : '1' ,
165
+ 'myNodeEdge' : {
166
+ 'cursor' : '1' ,
167
+ 'node' : {
168
+ 'name' : 'name'
169
+ }
170
+ }
171
+ }
172
+ }
0 commit comments