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

Merged
merged 2 commits into from
Apr 18, 2025
Merged
Changes from 1 commit
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
Next Next commit
check for async callable object and function for relay resolver
reallistic committed Apr 8, 2025
commit 21765ab4b74c65cf985017c54847d57093340d9c
4 changes: 2 additions & 2 deletions ariadne/contrib/relay/objects.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,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):
@@ -33,7 +33,7 @@ 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(
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
@@ -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
@@ -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",
},
}