Skip to content

Commit 58bb9fa

Browse files
committed
Allow mutations to return custom classes
Replicates graphql/graphql-relay-js@f221f03 We just needed to add the test.
1 parent 3ba3aa2 commit 58bb9fa

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/graphql_relay/mutation/mutation.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ async def resolve(_root: Any, info: GraphQLResolveInfo, input: Dict) -> Any:
8181
payload = await mutate_and_get_payload(info, **input)
8282
clientMutationId = input.get("clientMutationId")
8383
if payload is None:
84-
payload = NullResult(clientMutationId)
85-
else:
86-
payload.clientMutationId = clientMutationId
84+
return NullResult(clientMutationId)
85+
payload.clientMutationId = clientMutationId
8786
return payload
8887

8988
else:
@@ -95,9 +94,8 @@ def resolve( # type: ignore
9594
payload = mutate_and_get_payload(info, **input)
9695
clientMutationId = input.get("clientMutationId")
9796
if payload is None:
98-
payload = NullResult(clientMutationId)
99-
else:
100-
payload.clientMutationId = clientMutationId # type: ignore
97+
return NullResult(clientMutationId)
98+
payload.clientMutationId = clientMutationId # type: ignore
10199
return payload
102100

103101
return GraphQLField(

tests/mutation/test_mutation.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,41 @@ async def null_resolve(_info, **_input) -> None:
199199
None,
200200
)
201201

202+
def supports_mutations_returning_custom_classes():
203+
class SomeClass:
204+
@staticmethod
205+
def get_some_generated_data():
206+
return 1
207+
208+
@classmethod
209+
def mutate(cls, _info, **_input):
210+
return cls()
211+
212+
@classmethod
213+
def resolve(cls, obj, _info):
214+
assert isinstance(obj, cls)
215+
return obj.get_some_generated_data()
216+
217+
some_mutation = mutation_with_client_mutation_id(
218+
"SomeMutation",
219+
{},
220+
{"result": GraphQLField(GraphQLInt, resolve=SomeClass.resolve)},
221+
SomeClass.mutate,
222+
)
223+
schema = wrap_in_schema({"someMutation": some_mutation})
224+
source = """
225+
mutation {
226+
someMutation(input: {clientMutationId: "abc"}) {
227+
result
228+
clientMutationId
229+
}
230+
}
231+
"""
232+
assert graphql_sync(schema, source) == (
233+
{"someMutation": {"result": 1, "clientMutationId": "abc"}},
234+
None,
235+
)
236+
202237
def generates_correct_types():
203238
some_mutation = mutation_with_client_mutation_id(
204239
"SomeMutation",

0 commit comments

Comments
 (0)