Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add ImagesByProductVariantIdLoader dataloader
IKarbowiak547fb7f53b3b
diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.pyindex 19b7e30ae7..964a099d29 100644--- a/saleor/graphql/product/dataloaders/__init__.py+++ b/saleor/graphql/product/dataloaders/__init__.py@@ -8,6 +8,7 @@ from .products import (CollectionByIdLoader,CollectionsByProductIdLoader,ImagesByProductIdLoader,+ ImagesByProductVariantIdLoader,ProductByIdLoader,ProductVariantByIdLoader,ProductVariantsByProductIdLoader,@@ -22,6 +23,7 @@ __all__ = ["ProductByIdLoader","ProductVariantByIdLoader","ProductVariantsByProductIdLoader",+ "ImagesByProductVariantIdLoader","SelectedAttributesByProductIdLoader","SelectedAttributesByProductVariantIdLoader",]diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.pyindex eef9f23b9f..a61fb89e4b 100644--- a/saleor/graphql/product/dataloaders/products.py+++ b/saleor/graphql/product/dataloaders/products.py@@ -7,6 +7,7 @@ from ....product.models import (Product,ProductImage,ProductVariant,+ VariantImage,)from ...core.dataloaders import DataLoader@@ -59,6 +60,17 @@ class ProductVariantsByProductIdLoader(DataLoader):return [variant_map.get(product_id, []) for product_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]++class CollectionByIdLoader(DataLoader):context_key = "collection_by_id"diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex a2bddb910f..696c2d0fe7 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -51,6 +51,7 @@ from ..dataloaders import (CategoryByIdLoader,CollectionsByProductIdLoader,ImagesByProductIdLoader,+ ImagesByProductVariantIdLoader,ProductByIdLoader,ProductVariantsByProductIdLoader,SelectedAttributesByProductIdLoader,@@ -400,8 +401,8 @@ class ProductVariant(CountableDjangoObjectType):return calculate_revenue_for_variant(root, start_date)@staticmethod- def resolve_images(root: models.ProductVariant, *_args):- return root.images.all()+ def resolve_images(root: models.ProductVariant, info, *_args):+ return ImagesByProductVariantIdLoader(info.context).load(root.id)@classmethoddef get_node(cls, info, pk):