Skip to content

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

Fix stock webhook subscriptions failing when the variant was deleted (#19871)

Lukasz Ostrowskie6f8775128e4
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index ac6398dc39..349c913a98 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -1006,6 +1006,23 @@ class ProductVariantMetadataUpdated(SubscriptionObjectType, ProductVariantBase):
description = "Event sent when product variant metadata is updated."
+def resolve_variant_from_stock(stock, info: ResolveInfo, channel=None):
+ """Resolve the variant of a stock, tolerating a variant deleted in the meantime.
+
+ Webhook payloads are generated asynchronously, so the variant may already be
+ gone when the subscription query is executed.
+ """
+ return (
+ ProductVariantByIdLoader(info.context)
+ .load(stock.product_variant_id)
+ .then(
+ lambda variant: ChannelContext(node=variant, channel_slug=channel)
+ if variant
+ else None
+ )
+ )
+
+
class ProductVariantOutOfStock(SubscriptionObjectType, ProductVariantBase):
warehouse = graphene.Field(
"saleor.graphql.warehouse.types.Warehouse", description="Look up a warehouse."
@@ -1020,8 +1037,7 @@ class ProductVariantOutOfStock(SubscriptionObjectType, ProductVariantBase):
@staticmethod
def resolve_product_variant(root, info: ResolveInfo, channel=None):
_, stock = root
- variant = stock.product_variant
- return ChannelContext(node=variant, channel_slug=channel)
+ return resolve_variant_from_stock(stock, info, channel)
@staticmethod
def resolve_warehouse(root, _info: ResolveInfo):
@@ -1041,10 +1057,9 @@ class ProductVariantBackInStock(SubscriptionObjectType, ProductVariantBase):
description = "Event sent when product variant is back in stock."
@staticmethod
- def resolve_product_variant(root, _info: ResolveInfo, channel=None):
+ def resolve_product_variant(root, info: ResolveInfo, channel=None):
_, stock = root
- variant = stock.product_variant
- return ChannelContext(node=variant, channel_slug=channel)
+ return resolve_variant_from_stock(stock, info, channel)
@staticmethod
def resolve_warehouse(root, _info):
@@ -1067,11 +1082,7 @@ class ProductVariantStockUpdated(SubscriptionObjectType, ProductVariantBase):
@staticmethod
def resolve_product_variant(root, info: ResolveInfo, channel=None):
_, stock = root
- return (
- ProductVariantByIdLoader(info.context)
- .load(stock.product_variant.id)
- .then(lambda variant: ChannelContext(node=variant, channel_slug=None))
- )
+ return resolve_variant_from_stock(stock, info, channel)
@staticmethod
def resolve_warehouse(root, info: ResolveInfo):
diff --git a/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
index 83e9f448c0..9545c8750f 100644
--- a/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
@@ -12,12 +12,13 @@ from ....giftcard.models import GiftCard
from ....graphql.webhook.subscription_query import SubscriptionQuery
from ....menu.models import Menu, MenuItem
from ....product.interface import VariantDiscountedPriceChange
-from ....product.models import Category
+from ....product.models import Category, ProductVariant
from ....shipping.models import (
ShippingMethod,
ShippingZone,
)
from ....site.models import SiteSettings
+from ....warehouse.models import Stock
from ...event_types import WebhookEventAsyncType, WebhookEventSyncType
from ...transport.asynchronous.transport import (
create_deliveries_for_subscriptions,
@@ -1547,6 +1548,50 @@ def test_product_variant_stock_updated(
assert deliveries[0].webhook == webhooks[0]
+ ("event_type", "webhook_fixture", "with_warehouse"),
+ [
+ (
+ WebhookEventAsyncType.PRODUCT_VARIANT_OUT_OF_STOCK,
+ "subscription_product_variant_out_of_stock_webhook",
+ False,
+ ),
+ (
+ WebhookEventAsyncType.PRODUCT_VARIANT_BACK_IN_STOCK,
+ "subscription_product_variant_back_in_stock_webhook",
+ False,
+ ),
+ (
+ WebhookEventAsyncType.PRODUCT_VARIANT_STOCK_UPDATED,
+ "subscription_product_variant_stock_updated_webhook",
+ True,
+ ),
+ ],
+)
+def test_product_variant_stock_event_with_deleted_variant(
+ event_type, webhook_fixture, with_warehouse, stock, request
+):
+ # given
+ webhooks = [request.getfixturevalue(webhook_fixture)]
+ warehouse_id = graphene.Node.to_global_id("Warehouse", stock.warehouse_id)
+ # the payload is generated asynchronously, from a stock fetched before the
+ # variant was removed
+ stock = Stock.objects.get(pk=stock.pk)
+ ProductVariant.objects.filter(pk=stock.product_variant_id).delete()
+
+ expected_payload = {"productVariant": None}
+ if with_warehouse:
+ expected_payload["warehouse"] = {"id": warehouse_id}
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, stock, webhooks)
+
+ # then
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+ assert json.loads(deliveries[0].payload.get_payload()) == expected_payload
+
+
def test_product_variant_discounted_price_updated(
variant_with_many_stocks,
channel_USD,