From 1b2d5e02e42849c65432425a23026e61b893aaa6 Mon Sep 17 00:00:00 2001 From: Josh Warwick Date: Fri, 31 Mar 2023 14:04:37 -0700 Subject: [PATCH] handle async serlizer mutations --- graphene_django/rest_framework/mutation.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 4062a44..8cdd470 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -2,6 +2,8 @@ from collections import OrderedDict from django.shortcuts import get_object_or_404 from rest_framework import serializers +from asyncio import get_running_loop +from asgiref.sync import sync_to_async import graphene from graphene.relay.mutation import ClientIDMutation @@ -152,6 +154,19 @@ class SerializerMutation(ClientIDMutation): kwargs = cls.get_serializer_kwargs(root, info, **input) serializer = cls._meta.serializer_class(**kwargs) + try: + get_running_loop() + except RuntimeError: + pass + else: + async def perform_mutate_async(): + if await sync_to_async(serializer.is_valid)(): + return await sync_to_async(cls.perform_mutate)(serializer, info) + else: + errors = ErrorType.from_errors(serializer.errors) + return cls(errors=errors) + return perform_mutate_async() + if serializer.is_valid(): return cls.perform_mutate(serializer, info) else: