Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix pagination for resolvers operates on lists (#18810)

Iga Karbowiak798632158394
diff --git a/saleor/graphql/account/tests/queries/test_me.py b/saleor/graphql/account/tests/queries/test_me.py
index c23537a74f..cbddc8e867 100644
--- a/saleor/graphql/account/tests/queries/test_me.py
+++ b/saleor/graphql/account/tests/queries/test_me.py
@@ -684,3 +684,46 @@ def test_me_query_stored_payment_methods(
"data": payment_method_data,
}
]
+
+
+ME_ORDERS_PAGINATION_QUERY = """
+ query Me($first: Int, $last: Int, $after: String, $before: String) {
+ me {
+ orders(first: $first, last: $last, after: $after, before: $before) {
+ edges {
+ node {
+ id
+ }
+ }
+ pageInfo {
+ hasNextPage
+ hasPreviousPage
+ startCursor
+ endCursor
+ }
+ totalCount
+ }
+ }
+ }
+"""
+
+
+def test_me_orders_pagination_has_previous_page(user_api_client, order_list):
+ # given - 3 orders exist for the user; fetch first page of 1
+ variables = {"first": 1}
+ response = user_api_client.post_graphql(ME_ORDERS_PAGINATION_QUERY, variables)
+ content = get_graphql_content(response)
+ orders_data = content["data"]["me"]["orders"]
+ end_cursor = orders_data["pageInfo"]["endCursor"]
+ assert orders_data["pageInfo"]["hasPreviousPage"] is False
+ assert orders_data["pageInfo"]["hasNextPage"] is True
+
+ # when - fetch second page using after cursor
+ variables = {"first": 1, "after": end_cursor}
+ response = user_api_client.post_graphql(ME_ORDERS_PAGINATION_QUERY, variables)
+
+ # then - second page should report hasPreviousPage=True
+ content = get_graphql_content(response)
+ page_info = content["data"]["me"]["orders"]["pageInfo"]
+ assert page_info["hasPreviousPage"] is True
+ assert page_info["hasNextPage"] is True
diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.py
index f5fd61802f..844f151e2a 100644
--- a/saleor/graphql/core/connection.py
+++ b/saleor/graphql/core/connection.py
@@ -457,6 +457,24 @@ def slice_connection_iterable(
edge_type=edge_type or connection_type.Edge,
pageinfo_type=pageinfo_type or graphene.relay.PageInfo,
)
+ # This function handles pagination for list-based data (e.g., from dataloaders).
+ # The graphql-relay `connection_from_list_slice` follows the strict Relay
+ # spec where `has_previous_page` is only set when using `last` and
+ # `has_next_page` only when using `first`. Our queryset-based pagination
+ # (`connection_from_queryset_slice` → `_get_page_info`) is more lenient:
+ # it sets `has_previous_page = True` whenever an `after` cursor is present
+ # (forward pagination) and `has_next_page = True` whenever a `before` cursor
+ # is present (backward pagination). Align the list path with the queryset
+ # path so that nested connection fields (e.g., `me { orders }`) behave
+ # consistently with root-level connections.
+ first = args.get("first")
+ last = args.get("last")
+ after = args.get("after")
+ before = args.get("before")
+ if first and after:
+ slice.page_info.has_previous_page = True
+ if last and before:
+ slice.page_info.has_next_page = True
if "total_count" in connection_type._meta.fields:
slice.total_count = _len
diff --git a/saleor/graphql/core/tests/test_pagination.py b/saleor/graphql/core/tests/test_pagination.py
index f17f56956d..3d3e141855 100644
--- a/saleor/graphql/core/tests/test_pagination.py
+++ b/saleor/graphql/core/tests/test_pagination.py
@@ -30,6 +30,20 @@ class Query(graphene.ObjectType):
schema = graphene.Schema(query=Query)
+class ListQuery(graphene.ObjectType):
+ """Query that resolves books from a list (simulates dataloader path)."""
+
+ books = ConnectionField(BookTypeCountableConnection)
+
+ @staticmethod
+ def resolve_books(_root, info, **kwargs):
+ items = [BookType(name=f"Book{i}") for i in range(24)]
+ return create_connection_slice(items, info, kwargs, BookTypeCountableConnection)
+
+
+list_schema = graphene.Schema(query=ListQuery)
+
+
@pytest.fixture
def books(db):
books = [Book(name=f"Book{index}") for index in range(24)]
@@ -345,11 +359,120 @@ QUERY_PAGINATION_WITH_INLINE_FRAGMENTS = """
def test_query_with_pagination_and_inline_fragments(books):
+ # given
page_size = 10
variables = {"first": page_size}
+ # when
result = schema.execute(QUERY_PAGINATION_WITH_INLINE_FRAGMENTS, variables=variables)
+ # then
assert not result.errors
content = result.data
assert len(content["books"]["edges"]) == page_size
+
+
+def test_list_pagination_forward_first_page_info():
+ """First page of forward list pagination has no previous page."""
+ # given
+ variables = {"first": 5}
+
+ # when
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+
+ # then
+ assert not result.errors
+ page_info = result.data["books"]["pageInfo"]
+ assert page_info["hasNextPage"]
+ assert page_info["hasPreviousPage"] is False
+
+
+def test_list_pagination_forward_middle_page_info():
+ """Middle page of forward list pagination has both previous and next pages."""
+ # given
+ variables = {"first": 5}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+ assert not result.errors
+ end_cursor = result.data["books"]["pageInfo"]["endCursor"]
+
+ # when
+ variables = {"first": 5, "after": end_cursor}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+
+ # then
+ assert not result.errors
+ page_info = result.data["books"]["pageInfo"]
+ assert page_info["hasNextPage"]
+ assert page_info["hasPreviousPage"]
+
+
+def test_list_pagination_forward_last_page_info():
+ """Last page of forward list pagination has previous page but no next."""
+ # given
+ variables = {"first": 20}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+ assert not result.errors
+ end_cursor = result.data["books"]["pageInfo"]["endCursor"]
+
+ # when
+ variables = {"first": 20, "after": end_cursor}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+
+ # then
+ assert not result.errors
+ page_info = result.data["books"]["pageInfo"]
+ assert page_info["hasNextPage"] is False
+ assert page_info["hasPreviousPage"]
+
+
+def test_list_pagination_backward_first_page_info():
+ """First page of backward list pagination has previous page (items before)."""
+ # given
+ variables = {"last": 5}
+
+ # when
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+
+ # then
+ assert not result.errors
+ page_info = result.data["books"]["pageInfo"]
+ assert page_info["hasNextPage"] is False
+ assert page_info["hasPreviousPage"]
+
+
+def test_list_pagination_backward_middle_page_info():
+ """Middle page of backward list pagination has both previous and next pages."""
+ # given
+ variables = {"last": 5}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+ assert not result.errors
+ start_cursor = result.data["books"]["pageInfo"]["startCursor"]
+
+ # when
+ variables = {"last": 5, "before": start_cursor}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+
+ # then
+ assert not result.errors
+ page_info = result.data["books"]["pageInfo"]
+ assert page_info["hasNextPage"]
+ assert page_info["hasPreviousPage"]
+
+
+def test_list_pagination_backward_last_page_info():
+ """Last page of backward list pagination has next page but no previous."""
+ # given
+ variables = {"last": 20}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+ assert not result.errors
+ start_cursor = result.data["books"]["pageInfo"]["startCursor"]
+
+ # when
+ variables = {"last": 20, "before": start_cursor}
+ result = list_schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+
+ # then
+ assert not result.errors
+ page_info = result.data["books"]["pageInfo"]
+ assert page_info["hasNextPage"]
+ assert page_info["hasPreviousPage"] is False