Skip to content

Commit 791209f

Browse files
committed
Rejig concept to use middleware
1 parent 58b92e6 commit 791209f

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

graphene_django/converter.py

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from collections import OrderedDict
22
from functools import singledispatch, wraps
3-
from asyncio import get_running_loop
4-
from asgiref.sync import sync_to_async
53

64
from django.db import models
75
from django.utils.encoding import force_str
@@ -267,20 +265,7 @@ def dynamic_type():
267265
if not _type:
268266
return
269267

270-
class CustomField(Field):
271-
def wrap_resolve(self, parent_resolver):
272-
resolver = super().wrap_resolve(parent_resolver)
273-
274-
try:
275-
get_running_loop()
276-
except RuntimeError:
277-
pass
278-
else:
279-
resolver = sync_to_async(resolver)
280-
281-
return resolver
282-
283-
return CustomField(_type, required=not field.null)
268+
return Field(_type, required=not field.null)
284269

285270
return Dynamic(dynamic_type)
286271

@@ -335,20 +320,7 @@ def dynamic_type():
335320
if not _type:
336321
return
337322

338-
class CustomField(Field):
339-
def wrap_resolve(self, parent_resolver):
340-
resolver = super().wrap_resolve(parent_resolver)
341-
342-
try:
343-
get_running_loop()
344-
except RuntimeError:
345-
pass
346-
else:
347-
resolver = sync_to_async(resolver)
348-
349-
return resolver
350-
351-
return CustomField(
323+
return Field(
352324
_type,
353325
description=get_django_field_description(field),
354326
required=not field.null,

graphene_django/debug/middleware.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.db import connections
22

3-
from promise import Promise
4-
3+
from asgiref.sync import sync_to_async
4+
import inspect
55
from .sql.tracking import unwrap_cursor, wrap_cursor
66
from .exception.formating import wrap_exception
77
from .types import DjangoDebug
@@ -69,3 +69,26 @@ def resolve(self, next, root, info, **args):
6969
return context.django_debug.on_resolve_error(e)
7070
context.django_debug.add_result(result)
7171
return result
72+
73+
74+
class DjangoSyncRequiredMiddleware:
75+
def resolve(self, next, root, info, **args):
76+
parent_type = info.parent_type
77+
78+
## Anytime the parent is a DjangoObject type
79+
# and we're resolving a sync field, we need to wrap it in a sync_to_async
80+
if hasattr(parent_type, "graphene_type") and hasattr(
81+
parent_type.graphene_type._meta, "model"
82+
):
83+
if not inspect.iscoroutinefunction(next):
84+
return sync_to_async(next)(root, info, **args)
85+
86+
## In addition, if we're resolving to a DjangoObject type
87+
# we likely need to wrap it in a sync_to_async as well
88+
if hasattr(info.return_type, "graphene_type") and hasattr(
89+
info.return_type.graphene_type._meta, "model"
90+
):
91+
if not info.is_awaitable(next):
92+
return sync_to_async(next)(root, info, **args)
93+
94+
return next(root, info, **args)

0 commit comments

Comments
 (0)