Skip to content

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

Add sort by to webhooks query

fowczarek793717d5c713
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 3b55dbeb51..a902a8f87b 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3437,7 +3437,7 @@ type ProductVariantUpdatePrivateMeta {
type Query {
webhook(id: ID!): Webhook
- webhooks(filter: WebhookFilterInput, before: String, after: String, first: Int, last: Int): WebhookCountableConnection
+ webhooks(sortBy: WebhookSortingInput, filter: WebhookFilterInput, before: String, after: String, first: Int, last: Int): WebhookCountableConnection
webhookEvents: [WebhookEvent]
webhookSamplePayload(eventType: WebhookEventTypeEnum!): JSONString
translations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnection
@@ -4552,6 +4552,17 @@ input WebhookFilterInput {
isActive: Boolean
}
+enum WebhookSortField {
+ NAME
+ SERVICE_ACCOUNT
+ TARGET_URL
+}
+
+input WebhookSortingInput {
+ direction: OrderDirection!
+ field: WebhookSortField
+}
+
type WebhookUpdate {
errors: [Error!]
webhook: Webhook
diff --git a/saleor/graphql/webhook/resolvers.py b/saleor/graphql/webhook/resolvers.py
index fc164d8eaa..8756d8e37f 100644
--- a/saleor/graphql/webhook/resolvers.py
+++ b/saleor/graphql/webhook/resolvers.py
@@ -2,17 +2,22 @@ import graphene
from graphql_jwt.exceptions import PermissionDenied
from ...webhook import WebhookEventType, models, payloads
+from ..utils import sort_queryset
+from .sorters import WebhookSortField
from .types import Webhook, WebhookEvent
-def resolve_webhooks(info):
+def resolve_webhooks(info, sort_by=None, **_kwargs):
service_account = info.context.service_account
if service_account:
- return models.Webhook.objects.filter(service_account=service_account)
- user = info.context.user
- if not user.has_perm("webhook.manage_webhooks"):
- raise PermissionDenied()
- return models.Webhook.objects.all()
+ qs = models.Webhook.objects.filter(service_account=service_account)
+ else:
+ user = info.context.user
+ if not user.has_perm("webhook.manage_webhooks"):
+ raise PermissionDenied()
+ qs = models.Webhook.objects.all()
+ qs = sort_queryset(qs, sort_by, WebhookSortField)
+ return qs
def resolve_webhook(info, webhook_id):
diff --git a/saleor/graphql/webhook/schema.py b/saleor/graphql/webhook/schema.py
index 131e1aef88..dda98cc9e9 100644
--- a/saleor/graphql/webhook/schema.py
+++ b/saleor/graphql/webhook/schema.py
@@ -11,6 +11,7 @@ from .resolvers import (
resolve_webhook_events,
resolve_webhooks,
)
+from .sorters import WebhookSortingInput
from .types import Webhook, WebhookEvent
@@ -25,6 +26,7 @@ class WebhookQueries(graphene.ObjectType):
webhooks = FilterInputConnectionField(
Webhook,
description="List of webhooks.",
+ sort_by=WebhookSortingInput(description="Sort webhooks."),
filter=WebhookFilterInput(description="Filtering options for webhooks."),
)
webhook_events = graphene.List(
@@ -50,7 +52,7 @@ class WebhookQueries(graphene.ObjectType):
@staticmethod
def resolve_webhooks(_, info, **_kwargs):
- return resolve_webhooks(info)
+ return resolve_webhooks(info, **_kwargs)
@staticmethod
def resolve_webhook(_, info, **data):
diff --git a/saleor/graphql/webhook/sorters.py b/saleor/graphql/webhook/sorters.py
new file mode 100644
index 0000000000..c660d16c91
--- /dev/null
+++ b/saleor/graphql/webhook/sorters.py
@@ -0,0 +1,27 @@
+import graphene
+
+from ..core.types import SortInputObjectType
+
+
+class WebhookSortField(graphene.Enum):
+ NAME = "name"
+ SERVICE_ACCOUNT = "service_account__name"
+ TARGET_URL = "target_url"
+
+ @property
+ def description(self):
+ # pylint: disable=no-member
+ if self in [
+ WebhookSortField.NAME,
+ WebhookSortField.SERVICE_ACCOUNT,
+ WebhookSortField.TARGET_URL,
+ ]:
+ sort_name = self.name.lower().replace("_", " ")
+ return f"Sort webhooks by {sort_name}."
+ raise ValueError("Unsupported enum value: %s" % self.value)
+
+
+class WebhookSortingInput(SortInputObjectType):
+ class Meta:
+ sort_enum = WebhookSortField
+ type_name = "webhooks"
diff --git a/tests/api/test_webhook.py b/tests/api/test_webhook.py
index 8e442721b9..ab43770bd4 100644
--- a/tests/api/test_webhook.py
+++ b/tests/api/test_webhook.py
@@ -419,6 +419,70 @@ def test_query_webhooks_with_filters(
assert webhook_id == str(second_webhook.id)
+QUERY_WEBHOOKS_WITH_SORT = """
+ query ($sort_by: WebhookSortingInput!) {
+ webhooks(first:5, sortBy: $sort_by) {
+ edges{
+ node{
+ name
+ }
+ }
+ }
+ }
+"""
+
+
+ "webhooks_sort, result_order",
+ [
+ ({"field": "NAME", "direction": "ASC"}, ["backup", "hook1", "hook2"]),
+ ({"field": "NAME", "direction": "DESC"}, ["hook2", "hook1", "backup"]),
+ (
+ {"field": "SERVICE_ACCOUNT", "direction": "ASC"},
+ ["hook2", "backup", "hook1"],
+ ),
+ (
+ {"field": "SERVICE_ACCOUNT", "direction": "DESC"},
+ ["backup", "hook1", "hook2"],
+ ),
+ ({"field": "TARGET_URL", "direction": "ASC"}, ["hook2", "hook1", "backup"]),
+ ({"field": "TARGET_URL", "direction": "DESC"}, ["backup", "hook1", "hook2"]),
+ ],
+)
+def test_query_webhooks_with_sort(
+ webhooks_sort, result_order, staff_api_client, permission_manage_webhooks
+):
+ backupAccount = ServiceAccount.objects.create(name="backupAccount", is_active=True)
+ account = ServiceAccount.objects.create(name="account", is_active=True)
+ Webhook.objects.bulk_create(
+ [
+ Webhook(
+ name="hook1",
+ service_account=backupAccount,
+ target_url="http://example.com/activate2",
+ ),
+ Webhook(
+ name="hook2",
+ service_account=account,
+ target_url="http://example.com/activate",
+ ),
+ Webhook(
+ name="backup",
+ service_account=backupAccount,
+ target_url="http://example.com/backup",
+ ),
+ ]
+ )
+ variables = {"sort_by": webhooks_sort}
+ staff_api_client.user.user_permissions.add(permission_manage_webhooks)
+ response = staff_api_client.post_graphql(QUERY_WEBHOOKS_WITH_SORT, variables)
+ content = get_graphql_content(response)
+ webhooks = content["data"]["webhooks"]["edges"]
+
+ for order, webhook_name in enumerate(result_order):
+ assert webhooks[order]["node"]["name"] == webhook_name
+
+
def test_query_webhooks_by_service_account_without_permissions(
service_account_api_client
):