Skip to content

Commit

Permalink
fix: list connection optimization (#3487)
Browse files Browse the repository at this point in the history
  • Loading branch information
euriostigue committed May 9, 2024
1 parent 17f05a7 commit 9f332a1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Release type: patch

This release fixes a bug in release 0.227.3 where FragmentSpread nodes
were not resolving edges.
27 changes: 22 additions & 5 deletions strawberry/relay/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing_extensions import assert_never

from strawberry.types.info import Info
from strawberry.types.nodes import InlineFragment
from strawberry.types.nodes import InlineFragment, Selection
from strawberry.types.types import StrawberryObjectDefinition


Expand Down Expand Up @@ -77,11 +77,28 @@ def should_resolve_list_connection_edges(info: Info) -> bool:
"""
resolve_for_field_names = {"edges", "pageInfo"}

def _check_selection(selection: Selection) -> bool:
"""Recursively inspect the selection to check if the user requested to resolve the `edges` field.
Args:
selection (Selection): The selection to check.
Returns:
bool: True if the user requested to resolve the `edges` field of a connection, False otherwise.
"""
if (
not isinstance(selection, InlineFragment)
and selection.name in resolve_for_field_names
):
return True
if selection.selections:
return any(
_check_selection(selection) for selection in selection.selections
)
return False

for selection_field in info.selected_fields:
for selection in selection_field.selections:
if (
not isinstance(selection, InlineFragment)
and selection.name in resolve_for_field_names
):
if _check_selection(selection):
return True
return False
32 changes: 32 additions & 0 deletions tests/relay/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,35 @@ class Query:
"totalCount": -1,
}
}


def test_list_connection_with_nested_fragments():
ret = schema.execute_sync(
"""
query {
fruits {
...FruitFragment
}
}
fragment FruitFragment on FruitConnection {
edges {
node {
id
}
}
}
"""
)
assert ret.errors is None
assert ret.data == {
"fruits": {
"edges": [
{"node": {"id": "RnJ1aXQ6MQ=="}},
{"node": {"id": "RnJ1aXQ6Mg=="}},
{"node": {"id": "RnJ1aXQ6Mw=="}},
{"node": {"id": "RnJ1aXQ6NA=="}},
{"node": {"id": "RnJ1aXQ6NQ=="}},
]
}
}

0 comments on commit 9f332a1

Please sign in to comment.