Skip to content

check for async callable object and function for relay resolver #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions ariadne/contrib/relay/objects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Awaitable
from inspect import iscoroutinefunction
from typing import Optional, cast

Expand All @@ -6,6 +7,7 @@
from graphql.type import GraphQLSchema

from ariadne import InterfaceType, ObjectType
from ariadne.contrib.relay import RelayConnection
from ariadne.contrib.relay.arguments import (
ConnectionArguments,
ConnectionArgumentsTypeUnion,
Expand All @@ -16,7 +18,7 @@
)
from ariadne.contrib.relay.utils import decode_global_id
from ariadne.types import Resolver
from ariadne.utils import type_get_extension, type_set_extension
from ariadne.utils import is_async_callable, type_get_extension, type_set_extension


class RelayObjectType(ObjectType):
Expand All @@ -33,14 +35,16 @@ def __init__(
def resolve_wrapper(self, resolver: ConnectionResolver):
def wrapper(obj, info, *args, **kwargs):
connection_arguments = self.connection_arguments_class(**kwargs)
if iscoroutinefunction(resolver):
if is_async_callable(resolver):

async def async_my_extension():
relay_connection = await resolver(
relay_connection = resolver(
obj, info, connection_arguments, *args, **kwargs
)
if is_awaitable(relay_connection):
relay_connection = await relay_connection
relay_connection = await cast(
Awaitable[RelayConnection], relay_connection
)
return {
"totalCount": relay_connection.total,
"edges": relay_connection.get_edges(),
Expand Down
7 changes: 7 additions & 0 deletions ariadne/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
from collections.abc import Mapping
from functools import wraps
from typing import Any, Callable, Optional, Union, cast
Expand Down Expand Up @@ -264,3 +265,9 @@ def type_get_extension(
object_type: GraphQLNamedType, extension_name: str, fallback: Any = None
) -> Any:
return getattr(object_type, "extensions", {}).get(extension_name, fallback)


def is_async_callable(obj: Any) -> bool:
return inspect.iscoroutinefunction(obj) or (
callable(obj) and inspect.iscoroutinefunction(obj.__call__)
)
25 changes: 25 additions & 0 deletions tests/relay/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,28 @@ def test_relay_node_query_faction(

assert result.errors is None
assert result.data == {"node": {"bid": "RmFjdGlvbjoy", "name": "Galactic Empire"}}


@pytest.mark.asyncio
async def test_relay_object_resolve_wrapper_awaitable(friends_connection):
class Resolver:
async def __call__(self, *_, **__):
return friends_connection

object_type = RelayObjectType("User")
wrapped_resolver = object_type.resolve_wrapper(Resolver())

result = await wrapped_resolver(None, None, first=10)
assert result == {
"totalCount": 2,
"edges": [
{"node": {"id": "VXNlcjox", "name": "Alice"}, "cursor": "VXNlcjox"},
{"node": {"id": "VXNlcjoy", "name": "Bob"}, "cursor": "VXNlcjoy"},
],
"pageInfo": {
"hasNextPage": False,
"hasPreviousPage": False,
"startCursor": "VXNlcjox",
"endCursor": "VXNlcjoy",
},
}