Skip to content

Commit 1b2d5e0

Browse files
committed
handle async serlizer mutations
1 parent 66938e9 commit 1b2d5e0

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

graphene_django/rest_framework/mutation.py

Lines changed: 15 additions & 0 deletions
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
@@ -152,6 +154,19 @@ def mutate_and_get_payload(cls, root, info, **input):
152154
kwargs = cls.get_serializer_kwargs(root, info, **input)
153155
serializer = cls._meta.serializer_class(**kwargs)
154156

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

0 commit comments

Comments
 (0)