From b69476f50f0442c892a826329317f787a144b20b Mon Sep 17 00:00:00 2001 From: Josh Warwick Date: Tue, 9 May 2023 22:58:15 +0100 Subject: [PATCH] improve async detection --- graphene_django/debug/middleware.py | 8 ++++++-- graphene_django/fields.py | 7 +++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/graphene_django/debug/middleware.py b/graphene_django/debug/middleware.py index 40a951a..74366dd 100644 --- a/graphene_django/debug/middleware.py +++ b/graphene_django/debug/middleware.py @@ -80,7 +80,9 @@ class DjangoSyncRequiredMiddleware: if hasattr(parent_type, "graphene_type") and hasattr( parent_type.graphene_type._meta, "model" ): - if not inspect.iscoroutinefunction(next): + if not inspect.iscoroutinefunction(next) and not inspect.isasyncgenfunction( + next + ): return sync_to_async(next)(root, info, **args) ## In addition, if we're resolving to a DjangoObject type @@ -88,7 +90,9 @@ class DjangoSyncRequiredMiddleware: if hasattr(info.return_type, "graphene_type") and hasattr( info.return_type.graphene_type._meta, "model" ): - if not info.is_awaitable(next): + if not inspect.iscoroutinefunction(next) and not inspect.isasyncgenfunction( + next + ): return sync_to_async(next)(root, info, **args) return next(root, info, **args) diff --git a/graphene_django/fields.py b/graphene_django/fields.py index 99e84c7..43225c2 100644 --- a/graphene_django/fields.py +++ b/graphene_django/fields.py @@ -266,11 +266,10 @@ class DjangoConnectionField(ConnectionField): iterable = await iterable if iterable is None: iterable = default_manager - ## This could also be async - iterable = queryset_resolver(connection, iterable, info, args) - if info.is_awaitable(iterable): - iterable = await iterable + iterable = await sync_to_async(queryset_resolver)( + connection, iterable, info, args + ) return await sync_to_async(cls.resolve_connection)( connection, args, iterable, max_limit=max_limit