Skip to content

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

Allow sorting warehouses by name

dominik-zeglende97e9fd9527
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1695048d52..9428f73efa 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3583,7 +3583,7 @@ type Query {
webhookEvents: [WebhookEvent]
webhookSamplePayload(eventType: WebhookEventTypeEnum!): JSONString
warehouse(id: ID!): Warehouse
- warehouses(filter: WarehouseFilterInput, before: String, after: String, first: Int, last: Int): WarehouseCountableConnection
+ warehouses(filter: WarehouseFilterInput, sortBy: WarehouseSortingInput, before: String, after: String, first: Int, last: Int): WarehouseCountableConnection
translations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnection
translation(id: ID!, kind: TranslatableKinds!): TranslatableItem
stock(id: ID!): Stock
@@ -4770,6 +4770,15 @@ input WarehouseFilterInput {
search: String
}
+enum WarehouseSortField {
+ NAME
+}
+
+input WarehouseSortingInput {
+ direction: OrderDirection!
+ field: WarehouseSortField!
+}
+
type WarehouseUpdate {
errors: [Error!]
warehouseErrors: [WarehouseError!]
diff --git a/saleor/graphql/warehouse/schema.py b/saleor/graphql/warehouse/schema.py
index 0ff35ce134..f7a5490a8c 100644
--- a/saleor/graphql/warehouse/schema.py
+++ b/saleor/graphql/warehouse/schema.py
@@ -1,5 +1,6 @@
import graphene
import graphene_django_optimizer as gql_optimizer
+from django.db.models import QuerySet
from ...core.permissions import ProductPermissions
from ...warehouse import models
@@ -15,9 +16,20 @@ from .mutations import (
WarehouseDelete,
WarehouseUpdate,
)
+from ..utils import sort_queryset
+from .sorters import (
+ WarehouseSortField,
+ WarehouseSortingInput,
+)
from .types import Stock, Warehouse
+def sort_warehouses(qs: QuerySet, sort_by: WarehouseSortingInput) -> QuerySet:
+ if sort_by:
+ return sort_queryset(qs, sort_by, WarehouseSortField)
+ return qs.order_by("name")
+
+
class WarehouseQueries(graphene.ObjectType):
warehouse = graphene.Field(
Warehouse,
@@ -27,7 +39,10 @@ class WarehouseQueries(graphene.ObjectType):
),
)
warehouses = FilterInputConnectionField(
- Warehouse, description="List of warehouses.", filter=WarehouseFilterInput()
+ Warehouse,
+ description="List of warehouses.",
+ filter=WarehouseFilterInput(),
+ sort_by=WarehouseSortingInput(),
)
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
@@ -37,8 +52,9 @@ class WarehouseQueries(graphene.ObjectType):
return warehouse
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
- def resolve_warehouses(self, info, **kwargs):
+ def resolve_warehouses(self, info, sort_by=None, **kwargs):
qs = models.Warehouse.objects.all()
+ qs = sort_warehouses(qs, sort_by)
return gql_optimizer.query(qs, info)
diff --git a/saleor/graphql/warehouse/sorters.py b/saleor/graphql/warehouse/sorters.py
new file mode 100644
index 0000000000..0aa166e985
--- /dev/null
+++ b/saleor/graphql/warehouse/sorters.py
@@ -0,0 +1,23 @@
+import graphene
+
+from ..core.types import SortInputObjectType
+
+
+class WarehouseSortField(graphene.Enum):
+ NAME = "name"
+
+ @property
+ def description(self):
+ # pylint: disable=no-member
+ if self in [
+ WarehouseSortField.NAME,
+ ]:
+ sort_name = self.name.lower().replace("_", " ")
+ return f"Sort warehouses by {sort_name}."
+ raise ValueError("Unsupported enum value: %s" % self.value)
+
+
+class WarehouseSortingInput(SortInputObjectType):
+ class Meta:
+ sort_enum = WarehouseSortField
+ type_name = "warehouses"