Skip to content

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

Fix handling null slugs in VariantChannelListingByVariantIdAndChannelSlugLoader (#16129)

Marcin Gębala06c954c24193
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index 31871a4518..fd085b8fee 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -25,7 +25,7 @@ from ...core.dataloaders import BaseThumbnailBySizeAndFormatLoader, DataLoader
ProductIdAndChannelSlug = tuple[int, str]
VariantIdAndChannelSlug = tuple[int, str]
-VariantIdAndChannelId = tuple[int, int]
+VariantIdAndChannelId = tuple[int, Optional[int]]
class CategoryByIdLoader(DataLoader[int, Category]):
@@ -303,12 +303,12 @@ class VariantChannelListingByVariantIdAndChannelSlugLoader(
context_key = "variantchannelisting_by_variant_and_channelslug"
def batch_load(self, keys):
- channel_slugs = [channel_slug for _, channel_slug in keys]
+ channel_slugs = [channel_slug for _, channel_slug in keys if channel_slug]
def with_channels(channels):
channel_map = {c.slug: c.id for c in channels}
variant_id_channel_id_keys = [
- (variant_id, channel_map[channel_slug])
+ (variant_id, channel_map.get(channel_slug, None))
for (variant_id, channel_slug) in keys
]
return VariantChannelListingByVariantIdAndChannelIdLoader(
@@ -331,8 +331,8 @@ class VariantChannelListingByVariantIdAndChannelIdLoader(
# Split the list of keys by channel first. A typical query will only touch
# a handful of unique countries but may access thousands of product variants
# so it's cheaper to execute one query per channel.
- variant_channel_listing_by_channel: defaultdict[int, list[int]] = defaultdict(
- list
+ variant_channel_listing_by_channel: defaultdict[Optional[int], list[int]] = (
+ defaultdict(list)
)
for variant_id, channel_id in keys:
variant_channel_listing_by_channel[channel_id].append(variant_id)
@@ -351,7 +351,7 @@ class VariantChannelListingByVariantIdAndChannelIdLoader(
return [variant_channel_listing_by_variant_and_channel[key] for key in keys]
def batch_load_channel(
- self, channel_id: int, variant_ids: Iterable[int]
+ self, channel_id: Optional[int], variant_ids: Iterable[int]
) -> Iterable[tuple[int, Optional[ProductVariantChannelListing]]]:
filter = {
"channel_id": channel_id,
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
index 4edff8130f..f9c1aa28ca 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
@@ -16,17 +16,18 @@ from ...utils import create_stocks
VARIANT_STOCKS_UPDATE_MUTATIONS = """
mutation ProductVariantStocksUpdate($variantId: ID!, $stocks: [StockInput!]!){
productVariantStocksUpdate(variantId: $variantId, stocks: $stocks){
- productVariant{
- stocks{
+ productVariant {
+ quantityAvailable
+ stocks {
quantity
quantityAllocated
id
- warehouse{
+ warehouse {
slug
}
}
}
- errors{
+ errors {
code
field
message
@@ -38,8 +39,12 @@ VARIANT_STOCKS_UPDATE_MUTATIONS = """
def test_product_variant_stocks_update(
- staff_api_client, variant, warehouse, permission_manage_products
+ staff_api_client,
+ preorder_variant_global_threshold,
+ warehouse,
+ permission_manage_products,
):
+ variant = preorder_variant_global_threshold
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
second_warehouse = Warehouse.objects.get(pk=warehouse.pk)
second_warehouse.slug = "second warehouse"
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index e2caf35d09..8e078e37d2 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -10,6 +10,7 @@ from promise import Promise
from ....attribute import models as attribute_models
from ....channel.models import Channel
+from ....core.db.connection import allow_writer_in_context
from ....core.utils import build_absolute_uri
from ....core.utils.country import get_active_country
from ....core.weight import convert_weight_to_default_weight_unit
@@ -457,7 +458,11 @@ class ProductVariant(ChannelContextTypeWithMetadata[models.ProductVariant]):
if address is not None:
country_code = address.country
channel_slug = str(root.channel_slug) if root.channel_slug else None
- global_quantity_limit_per_checkout = site.settings.limit_quantity_per_checkout
+
+ with allow_writer_in_context(info.context):
+ global_quantity_limit_per_checkout = (
+ site.settings.limit_quantity_per_checkout
+ )
if root.node.is_preorder_active():
variant = root.node