3
3
from graphql import graphql , graphql_sync
4
4
from graphql .type import (
5
5
GraphQLField ,
6
+ GraphQLFieldMap ,
6
7
GraphQLInputField ,
7
8
GraphQLInt ,
8
9
GraphQLObjectType ,
@@ -20,85 +21,31 @@ def __init__(self, result, clientMutationId=None):
20
21
self .result = result
21
22
22
23
23
- def dummy_resolve (_info , ** _input ):
24
- return Result (1 )
24
+ def dummy_resolve (_info , inputData = None , clientMutationId = None ):
25
+ return Result (inputData or 1 , clientMutationId )
25
26
26
27
27
- simple_mutation = mutation_with_client_mutation_id (
28
- "SimpleMutation" ,
29
- input_fields = {},
30
- output_fields = {"result" : GraphQLField (GraphQLInt )},
31
- mutate_and_get_payload = dummy_resolve ,
32
- )
33
-
34
- simple_mutation_with_description = mutation_with_client_mutation_id (
35
- "SimpleMutationWithDescription" ,
36
- description = "Simple Mutation Description" ,
37
- input_fields = {},
38
- output_fields = {"result" : GraphQLField (GraphQLInt )},
39
- mutate_and_get_payload = dummy_resolve ,
40
- )
41
-
42
- simple_mutation_with_deprecation_reason = mutation_with_client_mutation_id (
43
- "SimpleMutationWithDeprecationReason" ,
44
- input_fields = {},
45
- output_fields = {"result" : GraphQLField (GraphQLInt )},
46
- mutate_and_get_payload = dummy_resolve ,
47
- deprecation_reason = "Just because" ,
48
- )
49
-
50
- # noinspection PyPep8Naming
51
- simple_mutation_with_thunk_fields = mutation_with_client_mutation_id (
52
- "SimpleMutationWithThunkFields" ,
53
- input_fields = lambda : {"inputData" : GraphQLInputField (GraphQLInt )},
54
- output_fields = lambda : {"result" : GraphQLField (GraphQLInt )},
55
- mutate_and_get_payload = lambda _info , inputData , ** _input : Result (inputData ),
56
- )
57
-
58
-
59
- # noinspection PyPep8Naming
60
- async def mutate_and_get_one_as_payload_async (_info , ** _input ):
61
- return Result (1 )
62
-
63
-
64
- simple_async_mutation = mutation_with_client_mutation_id (
65
- "SimpleAsyncMutation" ,
66
- input_fields = {},
67
- output_fields = {"result" : GraphQLField (GraphQLInt )},
68
- mutate_and_get_payload = mutate_and_get_one_as_payload_async ,
69
- )
70
-
71
- simple_root_value_mutation = mutation_with_client_mutation_id (
72
- "SimpleRootValueMutation" ,
73
- input_fields = {},
74
- output_fields = {"result" : GraphQLField (GraphQLInt )},
75
- mutate_and_get_payload = lambda info , ** _input : info .root_value ,
76
- )
77
-
78
- query_type : GraphQLObjectType = GraphQLObjectType (
79
- "Query" , lambda : {"query" : GraphQLField (query_type )}
80
- )
28
+ async def dummy_resolve_async (_info , inputData = None , clientMutationId = None ):
29
+ return Result (inputData or 1 , clientMutationId )
81
30
82
- mutation_type = GraphQLObjectType (
83
- "Mutation" ,
84
- fields = {
85
- "simpleMutation" : simple_mutation ,
86
- "simpleMutationWithDescription" : simple_mutation_with_description ,
87
- "simpleMutationWithDeprecationReason" : simple_mutation_with_deprecation_reason ,
88
- "simpleMutationWithThunkFields" : simple_mutation_with_thunk_fields ,
89
- "simpleAsyncMutation" : simple_async_mutation ,
90
- "simpleRootValueMutation" : simple_root_value_mutation ,
91
- },
92
- )
93
31
94
- schema = GraphQLSchema (query = query_type , mutation = mutation_type )
32
+ def wrap_in_schema (mutation_fields : GraphQLFieldMap ) -> GraphQLSchema :
33
+ query_type = GraphQLObjectType (
34
+ "DummyQuery" , fields = {"dummy" : GraphQLField (GraphQLInt )}
35
+ )
36
+ mutation_type = GraphQLObjectType ("Mutation" , fields = mutation_fields )
37
+ return GraphQLSchema (query_type , mutation_type )
95
38
96
39
97
40
def describe_mutation_with_client_mutation_id ():
98
41
def requires_an_argument ():
42
+ some_mutation = mutation_with_client_mutation_id (
43
+ "SomeMutation" , {}, {"result" : GraphQLField (GraphQLInt )}, dummy_resolve
44
+ )
45
+ schema = wrap_in_schema ({"someMutation" : some_mutation })
99
46
source = """
100
47
mutation {
101
- simpleMutation {
48
+ someMutation {
102
49
result
103
50
}
104
51
}
@@ -107,41 +54,51 @@ def requires_an_argument():
107
54
None ,
108
55
[
109
56
{
110
- "message" : "Field 'simpleMutation ' argument 'input'"
111
- " of type 'SimpleMutationInput !' is required,"
57
+ "message" : "Field 'someMutation ' argument 'input'"
58
+ " of type 'SomeMutationInput !' is required,"
112
59
" but it was not provided." ,
113
60
"locations" : [(3 , 15 )],
114
61
}
115
62
],
116
63
)
117
64
118
65
def returns_the_same_client_mutation_id ():
66
+ some_mutation = mutation_with_client_mutation_id (
67
+ "SomeMutation" , {}, {"result" : GraphQLField (GraphQLInt )}, dummy_resolve
68
+ )
69
+ schema = wrap_in_schema ({"someMutation" : some_mutation })
119
70
source = """
120
71
mutation {
121
- simpleMutation (input: {clientMutationId: "abc"}) {
72
+ someMutation (input: {clientMutationId: "abc"}) {
122
73
result
123
74
clientMutationId
124
75
}
125
76
}
126
77
"""
127
78
assert graphql_sync (schema , source ) == (
128
- {"simpleMutation " : {"result" : 1 , "clientMutationId" : "abc" }},
79
+ {"someMutation " : {"result" : 1 , "clientMutationId" : "abc" }},
129
80
None ,
130
81
)
131
82
132
83
def supports_thunks_as_input_and_output_fields ():
84
+ some_mutation = mutation_with_client_mutation_id (
85
+ "SomeMutation" ,
86
+ {"inputData" : GraphQLInputField (GraphQLInt )},
87
+ {"result" : GraphQLField (GraphQLInt )},
88
+ dummy_resolve ,
89
+ )
90
+ schema = wrap_in_schema ({"someMutation" : some_mutation })
133
91
source = """
134
92
mutation {
135
- simpleMutationWithThunkFields(
136
- input: {inputData: 1234, clientMutationId: "abc"}) {
93
+ someMutation(input: {inputData: 1234, clientMutationId: "abc"}) {
137
94
result
138
95
clientMutationId
139
96
}
140
97
}
141
98
"""
142
99
assert graphql_sync (schema , source ) == (
143
100
{
144
- "simpleMutationWithThunkFields " : {
101
+ "someMutation " : {
145
102
"result" : 1234 ,
146
103
"clientMutationId" : "abc" ,
147
104
}
@@ -151,48 +108,102 @@ def supports_thunks_as_input_and_output_fields():
151
108
152
109
@mark .asyncio
153
110
async def supports_async_mutations ():
111
+ some_mutation = mutation_with_client_mutation_id (
112
+ "SomeMutation" ,
113
+ {},
114
+ {"result" : GraphQLField (GraphQLInt )},
115
+ dummy_resolve_async ,
116
+ )
117
+ schema = wrap_in_schema ({"someMutation" : some_mutation })
154
118
source = """
155
119
mutation {
156
- simpleAsyncMutation (input: {clientMutationId: "abc"}) {
120
+ someMutation (input: {clientMutationId: "abc"}) {
157
121
result
158
122
clientMutationId
159
123
}
160
124
}
161
125
"""
162
126
assert await graphql (schema , source ) == (
163
- {"simpleAsyncMutation " : {"result" : 1 , "clientMutationId" : "abc" }},
127
+ {"someMutation " : {"result" : 1 , "clientMutationId" : "abc" }},
164
128
None ,
165
129
)
166
130
167
131
def can_access_root_value ():
132
+ some_mutation = mutation_with_client_mutation_id (
133
+ "SomeMutation" ,
134
+ {},
135
+ {"result" : GraphQLField (GraphQLInt )},
136
+ lambda info , clientMutationId = None : Result (
137
+ info .root_value , clientMutationId
138
+ ),
139
+ )
140
+ schema = wrap_in_schema ({"someMutation" : some_mutation })
168
141
source = """
169
142
mutation {
170
- simpleRootValueMutation (input: {clientMutationId: "abc"}) {
143
+ someMutation (input: {clientMutationId: "abc"}) {
171
144
result
172
145
clientMutationId
173
146
}
174
147
}
175
148
"""
176
- assert graphql_sync (schema , source , root_value = Result ( 1 ) ) == (
177
- {"simpleRootValueMutation " : {"result" : 1 , "clientMutationId" : "abc" }},
149
+ assert graphql_sync (schema , source , root_value = 1 ) == (
150
+ {"someMutation " : {"result" : 1 , "clientMutationId" : "abc" }},
178
151
None ,
179
152
)
180
153
181
154
def supports_mutations_returning_null ():
155
+ some_mutation = mutation_with_client_mutation_id (
156
+ "SomeMutation" ,
157
+ {},
158
+ {"result" : GraphQLField (GraphQLInt )},
159
+ lambda _info , ** _input : None ,
160
+ )
161
+ schema = wrap_in_schema ({"someMutation" : some_mutation })
182
162
source = """
183
163
mutation {
184
- simpleRootValueMutation (input: {clientMutationId: "abc"}) {
164
+ someMutation (input: {clientMutationId: "abc"}) {
185
165
result
186
166
clientMutationId
187
167
}
188
168
}
189
169
"""
190
- assert graphql_sync (schema , source , root_value = None ) == (
191
- {"simpleRootValueMutation " : {"result" : None , "clientMutationId" : "abc" }},
170
+ assert graphql_sync (schema , source ) == (
171
+ {"someMutation " : {"result" : None , "clientMutationId" : "abc" }},
192
172
None ,
193
173
)
194
174
195
175
def describe_introspection ():
176
+ simple_mutation = mutation_with_client_mutation_id (
177
+ "SimpleMutation" ,
178
+ input_fields = {},
179
+ output_fields = {"result" : GraphQLField (GraphQLInt )},
180
+ mutate_and_get_payload = dummy_resolve ,
181
+ )
182
+
183
+ simple_mutation_with_description = mutation_with_client_mutation_id (
184
+ "SimpleMutationWithDescription" ,
185
+ description = "Simple Mutation Description" ,
186
+ input_fields = {},
187
+ output_fields = {"result" : GraphQLField (GraphQLInt )},
188
+ mutate_and_get_payload = dummy_resolve ,
189
+ )
190
+
191
+ simple_mutation_with_deprecation_reason = mutation_with_client_mutation_id (
192
+ "SimpleMutationWithDeprecationReason" ,
193
+ input_fields = {},
194
+ output_fields = {"result" : GraphQLField (GraphQLInt )},
195
+ mutate_and_get_payload = dummy_resolve ,
196
+ deprecation_reason = "Just because" ,
197
+ )
198
+
199
+ schema = wrap_in_schema (
200
+ {
201
+ "simpleMutation" : simple_mutation ,
202
+ "simpleMutationWithDescription" : simple_mutation_with_description ,
203
+ "simpleMutationWithDeprecationReason" : simple_mutation_with_deprecation_reason , # noqa: E501
204
+ }
205
+ )
206
+
196
207
def contains_correct_input ():
197
208
source = """
198
209
{
@@ -334,67 +345,6 @@ def contains_correct_field():
334
345
"kind" : "OBJECT" ,
335
346
},
336
347
},
337
- {
338
- "name" : "simpleMutationWithThunkFields" ,
339
- "args" : [
340
- {
341
- "name" : "input" ,
342
- "type" : {
343
- "name" : None ,
344
- "kind" : "NON_NULL" ,
345
- "ofType" : {
346
- "name" : "SimpleMutation"
347
- "WithThunkFieldsInput" ,
348
- "kind" : "INPUT_OBJECT" ,
349
- },
350
- },
351
- }
352
- ],
353
- "type" : {
354
- "name" : "SimpleMutationWithThunkFieldsPayload" ,
355
- "kind" : "OBJECT" ,
356
- },
357
- },
358
- {
359
- "name" : "simpleAsyncMutation" ,
360
- "args" : [
361
- {
362
- "name" : "input" ,
363
- "type" : {
364
- "name" : None ,
365
- "kind" : "NON_NULL" ,
366
- "ofType" : {
367
- "name" : "SimpleAsyncMutationInput" ,
368
- "kind" : "INPUT_OBJECT" ,
369
- },
370
- },
371
- }
372
- ],
373
- "type" : {
374
- "name" : "SimpleAsyncMutationPayload" ,
375
- "kind" : "OBJECT" ,
376
- },
377
- },
378
- {
379
- "name" : "simpleRootValueMutation" ,
380
- "args" : [
381
- {
382
- "name" : "input" ,
383
- "type" : {
384
- "name" : None ,
385
- "kind" : "NON_NULL" ,
386
- "ofType" : {
387
- "name" : "SimpleRootValueMutationInput" , # noqa: E501
388
- "kind" : "INPUT_OBJECT" ,
389
- },
390
- },
391
- }
392
- ],
393
- "type" : {
394
- "name" : "SimpleRootValueMutationPayload" ,
395
- "kind" : "OBJECT" ,
396
- },
397
- },
398
348
]
399
349
}
400
350
}
@@ -425,15 +375,6 @@ def contains_correct_descriptions():
425
375
"name" : "simpleMutationWithDescription" ,
426
376
"description" : "Simple Mutation Description" ,
427
377
},
428
- {
429
- "name" : "simpleMutationWithThunkFields" ,
430
- "description" : None ,
431
- },
432
- {"name" : "simpleAsyncMutation" , "description" : None },
433
- {
434
- "name" : "simpleRootValueMutation" ,
435
- "description" : None ,
436
- },
437
378
]
438
379
}
439
380
}
@@ -475,21 +416,6 @@ def contains_correct_deprecation_reason():
475
416
"isDeprecated" : True ,
476
417
"deprecationReason" : "Just because" ,
477
418
},
478
- {
479
- "name" : "simpleMutationWithThunkFields" ,
480
- "isDeprecated" : False ,
481
- "deprecationReason" : None ,
482
- },
483
- {
484
- "name" : "simpleAsyncMutation" ,
485
- "isDeprecated" : False ,
486
- "deprecationReason" : None ,
487
- },
488
- {
489
- "name" : "simpleRootValueMutation" ,
490
- "isDeprecated" : False ,
491
- "deprecationReason" : None ,
492
- },
493
419
]
494
420
}
495
421
}
0 commit comments