Skip to content

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

add sorting by `created_at` field to `products` query (#10900)

Piotr Zabieglik44297d9cfa47
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2f0ed382eb..63cdfd3647 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add `User.checkouts` field. #10862 by @zedzior
- Add optional field `audience` to mutation `tokenCreate`. If provided, the created tokens will have key `aud` with value: `custom:{audience-input-value}` - #10845 by @korycins
- Use `AttributeValue.name` instead of `AttributeValue.slug` to determine uniqueness of a value instance for dropdown and multiselect attributes. - #10881 by @jakubkuc
+- Allow sorting products by `CREATED_AT` field. #10900 by @zedzior
- Add ability to pass metadata directly in create/update mutations for product app models - #10689 by @SzymJ
### Other changes
diff --git a/saleor/graphql/product/sorters.py b/saleor/graphql/product/sorters.py
index 5360c0695b..266240b2c6 100644
--- a/saleor/graphql/product/sorters.py
+++ b/saleor/graphql/product/sorters.py
@@ -21,7 +21,7 @@ from ...product.models import (
Product,
ProductChannelListing,
)
-from ..core.descriptions import CHANNEL_REQUIRED, DEPRECATED_IN_3X_INPUT
+from ..core.descriptions import ADDED_IN_38, CHANNEL_REQUIRED, DEPRECATED_IN_3X_INPUT
from ..core.types import ChannelSortInputObjectType, SortInputObjectType
@@ -145,6 +145,7 @@ class ProductOrderField(graphene.Enum):
LAST_MODIFIED_AT = ["updated_at", "name", "slug"]
COLLECTION = ["collectionproduct__sort_order", "pk"]
RATING = ["rating", "name", "slug"]
+ CREATED_AT = ["created_at", "name", "slug"]
@property
def description(self):
@@ -179,6 +180,7 @@ class ProductOrderField(graphene.Enum):
),
ProductOrderField.LAST_MODIFIED_AT.name: "update date.",
ProductOrderField.RATING.name: "rating.",
+ ProductOrderField.CREATED_AT.name: "creation date." + ADDED_IN_38,
}
if self.name in descriptions:
return f"Sort products by {descriptions[self.name]}"
diff --git a/saleor/graphql/product/tests/test_product_sorting.py b/saleor/graphql/product/tests/test_product_sorting.py
index e7204d56d0..ddafda9557 100644
--- a/saleor/graphql/product/tests/test_product_sorting.py
+++ b/saleor/graphql/product/tests/test_product_sorting.py
@@ -152,6 +152,7 @@ query Products($sortBy: ProductOrder, $channel: String) {
edges {
node {
id
+ created
}
}
}
@@ -201,6 +202,27 @@ def test_sort_products_by_published_at(
]
[email protected]("direction", ("ASC", "DESC"))
+def test_sort_products_by_created_at(direction, api_client, product_list, channel_USD):
+ variables = {
+ "sortBy": {
+ "direction": direction,
+ "field": "CREATED_AT",
+ },
+ "channel": channel_USD.slug,
+ }
+
+ response = api_client.post_graphql(GET_SORTED_PRODUCTS_QUERY, variables)
+
+ content = get_graphql_content(response)
+ creation_dates = [
+ p["node"]["created"] for p in content["data"]["products"]["edges"]
+ ]
+ if direction == "DESC":
+ creation_dates.reverse()
+ assert creation_dates[0] < creation_dates[1] < creation_dates[2]
+
+
@pytest.mark.parametrize(
"direction, order_direction",
(("ASC", "rating"), ("DESC", "-rating")),
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index c4a50b41e3..0795d9b39f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -6179,6 +6179,13 @@ enum ProductOrderField {
"""Sort products by rating."""
RATING
+
+ """
+ Sort products by creation date.
+
+ Added in Saleor 3.8.
+ """
+ CREATED_AT
}
type CollectionTranslation implements Node {