Skip to content

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

Fix CheckoutLinesInfoByCheckoutTokenLoader dataloader (#6929)

Iga Karbowiak60d6cdaf6d01
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58de521f46..21e3f42d01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -60,6 +60,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add product description_plaintext to populatedb - #6894 by @d-wysocki
- Deallocate stocks for order lines in a bulk way - #6896 by @IKarbowiak
- Prevent negative available quantity - #6897 by @d-wysocki
+- Fix CheckoutLinesInfoByCheckoutTokenLoader dataloader - #6929 by @IKarbowiak
# 2.11.1
diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.py
index 7ce33403d6..f7401d0098 100644
--- a/saleor/graphql/checkout/dataloaders.py
+++ b/saleor/graphql/checkout/dataloaders.py
@@ -10,7 +10,7 @@ from ..product.dataloaders import (
CollectionsByVariantIdLoader,
ProductByVariantIdLoader,
ProductVariantByIdLoader,
- VariantChannelListingByVariantIdAndChannelSlugLoader,
+ VariantChannelListingByVariantIdAndChannelIdLoader,
)
@@ -26,71 +26,67 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader):
context_key = "checkoutlinesinfo_by_checkout"
def batch_load(self, keys):
- from ..channel.dataloaders import ChannelByCheckoutLineIDLoader
-
- def with_checkout_lines(checkout_lines):
+ def with_checkout_lines(results):
+ checkouts, checkout_lines = results
variants_pks = list(
{line.variant_id for lines in checkout_lines for line in lines}
)
if not variants_pks:
return [[] for _ in keys]
- channel = ChannelByCheckoutLineIDLoader(self.context).load(
- checkout_lines[0][0].id
- )
- def map_variant_ids_with_channel(channel):
- def with_variants_products_collections(results):
- variants, products, collections, channel_listings = results
- variants_map = dict(zip(variants_pks, variants))
- products_map = dict(zip(variants_pks, products))
- collections_map = dict(zip(variants_pks, collections))
- channel_listings_map = dict(zip(variants_pks, channel_listings))
-
- lines_info = []
- for lines in checkout_lines:
- lines_info.append(
- [
- CheckoutLineInfo(
- line=line,
- variant=variants_map[line.variant_id],
- channel_listing=channel_listings_map[
- line.variant_id
- ],
- product=products_map[line.variant_id],
- collections=collections_map[line.variant_id],
- )
- for line in lines
- ]
- )
- return lines_info
-
- variants = ProductVariantByIdLoader(self.context).load_many(
- variants_pks
- )
- products = ProductByVariantIdLoader(self.context).load_many(
- variants_pks
+ channel_pks = [checkout.channel_id for checkout in checkouts]
+
+ def with_variants_products_collections(results):
+ variants, products, collections, channel_listings = results
+ variants_map = dict(zip(variants_pks, variants))
+ products_map = dict(zip(variants_pks, products))
+ collections_map = dict(zip(variants_pks, collections))
+ channel_listings_map = dict(
+ zip(variant_ids_channel_ids, channel_listings)
)
- collections = CollectionsByVariantIdLoader(self.context).load_many(
- variants_pks
+
+ lines_info_map = defaultdict(list)
+ for checkout, lines in zip(checkouts, checkout_lines):
+ lines_info_map[checkout.pk].extend(
+ [
+ CheckoutLineInfo(
+ line=line,
+ variant=variants_map[line.variant_id],
+ channel_listing=channel_listings_map[
+ (line.variant_id, checkout.channel_id)
+ ],
+ product=products_map[line.variant_id],
+ collections=collections_map[line.variant_id],
+ )
+ for line in lines
+ ]
+ )
+ return [lines_info_map[key] for key in keys]
+
+ variants = ProductVariantByIdLoader(self.context).load_many(variants_pks)
+ products = ProductByVariantIdLoader(self.context).load_many(variants_pks)
+ collections = CollectionsByVariantIdLoader(self.context).load_many(
+ variants_pks
+ )
+
+ variant_ids_channel_ids = []
+ for channel_id, lines in zip(channel_pks, checkout_lines):
+ variant_ids_channel_ids.extend(
+ [(line.variant_id, channel_id) for line in lines]
)
- variant_ids_channel_slug = [
- (variant_pk, channel.slug) for variant_pk in variants_pks
- ]
- channel_listings = VariantChannelListingByVariantIdAndChannelSlugLoader(
- self.context
- ).load_many(variant_ids_channel_slug)
- return Promise.all(
- [variants, products, collections, channel_listings]
- ).then(with_variants_products_collections)
-
- return channel.then(map_variant_ids_with_channel)
-
- return (
- CheckoutLinesByCheckoutTokenLoader(self.context)
- .load_many(keys)
- .then(with_checkout_lines)
+ channel_listings = VariantChannelListingByVariantIdAndChannelIdLoader(
+ self.context
+ ).load_many(variant_ids_channel_ids)
+ return Promise.all(
+ [variants, products, collections, channel_listings]
+ ).then(with_variants_products_collections)
+
+ checkouts = CheckoutByTokenLoader(self.context).load_many(keys)
+ checkout_lines = CheckoutLinesByCheckoutTokenLoader(self.context).load_many(
+ keys
)
+ return Promise.all([checkouts, checkout_lines]).then(with_checkout_lines)
class CheckoutByIdLoader(DataLoader):
diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.py
index e2063073ff..f20074511a 100644
--- a/saleor/graphql/product/dataloaders/__init__.py
+++ b/saleor/graphql/product/dataloaders/__init__.py
@@ -26,6 +26,7 @@ from .products import (
ProductVariantByIdLoader,
ProductVariantChannelListingByIdLoader,
ProductVariantsByProductIdLoader,
+ VariantChannelListingByVariantIdAndChannelIdLoader,
VariantChannelListingByVariantIdAndChannelSlugLoader,
VariantChannelListingByVariantIdLoader,
VariantsChannelListingByProductIdAndChanneSlugLoader,
@@ -58,6 +59,7 @@ __all__ = [
"SelectedAttributesByProductVariantIdLoader",
"VariantAttributesByProductTypeIdLoader",
"VariantChannelListingByVariantIdAndChannelSlugLoader",
+ "VariantChannelListingByVariantIdAndChannelIdLoader",
"VariantChannelListingByVariantIdLoader",
"VariantsChannelListingByProductIdAndChanneSlugLoader",
]
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index 3faf627ab5..fa04e372f8 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -192,10 +192,11 @@ class VariantChannelListingByVariantIdLoader(DataLoader):
]
-class VariantChannelListingByVariantIdAndChannelSlugLoader(
+class VariantChannelListingByVariantIdAndChannelLoader(
DataLoader[VariantIdAndChannelSlug, ProductVariantChannelListing]
):
context_key = "variantchannelisting_by_variant_and_channel"
+ field = ""
def batch_load(self, keys):
# Split the list of keys by channel first. A typical query will only touch
@@ -204,30 +205,27 @@ class VariantChannelListingByVariantIdAndChannelSlugLoader(
variant_channel_listing_by_channel: DefaultDict[str, List[int]] = defaultdict(
list
)
- for variant_id, channel_slug in keys:
- variant_channel_listing_by_channel[channel_slug].append(variant_id)
+ for variant_id, channel in keys:
+ variant_channel_listing_by_channel[channel].append(variant_id)
# For each channel execute a single query for all product variants.
variant_channel_listing_by_variant_and_channel: DefaultDict[
VariantIdAndChannelSlug, Optional[ProductVariantChannelListing]
] = defaultdict()
- for channel_slug, variant_ids in variant_channel_listing_by_channel.items():
- variant_channel_listings = self.batch_load_channel(
- channel_slug, variant_ids
- )
+ for channel, variant_ids in variant_channel_listing_by_channel.items():
+ variant_channel_listings = self.batch_load_channel(channel, variant_ids)
for variant_id, variant_channel_listing in variant_channel_listings:
variant_channel_listing_by_variant_and_channel[
- (variant_id, channel_slug)
+ (variant_id, channel)
] = variant_channel_listing
return [variant_channel_listing_by_variant_and_channel[key] for key in keys]
def batch_load_channel(
- self, channel_slug: str, variant_ids: Iterable[int]
+ self, channel: str, variant_ids: Iterable[int]
) -> Iterable[Tuple[int, Optional[ProductVariantChannelListing]]]:
- variant_channel_listings = ProductVariantChannelListing.objects.filter(
- channel__slug=channel_slug, variant_id__in=variant_ids
- )
+ filter = {f"channel__{self.field}": channel, "variant_id__in": variant_ids}
+ variant_channel_listings = ProductVariantChannelListing.objects.filter(**filter)
variant_channel_listings_map: Dict[int, ProductVariantChannelListing] = {}
for variant_channel_listing in variant_channel_listings.iterator():
@@ -241,6 +239,20 @@ class VariantChannelListingByVariantIdAndChannelSlugLoader(
]
+class VariantChannelListingByVariantIdAndChannelSlugLoader(
+ VariantChannelListingByVariantIdAndChannelLoader
+):
+ context_key = "variantchannelisting_by_variant_and_channelslug"
+ field = "slug"
+
+
+class VariantChannelListingByVariantIdAndChannelIdLoader(
+ VariantChannelListingByVariantIdAndChannelLoader
+):
+ context_key = "variantchannelisting_by_variant_and_channelid"
+ field = "id"
+
+
class VariantsChannelListingByProductIdAndChanneSlugLoader(
DataLoader[ProductIdAndChannelSlug, Iterable[ProductVariantChannelListing]]
):