Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
[SALEOR-6404] syncEvents and asyncEvents split
Tomasz Magulskic30eead1b3ee
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.pyindex 047ffe7438..895ef7b57c 100644--- a/saleor/app/installation_utils.py+++ b/saleor/app/installation_utils.py@@ -77,7 +77,7 @@ def install_app(app_installation: AppInstallation, activate: bool = False):WebhookEvent.objects.bulk_create([WebhookEvent(webhook=webhook, event_type=event)- for event in webhook_data["events"]+ for event in (webhook_data.get("asyncEvents", []) + webhook_data.get("syncEvents", []))])diff --git a/saleor/app/manifest_validations.py b/saleor/app/manifest_validations.pyindex 594e8e3db2..a90f66a713 100644--- a/saleor/app/manifest_validations.py+++ b/saleor/app/manifest_validations.py@@ -12,7 +12,7 @@ from ..core.permissions import (get_permissions_enum_list,split_permission_codename,)-from ..graphql.webhook.mutations import validate_query+from ..graphql.webhook.subscription_payload import validate_queryfrom .error_codes import AppErrorCodefrom .types import AppExtensionMount, AppExtensionTargetfrom .validators import AppURLValidator@@ -183,13 +183,13 @@ def clean_extensions(manifest_data, app_permissions, errors):def clean_webhooks(manifest_data, app_permissions, errors):webhooks = manifest_data.get("webhooks", [])for webhook in webhooks:- validate_query(webhook["query"])+ validate_query(webhook["query"], required=True)def validate_required_fields(manifest_data, errors):manifest_required_fields = {"id", "version", "name", "tokenTargetUrl"}extension_required_fields = {"label", "url", "mount"}- webhook_required_fields = {"name", "targetUrl", "isActive", "events", "query"}+ webhook_required_fields = {"name", "targetUrl", "isActive", "query"}if manifest_missing_fields := manifest_required_fields.difference(manifest_data):for missing_field in manifest_missing_fields:diff --git a/saleor/graphql/app/types.py b/saleor/graphql/app/types.pyindex f825fcb402..282afe6937 100644--- a/saleor/graphql/app/types.py+++ b/saleor/graphql/app/types.py@@ -8,12 +8,12 @@ from ...core.exceptions import PermissionDeniedfrom ...core.permissions import AppPermission, AuthorizationFiltersfrom ..account.utils import is_owner_or_has_one_of_permsfrom ..core.connection import CountableConnection-from ..core.descriptions import ADDED_IN_31, PREVIEW_FEATURE+from ..core.descriptions import ADDED_IN_31, ADDED_IN_34, PREVIEW_FEATUREfrom ..core.federation import federated_entity, resolve_federation_referencesfrom ..core.types import Job, ModelObjectType, NonNullList, Permissionfrom ..meta.types import ObjectWithMetadatafrom ..utils import format_permissions_for_display, get_user_or_app_from_context-from ..webhook.enums import WebhookEventTypeEnum+from ..webhook.enums import WebhookEventTypeAsyncEnum, WebhookEventTypeSyncEnumfrom ..webhook.types import Webhookfrom .dataloaders import AppByIdLoader, AppExtensionByAppIdLoaderfrom .enums import AppExtensionMountEnum, AppExtensionTargetEnum, AppTypeEnum@@ -126,15 +126,32 @@ class AppExtensionCountableConnection(CountableConnection):class AppManifestWebhook(graphene.ObjectType):- name = graphene.String(required=True)- events = NonNullList(WebhookEventTypeEnum, required=True)- query = graphene.String(required=True)- target_url = graphene.String(required=True)- is_active = graphene.Boolean(required=True)+ name = graphene.String(description="The name of the webhook.", required=True)+ async_events = NonNullList(+ WebhookEventTypeAsyncEnum,+ description="The asynchronous events that webhook wants to subscribe.",+ )+ sync_events = NonNullList(+ WebhookEventTypeSyncEnum,+ description="The synchronous events that webhook wants to subscribe.",+ )+ query = graphene.String(+ description="Subscription query of a webhook", required=True+ )+ target_url = graphene.String(+ description="The url to receive the payload.", required=True+ )+ is_active = graphene.Boolean(+ description="Determine if webhook will be set active or not.", required=True+ )++ @staticmethod+ def resolve_async_events(root, info):+ return [WebhookEventTypeAsyncEnum[name] for name in root.get("asyncEvents", [])]@staticmethod- def resolve_events(root, info):- return [WebhookEventTypeEnum[name] for name in root["events"]]+ def resolve_sync_events(root, info):+ return [WebhookEventTypeSyncEnum[name] for name in root.get("syncEvents", [])]@staticmethoddef resolve_target_url(root, info):@@ -159,7 +176,11 @@ class Manifest(graphene.ObjectType):homepage_url = graphene.String()support_url = graphene.String()extensions = NonNullList(AppManifestExtension, required=True)- webhooks = NonNullList(AppManifestWebhook, required=True)+ webhooks = NonNullList(+ AppManifestWebhook,+ description="List of the app's webhooks." + ADDED_IN_34 + PREVIEW_FEATURE,+ required=True,+ )class Meta:description = "The manifest definition."@@ -230,7 +251,7 @@ class App(ModelObjectType):)extensions = NonNullList(AppExtension,- description="App's dashboard extensions." + ADDED_IN_31 + PREVIEW_FEATURE,+ description=f"App's dashboard extensions.{ADDED_IN_31}{PREVIEW_FEATURE}",required=True,)diff --git a/saleor/graphql/webhook/mutations.py b/saleor/graphql/webhook/mutations.pyindex 866167b288..7ad75c896d 100644--- a/saleor/graphql/webhook/mutations.py+++ b/saleor/graphql/webhook/mutations.py@@ -8,25 +8,10 @@ from ..core.descriptions import ADDED_IN_32, DEPRECATED_IN_3X_INPUT, PREVIEW_FEAfrom ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutationfrom ..core.types import NonNullList, WebhookErrorfrom . import enums-from .subscription_payload import validate_subscription_query+from .subscription_payload import validate_queryfrom .types import EventDelivery, Webhook-def validate_query(query):- if not query:- return- is_valid = validate_subscription_query(query)- if not is_valid:- raise ValidationError(- {- "query": ValidationError(- "Subscription query is not valid",- code=WebhookErrorCode.INVALID.value,- )- }- )--class WebhookCreateInput(graphene.InputObjectType):name = graphene.String(description="The name of the webhook.", required=False)target_url = graphene.String(description="The url to receive the payload.")diff --git a/saleor/graphql/webhook/subscription_payload.py b/saleor/graphql/webhook/subscription_payload.pyindex a917dd455c..89323cda4b 100644--- a/saleor/graphql/webhook/subscription_payload.py+++ b/saleor/graphql/webhook/subscription_payload.py@@ -1,6 +1,7 @@from typing import Any, Dict, Optionalfrom celery.utils.log import get_task_logger+from django.core.exceptions import ValidationErrorfrom django.core.handlers.base import BaseHandlerfrom django.http import HttpRequestfrom django.test.client import RequestFactory@@ -10,6 +11,8 @@ from graphql.language.ast import FragmentDefinition, OperationDefinitionfrom promise import Promisefrom ...app.models import App+from ...webhook.error_codes import WebhookErrorCode+logger = get_task_logger(__name__)@@ -27,6 +30,21 @@ def validate_subscription_query(query: str) -> bool:return True+def validate_query(query, required=False):+ if not query and not required:+ return+ is_valid = validate_subscription_query(query)+ if not is_valid:+ raise ValidationError(+ {+ "query": ValidationError(+ "Subscription query is not valid",+ code=WebhookErrorCode.INVALID.value,+ )+ }+ )++def check_document_is_single_subscription(document: GraphQLDocument) -> bool:"""Check if document contains only a single subscription definition.diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex d485211c59..6a5c90b9af 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -5718,7 +5718,7 @@ def app_manifest():def app_manifest_webhook():return {"name": "webhook",- "events": [+ "asyncEvents": ["ORDER_CREATED","ORDER_FULLY_PAID","CUSTOMER_CREATED",