Skip to content
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

fix(relay): add missing key in return RelayObjectType.resolve_wrapper #1224

Merged
merged 1 commit into from
Feb 3, 2025
Merged
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
4 changes: 2 additions & 2 deletions ariadne/contrib/relay/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def get_page_info(
return {
"hasNextPage": self.has_next_page,
"hasPreviousPage": self.has_previous_page,
"startCursor": self.get_cursor(self.edges[0]),
"endCursor": self.get_cursor(self.edges[-1]),
"startCursor": self.get_cursor(self.edges[0]) if self.edges else None,
"endCursor": self.get_cursor(self.edges[-1]) if self.edges else None,
}

def get_edges(self):
Expand Down
2 changes: 2 additions & 0 deletions ariadne/contrib/relay/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async def async_my_extension():
if is_awaitable(relay_connection):
relay_connection = await relay_connection
return {
"totalCount": relay_connection.total,
"edges": relay_connection.get_edges(),
"pageInfo": relay_connection.get_page_info(
connection_arguments
Expand All @@ -57,6 +58,7 @@ async def async_my_extension():
obj, info, connection_arguments, *args, **kwargs
)
return {
"totalCount": relay_connection.total,
"edges": relay_connection.get_edges(),
"pageInfo": relay_connection.get_page_info(connection_arguments),
}
Expand Down
41 changes: 39 additions & 2 deletions tests/relay/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

@pytest.fixture
def friends_connection():
edges = [{"id": "VXNlcjox", "name": "Alice"}, {"id": "VXNlcjoy", "name": "Bob"}]
return RelayConnection(
edges=[{"id": "VXNlcjox", "name": "Alice"}, {"id": "VXNlcjoy", "name": "Bob"}],
total=2,
edges=edges,
total=len(edges),
has_next_page=False,
has_previous_page=False,
)
Expand Down Expand Up @@ -117,6 +118,7 @@ def test_relay_object_resolve_wrapper(mocker: MockFixture, friends_connection):

result = 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"},
Expand Down Expand Up @@ -145,6 +147,7 @@ async def 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"},
Expand All @@ -158,6 +161,40 @@ async def resolver(*_, **__):
}


def test_relay_object_resolve_wrapper_without_edges(
mocker: MockFixture, friends_connection
):
edges = []
friends_connection.edges = edges
friends_connection.total = len(edges)
mock_resolver = mocker.Mock(return_value=friends_connection)
mock_connection_arguments = mocker.Mock()
mock_connection_arguments_class = mocker.Mock(
return_value=mock_connection_arguments
)
object_type = RelayObjectType(
"User", connection_arguments_class=mock_connection_arguments_class
)
wrapped_resolver = object_type.resolve_wrapper(mock_resolver)

result = wrapped_resolver(None, None, first=10)
assert result == {
"totalCount": 0,
"edges": [],
"pageInfo": {
"hasNextPage": False,
"hasPreviousPage": False,
"startCursor": None,
"endCursor": None,
},
}

mock_resolver.assert_called_once_with(
None, None, mock_connection_arguments, first=10
)
mock_connection_arguments_class.assert_called_once_with(first=10)


def test_relay_object_resolve_wrapper_with_custom_arguments(mocker: MockFixture):
object_type = RelayObjectType(
"User", connection_arguments_class=ForwardConnectionArguments
Expand Down