Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix cursor in product search results (#8158)
Rafał Pitońbc6895dbb32e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex eceb1449af..5267758999 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,7 @@ All notable, unreleased changes to this project will be documented in this file.- Possibility to pass metadata in input of `checkoutPaymentCreate` - #8076 by @mateuszgrzyb- Fix shipping address issue in `availableCollectionPoints` resolver for checkout - #8143 by @kuchichan- Improve draft orders and orders webhooks by @jakubkuc+- Fix cursor-based pagination in products search - #8011 by @rafalp# 3.0.0 [Unreleased]diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.pyindex 23e1d844f4..d86a4dae83 100644--- a/saleor/graphql/core/connection.py+++ b/saleor/graphql/core/connection.py@@ -1,4 +1,5 @@import json+from decimal import Decimal, InvalidOperationfrom typing import Any, Dict, Iterable, List, Tuple, Unionimport graphene@@ -14,6 +15,8 @@ from ..core.enums import OrderDirectionConnectionArguments = Dict[str, Any]+EPSILON = Decimal("0.000001")+def to_global_cursor(values):if not isinstance(values, Iterable):@@ -39,6 +42,28 @@ def get_field_value(instance: DjangoModel, field_name: str):return attr+def _prepare_filter_by_rank_expression(+ cursor: List[str],+ sorting_direction: str,+) -> Q:+ try:+ rank = Decimal(cursor[0])+ int(cursor[1])+ except (InvalidOperation, ValueError, TypeError, KeyError):+ raise ValueError("Invalid cursor for sorting by rank.")++ # Because rank is float number, it gets mangled by PostgreSQL's query parser+ # making equal comparisons impossible. Instead we compare rank against small+ # range of values, constructed using epsilon.+ if sorting_direction == "gt":+ return Q(rank__range=(rank - EPSILON, rank + EPSILON), id__lt=cursor[1]) | Q(+ rank__gt=rank + EPSILON+ )+ return Q(rank__range=(rank - EPSILON, rank + EPSILON), id__gt=cursor[1]) | Q(+ rank__lt=rank - EPSILON+ )++def _prepare_filter_expression(field_name: str,index: int,@@ -80,6 +105,10 @@ def _prepare_filter(('first_field', 'first_value_form_cursor')))"""+ if sorting_fields == ["rank", "id"]:+ # Fast path for filtering by rank+ return _prepare_filter_by_rank_expression(cursor, sorting_direction)+filter_kwargs = Q()for index, field_name in enumerate(sorting_fields):if cursor[index] is None and sorting_direction == "gt":diff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.pyindex 5e98d27f6e..317d9c5fe9 100644--- a/saleor/graphql/product/sorters.py+++ b/saleor/graphql/product/sorters.py@@ -123,7 +123,7 @@ class CollectionSortingInput(ChannelSortInputObjectType):class ProductOrderField(graphene.Enum):NAME = ["name", "slug"]- RANK = ["rank"]+ RANK = ["rank", "id"]PRICE = ["min_variants_price_amount", "name", "slug"]MINIMAL_PRICE = ["discounted_price_amount", "name", "slug"]DATE = ["updated_at", "name", "slug"]diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex 6aca5ad8f1..c2e8e66d54 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -3749,14 +3749,24 @@ def test_create_product_with_boolean_attribute(SEARCH_PRODUCTS_QUERY = """query Products(- $filters: ProductFilterInput, $sortBy: ProductOrder, $channel: String+ $filters: ProductFilterInput,+ $sortBy: ProductOrder,+ $channel: String,+ $after: String,) {- products(first: 5, filter: $filters, sortBy: $sortBy, channel: $channel) {+ products(+ first: 5,+ filter: $filters,+ sortBy: $sortBy,+ channel: $channel,+ after: $after,+ ) {edges {node {idname}+ cursor}}}@@ -3900,6 +3910,44 @@ def test_search_product_by_description_and_name_without_sort_by(assert data[2]["node"]["name"] == product_1.name+def test_search_product_by_description_and_name_and_use_cursor(+ user_api_client, product_list, product, channel_USD, category, product_type+):+ product.description_plaintext = "red big red product"+ product.save()++ product_2 = product_list[1]+ product_2.name = "red product"+ product_2.save()+ product_1 = product_list[0]+ product_1.description_plaintext = "some red product"+ product_1.save()++ variables = {+ "filters": {+ "search": "red",+ },+ "channel": channel_USD.slug,+ }+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)+ content = get_graphql_content(response)+ cursor = content["data"]["products"]["edges"][0]["cursor"]++ variables = {+ "filters": {+ "search": "red",+ },+ "after": cursor,+ "channel": channel_USD.slug,+ }+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)+ content = get_graphql_content(response)+ data = content["data"]["products"]["edges"]++ assert data[0]["node"]["name"] == product.name+ assert data[1]["node"]["name"] == product_1.name++@freeze_time("2020-03-18 12:00:00")def test_create_product_with_rating(staff_api_client,diff --git a/saleor/graphql/utils/sorting.py b/saleor/graphql/utils/sorting.pyindex 8b981687f4..ec1c92ed94 100644--- a/saleor/graphql/utils/sorting.py+++ b/saleor/graphql/utils/sorting.py@@ -37,7 +37,7 @@ def sort_queryset_for_connection(iterable, args):or get_default_channel_slug_or_graphql_error(),)elif query and "-rank" in query.order_by:- return iterable, {"field": "rank", "direction": "-"}+ return iterable, {"field": ["rank", "id"], "direction": "-"}else:iterable, sort_by = sort_queryset_by_default(queryset=iterable, reversed=reversed@@ -72,7 +72,6 @@ def sort_queryset(sorting_field = sort_by.fieldsorting_attribute = getattr(sort_by, "attribute_id", None)-if sorting_field is not None and sorting_attribute is not None:raise GraphQLError("You must provide either `field` or `attributeId` to sort the products."@@ -90,8 +89,15 @@ def sort_queryset(if custom_sort_by:queryset = custom_sort_by(queryset, channel_slug=channel_slug)- sorting_field_value = sorting_fields.value- sorting_list = [f"{sorting_direction}{field}" for field in sorting_field_value]+ if sorting_field_name == "rank":+ # In rank sorting ID is sorted in opposite direction to rank+ if sorting_direction == "-":+ sorting_list = ["-rank", "id"]+ else:+ sorting_list = ["rank", "-id"]+ else:+ sorting_field_value = sorting_fields.value+ sorting_list = [f"{sorting_direction}{field}" for field in sorting_field_value]return queryset.order_by(*sorting_list)