Skip to content

Commit 5590a2e

Browse files
committed
handle async serlizer mutations
1 parent d73d2b5 commit 5590a2e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: graphene_django/rest_framework/mutation.py

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from django.shortcuts import get_object_or_404
44
from rest_framework import serializers
5+
from asyncio import get_running_loop
6+
from asgiref.sync import sync_to_async
57

68
import graphene
79
from graphene.relay.mutation import ClientIDMutation
@@ -153,6 +155,19 @@ def mutate_and_get_payload(cls, root, info, **input):
153155
kwargs = cls.get_serializer_kwargs(root, info, **input)
154156
serializer = cls._meta.serializer_class(**kwargs)
155157

158+
try:
159+
get_running_loop()
160+
except RuntimeError:
161+
pass
162+
else:
163+
async def perform_mutate_async():
164+
if await sync_to_async(serializer.is_valid)():
165+
return await sync_to_async(cls.perform_mutate)(serializer, info)
166+
else:
167+
errors = ErrorType.from_errors(serializer.errors)
168+
return cls(errors=errors)
169+
return perform_mutate_async()
170+
156171
if serializer.is_valid():
157172
return cls.perform_mutate(serializer, info)
158173
else:

0 commit comments

Comments
 (0)