handle connections without middleware

This commit is contained in:
Josh Warwick 2023-05-16 16:07:49 +01:00
parent 8c068fbc2b
commit d3f8fcf906
2 changed files with 22 additions and 14 deletions

View File

@ -104,16 +104,6 @@ class DjangoSyncRequiredMiddleware:
):
return sync_to_async(next)(root, info, **args)
## We can move this resolver logic into the field resolver itself and probably should
if hasattr(return_type, "graphene_type"):
if hasattr(return_type.graphene_type, "Edge"):
node_type = return_type.graphene_type.Edge.node.type
if hasattr(node_type, "_meta") and hasattr(node_type._meta, "model"):
if not inspect.iscoroutinefunction(
next
) and not inspect.isasyncgenfunction(next):
return sync_to_async(next)(root, info, **args)
if info.parent_type.name == "Mutation":
if not inspect.iscoroutinefunction(next) and not inspect.isasyncgenfunction(
next

View File

@ -270,6 +270,17 @@ class DjangoConnectionField(ConnectionField):
# eventually leads to DjangoObjectType's get_queryset (accepts queryset)
# or a resolve_foo (does not accept queryset)
try:
get_running_loop()
except RuntimeError:
pass
else:
if not inspect.iscoroutinefunction(
resolver
) and not inspect.isasyncgenfunction(resolver):
resolver = sync_to_async(resolver)
iterable = resolver(root, info, **args)
if info.is_awaitable(iterable):
@ -293,17 +304,24 @@ class DjangoConnectionField(ConnectionField):
iterable = default_manager
# thus the iterable gets refiltered by resolve_queryset
# but iterable might be promise
iterable = queryset_resolver(connection, iterable, info, args)
try:
get_running_loop()
except RuntimeError:
pass
else:
return sync_to_async(cls.resolve_connection)(
async def perform_resolve(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
)
return perform_resolve(iterable)
iterable = queryset_resolver(connection, iterable, info, args)
return cls.resolve_connection(connection, args, iterable, max_limit=max_limit)
def wrap_resolve(self, parent_resolver):