Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix paginations for queries with fragments (#8592)
Iga Karbowiakf9fc69ea22c6
diff --git a/saleor/graphql/core/fields.py b/saleor/graphql/core/fields.pyindex 1e692d1018..b6a8089a0e 100644--- a/saleor/graphql/core/fields.py+++ b/saleor/graphql/core/fields.py@@ -5,6 +5,7 @@ import graphenefrom graphene.relay import PageInfofrom graphene_django.fields import DjangoConnectionFieldfrom graphql.error import GraphQLError+from graphql.language.ast import FragmentSpreadfrom graphql_relay.connection.arrayconnection import connection_from_list_slicefrom 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+@classmethoddef 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.pyindex 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.errorsassert len(result.errors) == 1assert 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