Skip to content

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

Improve ImagesByProductVariantIdLoader (#6536)

Damian Wysocki290250bf1253
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 7d2480faac..a42d0ca85c 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -67,6 +67,7 @@ from ...product.models import (
ProductType,
ProductVariant,
ProductVariantChannelListing,
+ VariantImage,
)
from ...product.tasks import update_products_discounted_prices_of_discount_task
from ...product.thumbnails import (
@@ -275,7 +276,7 @@ def create_stocks(variant, warehouse_qs=None, **defaults):
)
-def create_product_variants(variants_data):
+def create_product_variants(variants_data, create_images):
for variant in variants_data:
pk = variant["pk"]
defaults = variant["fields"]
@@ -293,6 +294,9 @@ def create_product_variants(variants_data):
product = variant.product
product.default_variant = variant
product.save(update_fields=["default_variant", "updated_at"])
+ if create_images:
+ image = variant.product.images.filter().first()
+ VariantImage.objects.create(variant=variant, image=image)
quantity = random.randint(100, 500)
create_stocks(variant, quantity=quantity)
@@ -407,7 +411,9 @@ def create_products_by_schema(placeholder_dir, create_images):
create_product_channel_listings(
product_channel_listings_data=types["product.productchannellisting"],
)
- create_product_variants(variants_data=types["product.productvariant"])
+ create_product_variants(
+ variants_data=types["product.productvariant"], create_images=create_images
+ )
create_product_variant_channel_listings(
product_variant_channel_listings_data=types[
"product.productvariantchannellisting"
diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.py
index 99f09d6019..b464dbb1d3 100644
--- a/saleor/graphql/product/dataloaders/__init__.py
+++ b/saleor/graphql/product/dataloaders/__init__.py
@@ -17,6 +17,7 @@ from .products import (
ProductChannelListingByIdLoader,
ProductChannelListingByProductIdAndChannelSlugLoader,
ProductChannelListingByProductIdLoader,
+ ProductImageByIdLoader,
ProductTypeByIdLoader,
ProductVariantByIdLoader,
ProductVariantChannelListingByIdLoader,
@@ -43,6 +44,7 @@ __all__ = [
"ProductVariantByIdLoader",
"ProductVariantChannelListingByIdLoader",
"ProductVariantsByProductIdLoader",
+ "ProductImageByIdLoader",
"ImagesByProductVariantIdLoader",
"SelectedAttributesByProductIdLoader",
"SelectedAttributesByProductVariantIdLoader",
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index 7f70b50fe6..6c20663fd1 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -281,15 +281,38 @@ class VariantsChannelListingByProductIdAndChanneSlugLoader(
]
+class ProductImageByIdLoader(DataLoader):
+ context_key = "product_image_by_id"
+
+ def batch_load(self, keys):
+ product_images = ProductImage.objects.in_bulk(keys)
+ return [product_images.get(product_image_id) for product_image_id in keys]
+
+
class ImagesByProductVariantIdLoader(DataLoader):
context_key = "images_by_product_variant"
def batch_load(self, keys):
- variant_images = VariantImage.objects.filter(variant_id__in=keys)
- image_map = defaultdict(list)
- for variant_image in variant_images:
- image_map[variant_image.variant_id].append(variant_image.image)
- return [image_map[product_id] for product_id in keys]
+ variant_images = VariantImage.objects.filter(variant_id__in=keys).values_list(
+ "variant_id", "image_id"
+ )
+
+ variant_image_pairs = defaultdict(list)
+ for variant_id, image_id in variant_images:
+ variant_image_pairs[variant_id].append(image_id)
+
+ def map_variant_images(images):
+ images_map = {image.id: image for image in images}
+ return [
+ [images_map[image_id] for image_id in variant_image_pairs[variant_id]]
+ for variant_id in keys
+ ]
+
+ return (
+ ProductImageByIdLoader(self.context)
+ .load_many(set(image_id for variant_id, image_id in variant_images))
+ .then(map_variant_images)
+ )
class CollectionByIdLoader(DataLoader):
diff --git a/saleor/graphql/product/tests/benchmark/test_product.py b/saleor/graphql/product/tests/benchmark/test_product.py
index 9b7ad281d3..ca36f85fd7 100644
--- a/saleor/graphql/product/tests/benchmark/test_product.py
+++ b/saleor/graphql/product/tests/benchmark/test_product.py
@@ -6,7 +6,7 @@ from ....tests.utils import get_graphql_content
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
-def test_product_details(product, api_client, count_queries, channel_USD):
+def test_product_details(product_with_image, api_client, count_queries, channel_USD):
query = """
fragment BasicProductFields on Product {
id
@@ -67,6 +67,10 @@ def test_product_details(product, api_client, count_queries, channel_USD):
value: name
}
}
+ images {
+ id
+ url
+ }
}
query ProductDetails($id: ID!, $channel: String) {
@@ -151,6 +155,10 @@ def test_product_details(product, api_client, count_queries, channel_USD):
}
}
"""
+ product = product_with_image
+ variant = product_with_image.variants.first()
+ image = product_with_image.get_first_image()
+ image.variant_images.create(variant=variant)
variables = {
"id": Node.to_global_id("Product", product.pk),
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 332572b97f..69dd9459bd 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -34,7 +34,6 @@ from ..attribute.models import (
AttributeValueTranslation,
)
from ..attribute.utils import associate_attribute_values_to_instance
-from ..checkout import utils
from ..checkout.models import Checkout
from ..checkout.utils import add_variant_to_checkout
from ..core import JobStatus
@@ -52,7 +51,6 @@ from ..discount.models import (
VoucherTranslation,
)
from ..giftcard.models import GiftCard
-from ..invoice.models import Invoice
from ..menu.models import Menu, MenuItem, MenuItemTranslation
from ..order import OrderStatus
from ..order.actions import cancel_fulfillment, fulfill_order_line
@@ -63,7 +61,6 @@ from ..page.models import Page, PageTranslation, PageType
from ..payment import ChargeStatus, TransactionKind
from ..payment.interface import GatewayConfig, PaymentData
from ..payment.models import Payment
-from ..plugins.invoicing.plugin import InvoicingPlugin
from ..plugins.models import PluginConfiguration
from ..plugins.vatlayer.plugin import VatlayerPlugin
from ..product.models import (
@@ -82,6 +79,7 @@ from ..product.models import (
ProductVariant,
ProductVariantChannelListing,
ProductVariantTranslation,
+ VariantImage,
)
from ..product.tests.utils import create_image
from ..shipping.models import (
@@ -968,7 +966,6 @@ def product(product_type, category, warehouse, channel_USD):
product_type=product_type,
category=category,
)
-
ProductChannelListing.objects.create(
product=product,
channel=channel_USD,