Skip to content

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

Add query Webhook events

Maciej Korycinskief4f4b92f408
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 4efcdaaa45..5c718b2cac 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3328,6 +3328,7 @@ type ProductVariantUpdatePrivateMeta {
type Query {
webhook(id: ID!): Webhook
webhooks(filter: WebhookFilterInput, before: String, after: String, first: Int, last: Int): WebhookCountableConnection
+ webhookEvents(before: String, after: String, first: Int, last: Int): WebhookEventCountableConnection
translations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnection
shop: Shop
shippingZone(id: ID!): ShippingZone
@@ -4316,6 +4317,18 @@ enum WebhookErrorCode {
type WebhookEvent {
eventType: WebhookEventTypeEnum
+ name: String
+}
+
+type WebhookEventCountableConnection {
+ pageInfo: PageInfo!
+ edges: [WebhookEventCountableEdge!]!
+ totalCount: Int
+}
+
+type WebhookEventCountableEdge {
+ node: WebhookEvent!
+ cursor: String!
}
enum WebhookEventTypeEnum {
diff --git a/saleor/graphql/webhook/resolvers.py b/saleor/graphql/webhook/resolvers.py
index 46be1b97eb..82ed1e8088 100644
--- a/saleor/graphql/webhook/resolvers.py
+++ b/saleor/graphql/webhook/resolvers.py
@@ -1,8 +1,8 @@
import graphene
from graphql_jwt.exceptions import PermissionDenied
-from ...webhook import models
-from .types import Webhook
+from ...webhook import WebhookEventType, models
+from .types import Webhook, WebhookEvent
def resolve_webhooks(info):
@@ -24,3 +24,10 @@ def resolve_webhook(info, webhook_id):
if user.has_perm("webhook.manage_webhooks"):
return graphene.Node.get_node_from_global_id(info, webhook_id, Webhook)
raise PermissionDenied()
+
+
+def resolve_webhook_events():
+ return [
+ WebhookEvent(event_type=event_type[0])
+ for event_type in WebhookEventType.CHOICES
+ ]
diff --git a/saleor/graphql/webhook/schema.py b/saleor/graphql/webhook/schema.py
index cb2e62a8f2..ffd640aa84 100644
--- a/saleor/graphql/webhook/schema.py
+++ b/saleor/graphql/webhook/schema.py
@@ -1,10 +1,11 @@
import graphene
-from ..core.fields import FilterInputConnectionField
+from ..core.fields import FilterInputConnectionField, PrefetchingConnectionField
+from ..decorators import permission_required
from .filters import WebhookFilterInput
from .mutations import WebhookCreate, WebhookDelete, WebhookUpdate
-from .resolvers import resolve_webhook, resolve_webhooks
-from .types import Webhook
+from .resolvers import resolve_webhook, resolve_webhook_events, resolve_webhooks
+from .types import Webhook, WebhookEvent
class WebhookQueries(graphene.ObjectType):
@@ -20,6 +21,9 @@ class WebhookQueries(graphene.ObjectType):
description="List of webhooks.",
filter=WebhookFilterInput(description="Filtering options for webhooks."),
)
+ webhook_events = PrefetchingConnectionField(
+ WebhookEvent, description="List of all available webhook events."
+ )
@staticmethod
def resolve_webhooks(_, info, **_kwargs):
@@ -29,6 +33,11 @@ class WebhookQueries(graphene.ObjectType):
def resolve_webhook(_, info, **data):
return resolve_webhook(info, data["id"])
+ @staticmethod
+ @permission_required("webhook.manage_webhooks")
+ def resolve_webhook_events(_, *_args, **_data):
+ return resolve_webhook_events()
+
class WebhookMutations(graphene.ObjectType):
webhook_create = WebhookCreate.Field()
diff --git a/saleor/graphql/webhook/types.py b/saleor/graphql/webhook/types.py
index 217901c098..58a6bdfd06 100644
--- a/saleor/graphql/webhook/types.py
+++ b/saleor/graphql/webhook/types.py
@@ -1,16 +1,24 @@
import graphene
import graphene_django_optimizer as gql_optimizer
-from ...webhook import models
+from ...webhook import WebhookEventType, models
from ..core.connection import CountableDjangoObjectType
from .enums import WebhookEventTypeEnum
-class WebhookEvent(graphene.ObjectType):
- event_type = WebhookEventTypeEnum(description="Name of the event type.")
+class WebhookEvent(CountableDjangoObjectType):
+ name = graphene.String(description="Printable name of the event")
+ event_type = WebhookEventTypeEnum(description="Internal name of the event type.")
class Meta:
+ model = models.WebhookEvent
description = "Webhook event."
+ only_fields = ["event_type", "name"]
+
+ @staticmethod
+ def resolve_name(info: models.WebhookEvent, *_args, **_kwargs):
+ name = WebhookEventType.DISPLAY_LABELS.get(info.event_type) or info.event_type
+ return name
class Webhook(CountableDjangoObjectType):
diff --git a/saleor/webhook/__init__.py b/saleor/webhook/__init__.py
index bf30635199..5f75f0a070 100644
--- a/saleor/webhook/__init__.py
+++ b/saleor/webhook/__init__.py
@@ -13,18 +13,47 @@ class WebhookEventType:
PRODUCT_CREATED = "product_created"
+ DISPLAY_LABELS = {
+ ANY: "Any events",
+ ORDER_CREATED: "Order created",
+ ORDER_FULLYPAID: "Order paid",
+ ORDER_UPDATED: "Order updated",
+ ORDER_CANCELLED: "Order cancelled",
+ ORDER_FULFILLED: "Order fulfilled",
+ CUSTOMER_CREATED: "Customer created",
+ PRODUCT_CREATED: "Product created",
+ }
+
CHOICES = [
- (ANY, pgettext_lazy("Any events", "Any events")),
- (ORDER_CREATED, pgettext_lazy("Order has been placed", "Order created")),
- (ORDER_FULLYPAID, pgettext_lazy("Order has been fully paid", "Order paid")),
- (ORDER_UPDATED, pgettext_lazy("Order has been updated", "Order updated")),
- (ORDER_CANCELLED, pgettext_lazy("Order has been cancelled", "Order cancelled")),
+ (ANY, pgettext_lazy("Any events", DISPLAY_LABELS[ANY])),
+ (
+ ORDER_CREATED,
+ pgettext_lazy("Order has been placed", DISPLAY_LABELS[ORDER_CREATED]),
+ ),
+ (
+ ORDER_FULLYPAID,
+ pgettext_lazy("Order has been fully paid", DISPLAY_LABELS[ORDER_FULLYPAID]),
+ ),
+ (
+ ORDER_UPDATED,
+ pgettext_lazy("Order has been updated", DISPLAY_LABELS[ORDER_UPDATED]),
+ ),
+ (
+ ORDER_CANCELLED,
+ pgettext_lazy("Order has been cancelled", DISPLAY_LABELS[ORDER_CANCELLED]),
+ ),
(
CUSTOMER_CREATED,
- pgettext_lazy("Customer has been created", "Customer created"),
+ pgettext_lazy(
+ "Customer has been created", DISPLAY_LABELS[CUSTOMER_CREATED]
+ ),
+ ),
+ (
+ PRODUCT_CREATED,
+ pgettext_lazy("Product has been created", DISPLAY_LABELS[PRODUCT_CREATED]),
),
- (PRODUCT_CREATED, pgettext_lazy("Product has been created", "Product created")),
]
+
PERMISSIONS = {
ORDER_CREATED: "order.manage_orders",
ORDER_FULLYPAID: "order.manage_orders",
diff --git a/tests/api/test_webhook.py b/tests/api/test_webhook.py
index 341001e7ba..b5fd44dba3 100644
--- a/tests/api/test_webhook.py
+++ b/tests/api/test_webhook.py
@@ -3,6 +3,7 @@ import pytest
from saleor.account.models import ServiceAccount
from saleor.graphql.webhook.enums import WebhookEventTypeEnum
+from saleor.webhook import WebhookEventType
from saleor.webhook.models import Webhook
from .utils import assert_no_permission, get_graphql_content
@@ -507,3 +508,34 @@ def test_query_webhook_by_service_account_without_permission(
content = get_graphql_content(response)
webhook_response = content["data"]["webhook"]
assert webhook_response is None
+
+
+WEBHOOK_EVENTS_QUERY = """
+{
+ webhookEvents(first:10){
+ edges{
+ node{
+ eventType
+ name
+ }
+ }
+ }
+}
+"""
+
+
+def test_query_webhook_events(staff_api_client, permission_manage_webhooks):
+ query = WEBHOOK_EVENTS_QUERY
+ staff_api_client.user.user_permissions.add(permission_manage_webhooks)
+ response = staff_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ webhook_events = content["data"]["webhookEvents"]["edges"]
+
+ assert len(webhook_events) == len(WebhookEventType.CHOICES)
+
+
+def test_query_webhook_events_without_permissions(staff_api_client):
+
+ query = WEBHOOK_EVENTS_QUERY
+ response = staff_api_client.post_graphql(query)
+ assert_no_permission(response)