Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Reject products sortBy with neither field nor attributeId (#19798)
Lukasz Ostrowskibb75f87973ab
diff --git a/saleor/graphql/product/tests/test_product_sorting_attributes.py b/saleor/graphql/product/tests/test_product_sorting_attributes.pyindex b8a5bbdcc2..a66295d75b 100644--- a/saleor/graphql/product/tests/test_product_sorting_attributes.py+++ b/saleor/graphql/product/tests/test_product_sorting_attributes.py@@ -296,6 +296,24 @@ def test_sort_products_cannot_sort_both_by_field_and_by_attribute()+def test_sort_products_without_field_and_attribute(api_client, channel_USD):+ """Test that `sortBy` with only a direction is rejected, not a server error."""+ # given+ query = QUERY_SORT_PRODUCTS_BY_ATTRIBUTE+ variables = {"direction": "DESC", "channel": channel_USD.slug}++ # when+ response = api_client.post_graphql(query, variables)++ # then+ content = get_graphql_content(response, ignore_errors=True)+ errors = content["errors"]+ assert len(errors) == 1+ assert errors[0]["message"] == (+ "You must provide `field` or `attributeId` to sort the products."+ )++# Ordered by the given attribute value, then by the product name.## If the product doesn't have a value, it will be placed at the bottom of the productsdiff --git a/saleor/graphql/utils/sorting.py b/saleor/graphql/utils/sorting.pyindex 0c7ae89f0c..a9a4bde6b6 100644--- a/saleor/graphql/utils/sorting.py+++ b/saleor/graphql/utils/sorting.py@@ -69,14 +69,18 @@ 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."- )if sorting_attribute is not None: # empty string as sorting_attribute is valid+ if sorting_field is not None:+ raise GraphQLError(+ "You must provide either `field` or `attributeId` to sort the products."+ )return _sort_queryset_by_attribute(queryset, sorting_attribute, sorting_direction)+ if sorting_field is None:+ raise GraphQLError(+ "You must provide `field` or `attributeId` to sort the products."+ )sort_enum = sort_by._meta.sort_enumsorting_fields = sort_enum.get(sorting_field)