Skip to content

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

Fix paginations for queries with fragments (#8592)

Iga Karbowiakf9fc69ea22c6
diff --git a/saleor/graphql/core/fields.py b/saleor/graphql/core/fields.py
index 1e692d1018..b6a8089a0e 100644
--- a/saleor/graphql/core/fields.py
+++ b/saleor/graphql/core/fields.py
@@ -5,6 +5,7 @@ import graphene
from graphene.relay import PageInfo
from graphene_django.fields import DjangoConnectionField
from graphql.error import GraphQLError
+from graphql.language.ast import FragmentSpread
from graphql_relay.connection.arrayconnection import connection_from_list_slice
from promise import Promise
@@ -133,12 +134,7 @@ class FilterInputConnectionField(BaseDjangoConnectionField):
info,
**args,
):
- # Disable `enforce_first_or_last` if not querying for `edges`.
- values = [
- field.name.value for field in info.field_asts[0].selection_set.selections
- ]
- if "edges" not in values:
- enforce_first_or_last = False
+ enforce_first_or_last = cls.is_first_or_last_required(info)
first = args.get("first")
last = args.get("last")
@@ -188,6 +184,30 @@ class FilterInputConnectionField(BaseDjangoConnectionField):
return Promise.resolve(iterable).then(on_resolve)
return on_resolve(iterable)
+ @classmethod
+ def is_first_or_last_required(cls, info):
+ """Disable `enforce_first_or_last` if not querying for `edges`."""
+ selections = info.field_asts[0].selection_set.selections
+ values = [field.name.value for field in selections]
+ if "edges" in values:
+ return True
+
+ fragments = [
+ field.name.value
+ for field in selections
+ if isinstance(field, FragmentSpread)
+ ]
+
+ for fragment in fragments:
+ fragment_values = [
+ field.name.value
+ for field in info.fragments[fragment].selection_set.selections
+ ]
+ if "edges" in fragment_values:
+ return True
+
+ return False
+
@classmethod
def filter_iterable(cls, iterable, filterset_class, filters_name, info, **args):
filter_input = args.get(filters_name)
diff --git a/saleor/graphql/core/tests/test_pagination.py b/saleor/graphql/core/tests/test_pagination.py
index 09d513480a..a62a6fdefc 100644
--- a/saleor/graphql/core/tests/test_pagination.py
+++ b/saleor/graphql/core/tests/test_pagination.py
@@ -229,3 +229,64 @@ def test_pagination_invalid_cursor(books):
assert result.errors
assert len(result.errors) == 1
assert str(result.errors[0]) == "Received cursor is invalid."
+
+
+QUERY_PAGINATION_WITH_FRAGMENTS = """
+ fragment BookFragment on BookType {
+ name
+ __typename
+ }
+
+ fragment PageInfoFragment on PageInfo {
+ endCursor
+ hasNextPage
+ hasPreviousPage
+ startCursor
+ __typename
+ }
+
+ fragment BookListFragment on BookTypeCountableConnection {
+ pageInfo {
+ ...PageInfoFragment
+ __typename
+ }
+ edges {
+ cursor
+ node {
+ ...BookFragment
+ __typename
+ }
+ __typename
+ }
+ __typename
+ }
+
+ query BooksPaginationTest($first: Int, $last: Int, $after: String, $before: String){
+ books(first: $first, last: $last, after: $after, before: $before) {
+ ...BookListFragment
+ }
+ }
+"""
+
+
+def test_query_with_pagination_and_fragments(books):
+ page_size = 10
+ variables = {"first": page_size}
+
+ result = schema.execute(QUERY_PAGINATION_WITH_FRAGMENTS, variables=variables)
+
+ assert not result.errors
+ content = result.data
+ assert len(content["books"]["edges"]) == page_size
+
+
+def test_query_with_pagination_and_fragments_no_first_or_last_raises_an_error(books):
+ result = schema.execute(QUERY_PAGINATION_WITH_FRAGMENTS, variables={})
+
+ assert result.errors
+ assert len(result.errors) == 1
+ expected_err_msg = (
+ "You must provide a `first` or `last` value to properly paginate "
+ "the `books` connection."
+ )
+ assert str(result.errors[0]) == expected_err_msg