Skip to content

Commit 41b8208

Browse files
committed
Avoid null payload in mutation
Replicates graphql/graphql-relay-js@0595d8b
1 parent 1333a26 commit 41b8208

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def resolve_maybe_thunk(thing_or_thunk: Thunk) -> Any:
3131
return thing_or_thunk() if callable(thing_or_thunk) else thing_or_thunk
3232

3333

34+
class NullResult:
35+
def __init__(self, clientMutationId=None):
36+
self.clientMutationId = clientMutationId
37+
38+
3439
def mutation_with_client_mutation_id(
3540
name: str,
3641
input_fields: Thunk[GraphQLInputFieldMap],
@@ -76,12 +81,16 @@ async def resolve(_root, info, input):
7681
if isawaitable(payload):
7782
payload = await payload
7883
try:
79-
payload.clientMutationId = input["clientMutationId"]
84+
clientMutationId = input["clientMutationId"]
8085
except KeyError:
8186
raise GraphQLError(
8287
"Cannot set clientMutationId"
8388
f" in the payload object {inspect(payload)}."
8489
)
90+
if payload is None:
91+
payload = NullResult(clientMutationId)
92+
else:
93+
payload.clientMutationId = clientMutationId
8594
return payload
8695

8796
return GraphQLField(

Diff for: tests/mutation/test_mutation.py

+15
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ async def can_access_root_value():
178178
None,
179179
)
180180

181+
@mark.asyncio
182+
async def supports_mutations_returning_null():
183+
query = """
184+
mutation M {
185+
simpleRootValueMutation(input: {clientMutationId: "abc"}) {
186+
result
187+
clientMutationId
188+
}
189+
}
190+
"""
191+
assert await graphql(schema, query, root_value=None) == (
192+
{"simpleRootValueMutation": {"result": None, "clientMutationId": "abc"}},
193+
None,
194+
)
195+
181196
def describe_introspection():
182197
@mark.asyncio
183198
async def contains_correct_input():

0 commit comments

Comments
 (0)