Skip to content

Commit

Permalink
Add Webhook payload via graphql subscriptions (#9394)
Browse files Browse the repository at this point in the history
* Add PoC of webhook subscriptions

* add async webhooks subscription payloads feature

* remove unneeded file

* add translations subscription handling, fixes after review

* remove todo

* add descriptions

* add descriptions, move subsrciption_payloads.py

* refactor

* fix imports, add changelog

* check_document_is_single_subscription refactor

Co-authored-by: Maciej Korycinski <maciej@mirumee.com>
Co-authored-by: Marcin Gębala <5421321+maarcingebala@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 1, 2022
1 parent 536ece4 commit aca6418
Show file tree
Hide file tree
Showing 25 changed files with 3,571 additions and 115 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix failing `checkoutCustomerAttach` mutation - #9401 by @IKarbowiak
- Add new mutation `orderCreateFromCheckout` - #9343 by @korycins
- Add `language_code` field to webhook payload for `Order`, `Checkout` and `Customer` - #9433 by @rafalp
- Add handling webhook payload via GraphQL subscriptions (#9394) @jakubkuc
- Fix access to own resources by App - #9425 by @korycins


# 3.1.7

- Handle `ValidationError` in metadata mutations (#9380) (75deaf6ea)
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
"saleor.graphql.account.tests.benchmark.fixtures",
"saleor.graphql.order.tests.benchmark.fixtures",
"saleor.graphql.giftcard.tests.benchmark.fixtures",
"saleor.plugins.webhook.tests.subscription_webhooks.fixtures",
]
5 changes: 4 additions & 1 deletion saleor/graphql/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .translations.schema import TranslationQueries
from .warehouse.schema import StockQueries, WarehouseMutations, WarehouseQueries
from .webhook.schema import WebhookMutations, WebhookQueries
from .webhook.subscription_types import Subscription

API_PATH = SimpleLazyObject(lambda: reverse("api"))

Expand Down Expand Up @@ -83,4 +84,6 @@ class Mutation(
pass


schema = build_federated_schema(query=Query, mutation=Mutation, types=unit_enums)
schema = build_federated_schema(
Query, mutation=Mutation, types=unit_enums, subscription=Subscription
)
3 changes: 2 additions & 1 deletion saleor/graphql/core/federation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ class _Service(graphene.ObjectType):
sdl = graphene.String()


def build_federated_schema(query=None, mutation=None, types=None):
def build_federated_schema(query=None, mutation=None, types=None, subscription=None):
"""Create GraphQL schema that supports Apollo Federation."""
schema = graphene.Schema(
query=query,
mutation=mutation,
types=list(types) + [_Any, _Entity, _Service],
subscription=subscription,
)

entity_type = schema.get_type("_Entity")
Expand Down
7 changes: 6 additions & 1 deletion saleor/graphql/page/tests/mutations/test_page_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import graphene
import pytz
from django.utils.functional import SimpleLazyObject
from django.utils.text import slugify
from freezegun import freeze_time

Expand Down Expand Up @@ -156,7 +157,11 @@ def test_page_create_trigger_page_webhook(
expected_data = generate_page_payload(page, staff_api_client.user)

mocked_webhook_trigger.assert_called_once_with(
expected_data, WebhookEventAsyncType.PAGE_CREATED, [any_webhook]
expected_data,
WebhookEventAsyncType.PAGE_CREATED,
[any_webhook],
page,
SimpleLazyObject(lambda: staff_api_client.user),
)


Expand Down
7 changes: 6 additions & 1 deletion saleor/graphql/page/tests/mutations/test_page_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import graphene
import pytest
from django.utils.functional import SimpleLazyObject
from freezegun import freeze_time

from .....attribute.models import AttributeValue
Expand Down Expand Up @@ -64,7 +65,11 @@ def test_page_delete_trigger_webhook(
page.refresh_from_db()
expected_data = generate_page_payload(page, staff_api_client.user)
mocked_webhook_trigger.assert_called_once_with(
expected_data, WebhookEventAsyncType.PAGE_DELETED, [any_webhook]
expected_data,
WebhookEventAsyncType.PAGE_DELETED,
[any_webhook],
page,
SimpleLazyObject(lambda: staff_api_client.user),
)


Expand Down
7 changes: 6 additions & 1 deletion saleor/graphql/page/tests/mutations/test_page_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import graphene
import pytest
from django.utils.functional import SimpleLazyObject
from django.utils.text import slugify
from freezegun import freeze_time

Expand Down Expand Up @@ -167,7 +168,11 @@ def test_update_page_trigger_webhook(
page.publication_date = date(2020, 3, 18)
expected_data = generate_page_payload(page, staff_api_client.user)
mocked_webhook_trigger.assert_called_once_with(
expected_data, WebhookEventAsyncType.PAGE_UPDATED, [any_webhook]
expected_data,
WebhookEventAsyncType.PAGE_UPDATED,
[any_webhook],
page,
SimpleLazyObject(lambda: staff_api_client.user),
)


Expand Down
7 changes: 6 additions & 1 deletion saleor/graphql/product/tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.db.models import Sum
from django.utils import timezone
from django.utils.dateparse import parse_datetime
from django.utils.functional import SimpleLazyObject
from django.utils.html import strip_tags
from django.utils.text import slugify
from freezegun import freeze_time
Expand Down Expand Up @@ -7766,7 +7767,11 @@ def test_delete_product_trigger_webhook(
product, variants_id, staff_api_client.user
)
mocked_webhook_trigger.assert_called_once_with(
expected_data, WebhookEventAsyncType.PRODUCT_DELETED, [any_webhook]
expected_data,
WebhookEventAsyncType.PRODUCT_DELETED,
[any_webhook],
product,
SimpleLazyObject(lambda: staff_api_client.user),
)
mocked_recalculate_orders_task.assert_not_called()

Expand Down

0 comments on commit aca6418

Please sign in to comment.