Skip to content

Commit c5d0407

Browse files
committed
Allow mutations to return mappings
1 parent 58bb9fa commit c5d0407

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

Diff for: src/graphql_relay/mutation/mutation.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Mapping
12
from inspect import iscoroutinefunction
23
from typing import Any, Callable, Dict, Optional
34

@@ -82,7 +83,10 @@ async def resolve(_root: Any, info: GraphQLResolveInfo, input: Dict) -> Any:
8283
clientMutationId = input.get("clientMutationId")
8384
if payload is None:
8485
return NullResult(clientMutationId)
85-
payload.clientMutationId = clientMutationId
86+
if isinstance(payload, Mapping):
87+
payload["clientMutationId"] = clientMutationId # type: ignore
88+
else:
89+
payload.clientMutationId = clientMutationId
8690
return payload
8791

8892
else:
@@ -95,7 +99,10 @@ def resolve( # type: ignore
9599
clientMutationId = input.get("clientMutationId")
96100
if payload is None:
97101
return NullResult(clientMutationId)
98-
payload.clientMutationId = clientMutationId # type: ignore
102+
if isinstance(payload, Mapping):
103+
payload["clientMutationId"] = clientMutationId # type: ignore
104+
else:
105+
payload.clientMutationId = clientMutationId # type: ignore
99106
return payload
100107

101108
return GraphQLField(

Diff for: tests/mutation/test_mutation.py

+57-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def can_access_root_value():
154154
)
155155

156156
def supports_mutations_returning_null():
157-
def null_resolve(_info, **_input) -> None:
157+
def null_resolve(_info, **_input):
158158
return None
159159

160160
some_mutation = mutation_with_client_mutation_id(
@@ -176,7 +176,7 @@ def null_resolve(_info, **_input) -> None:
176176

177177
@mark.asyncio
178178
async def supports_async_mutations_returning_null():
179-
async def null_resolve(_info, **_input) -> None:
179+
async def null_resolve(_info, **_input):
180180
return None
181181

182182
some_mutation = mutation_with_client_mutation_id(
@@ -234,6 +234,61 @@ def resolve(cls, obj, _info):
234234
None,
235235
)
236236

237+
def supports_mutations_returning_mappings():
238+
def dict_mutate(_info, **_input):
239+
return {"some_data": 1}
240+
241+
def dict_resolve(obj, _info):
242+
return obj["some_data"]
243+
244+
some_mutation = mutation_with_client_mutation_id(
245+
"SomeMutation",
246+
{},
247+
{"result": GraphQLField(GraphQLInt, resolve=dict_resolve)},
248+
dict_mutate,
249+
)
250+
schema = wrap_in_schema({"someMutation": some_mutation})
251+
source = """
252+
mutation {
253+
someMutation(input: {clientMutationId: "abc"}) {
254+
result
255+
clientMutationId
256+
}
257+
}
258+
"""
259+
assert graphql_sync(schema, source) == (
260+
{"someMutation": {"result": 1, "clientMutationId": "abc"}},
261+
None,
262+
)
263+
264+
@mark.asyncio
265+
async def supports_async_mutations_returning_mappings():
266+
async def dict_mutate(_info, **_input):
267+
return {"some_data": 1}
268+
269+
async def dict_resolve(obj, _info):
270+
return obj["some_data"]
271+
272+
some_mutation = mutation_with_client_mutation_id(
273+
"SomeMutation",
274+
{},
275+
{"result": GraphQLField(GraphQLInt, resolve=dict_resolve)},
276+
dict_mutate,
277+
)
278+
schema = wrap_in_schema({"someMutation": some_mutation})
279+
source = """
280+
mutation {
281+
someMutation(input: {clientMutationId: "abc"}) {
282+
result
283+
clientMutationId
284+
}
285+
}
286+
"""
287+
assert await graphql(schema, source) == (
288+
{"someMutation": {"result": 1, "clientMutationId": "abc"}},
289+
None,
290+
)
291+
237292
def generates_correct_types():
238293
some_mutation = mutation_with_client_mutation_id(
239294
"SomeMutation",

0 commit comments

Comments
 (0)