Skip to content

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

ix PromotionRule giftIds resolver (#17584)

Iga Karbowiakdf451b527e11
diff --git a/saleor/graphql/discount/tests/queries/test_promotion.py b/saleor/graphql/discount/tests/queries/test_promotion.py
index 27220a46f4..0c2d731b9b 100644
--- a/saleor/graphql/discount/tests/queries/test_promotion.py
+++ b/saleor/graphql/discount/tests/queries/test_promotion.py
@@ -1,5 +1,6 @@
import json
from decimal import Decimal
+from unittest.mock import patch
import graphene
@@ -214,6 +215,52 @@ def test_query_order_promotion_with_gift_rule(
assert rule["rewardType"] == RewardType.GIFT.upper()
+@patch(
+ "saleor.graphql.discount.types.promotions.GiftsByPromotionRuleIDLoader.batch_load"
+)
+def test_query_order_promotion_with_gift_rule_variant_removed_in_meantime(
+ mock_batch_load,
+ order_promotion_without_rules,
+ gift_promotion_rule,
+ staff_api_client,
+ permission_group_manage_discounts,
+):
+ # given
+ query = """
+ query Promotion($id: ID!) {
+ promotion(id: $id) {
+ id
+ rules {
+ rewardType
+ giftIds
+ giftsLimit
+ }
+ }
+ }
+ """
+ promotion = order_promotion_without_rules
+ permission_group_manage_discounts.user_set.add(staff_api_client.user)
+ promotion_id = graphene.Node.to_global_id("Promotion", promotion.id)
+ promotion.rules.add(gift_promotion_rule)
+
+ variables = {"id": promotion_id}
+
+ returned_gift = gift_promotion_rule.gifts.first()
+ mock_batch_load.return_value = [[None, returned_gift]]
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ rule = content["data"]["promotion"]["rules"][0]
+ assert set(rule["giftIds"]) == {
+ graphene.Node.to_global_id("ProductVariant", returned_gift.pk)
+ }
+ assert rule["giftsLimit"] == 1
+ assert rule["rewardType"] == RewardType.GIFT.upper()
+
+
def test_query_promotion_translation(
staff_api_client,
catalogue_promotion,
diff --git a/saleor/graphql/discount/types/promotions.py b/saleor/graphql/discount/types/promotions.py
index 3af8f10cbf..d942e12ada 100644
--- a/saleor/graphql/discount/types/promotions.py
+++ b/saleor/graphql/discount/types/promotions.py
@@ -162,7 +162,9 @@ class PromotionRule(ModelObjectType[models.PromotionRule]):
def resolve_gift_ids(root: models.PromotionRule, info: ResolveInfo):
def with_gifts(gifts):
return [
- graphene.Node.to_global_id("ProductVariant", gift.pk) for gift in gifts
+ graphene.Node.to_global_id("ProductVariant", gift.pk)
+ for gift in gifts
+ if gift
]
return GiftsByPromotionRuleIDLoader(info.context).load(root.id).then(with_gifts)