Skip to content

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

Fix using search argument with sorting by RANK (#13617)

SzymJ421cddd36cf8
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 94105c30d9..6d7c8c1e1f 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -148,7 +148,9 @@ from .types import (
def search_string_in_kwargs(kwargs: dict) -> bool:
- return bool(kwargs.get("filter", {}).get("search", "").strip())
+ return bool(kwargs.get("filter", {}).get("search", "").strip()) or bool(
+ kwargs.get("search", "").strip()
+ )
def sort_field_from_kwargs(kwargs: dict) -> Optional[List[str]]:
@@ -448,14 +450,15 @@ class ProductQueries(graphene.ObjectType):
@staticmethod
@traced_resolver
- def resolve_products(
- _root, info: ResolveInfo, *, channel=None, search=None, **kwargs
- ):
+ def resolve_products(_root, info: ResolveInfo, *, channel=None, **kwargs):
if sort_field_from_kwargs(kwargs) == ProductOrderField.RANK:
# sort by RANK can be used only with search filter
if not search_string_in_kwargs(kwargs):
raise GraphQLError(
- "Sorting by RANK is available only when using a search filter."
+ (
+ "Sorting by RANK is available only when using a search filter "
+ "or search argument."
+ )
)
if search_string_in_kwargs(kwargs) and not sort_field_from_kwargs(kwargs):
# default to sorting by RANK if search is used
@@ -465,6 +468,7 @@ class ProductQueries(graphene.ObjectType):
{"direction": "-", "field": ["search_rank", "id"]}
)
+ search = kwargs.get("search")
requestor = get_user_or_app_from_context(info.context)
has_required_permissions = has_one_of_permissions(
requestor, ALL_PRODUCTS_PERMISSIONS
diff --git a/saleor/graphql/product/tests/queries/test_products_query.py b/saleor/graphql/product/tests/queries/test_products_query.py
index 90d0d98f69..e7622c9c95 100644
--- a/saleor/graphql/product/tests/queries/test_products_query.py
+++ b/saleor/graphql/product/tests/queries/test_products_query.py
@@ -861,11 +861,12 @@ def test_sort_product_by_rank_without_search(
}
response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)
content = get_graphql_content(response, ignore_errors=True)
- assert "errors" in content
- assert (
- content["errors"][0]["message"]
- == "Sorting by RANK is available only when using a search filter."
+ message = (
+ "Sorting by RANK is available only when using a search filter or search "
+ "argument."
)
+ assert "errors" in content
+ assert content["errors"][0]["message"] == message
def test_search_product_by_description_and_name_without_sort_by(
@@ -1177,3 +1178,41 @@ def test_search_products_on_root_level(
assert len(nodes) == len(indexes)
returned_attrs = {node["node"]["slug"] for node in nodes}
assert returned_attrs == {product_list[index].slug for index in indexes}
+
+
+def test_search_product_using_search_argument_with_sort_by(
+ user_api_client, product_list, product, channel_USD
+):
+ product.description_plaintext = "new big new product"
+
+ product_2 = product_list[1]
+ product_2.name = "new product"
+ product_1 = product_list[0]
+ product_1.description_plaintext = "some new product"
+
+ product_list.append(product)
+ for prod in product_list:
+ prod.search_vector = FlatConcatSearchVector(
+ *prepare_product_search_vector_value(prod)
+ )
+
+ Product.objects.bulk_update(
+ product_list,
+ ["search_vector", "name", "description_plaintext"],
+ )
+
+ variables = {
+ "search": "new",
+ "sortBy": {"field": "RANK", "direction": "DESC"},
+ "channel": channel_USD.slug,
+ }
+ response = user_api_client.post_graphql(PRODUCT_SEARCH_QUERY, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["products"]["edges"]
+
+ assert len(data) == 3
+ assert {node["node"]["name"] for node in data} == {
+ product.name,
+ product_1.name,
+ product_2.name,
+ }