Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Order line thumbnail performance (#7224)
Tomasz Szymański4902175839c9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex acac667a62..a900436dcc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -65,6 +65,7 @@ All notable, unreleased changes to this project will be documented in this file.- Performance upgrade on orders query with `totalAuthorized` field - #7170 by @tomaszszymanski129- Fix export with empty assignment values - #7207 by @IKarbowiak- Change exported file name - #7218 by @IKarbowiak+- Performance upgrade on `OrderLine` type with `thumbnail` field - #7224 by @tomaszszymanski129### Breaking- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysockidiff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex f0d052933a..53d33712c3 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -29,6 +29,7 @@ from ...payment.model_helpers import (get_total_authorized,get_total_captured,)+from ...product import ProductMediaTypesfrom ...product.product_images import get_product_image_thumbnailfrom ..account.dataloaders import AddressByIdLoader, UserByUserIdLoaderfrom ..account.types import User@@ -49,7 +50,10 @@ from ..invoice.types import Invoicefrom ..meta.types import ObjectWithMetadatafrom ..payment.types import OrderAction, Payment, PaymentChargeStatusEnumfrom ..product.dataloaders import (+ MediaByProductVariantIdLoader,+ ProductByVariantIdLoader,ProductChannelListingByProductIdAndChannelSlugLoader,+ ProductImageByProductIdLoader,ProductVariantByIdLoader,)from ..product.types import ProductVariant@@ -441,14 +445,46 @@ class OrderLine(CountableDjangoObjectType):@staticmethod@traced_resolverdef resolve_thumbnail(root: models.OrderLine, info, *, size=255):- if not root.variant:+ if not root.variant_id:return None- image = root.variant.get_first_image()- if image:++ def _get_image_from_media(image):url = get_product_image_thumbnail(image, size, method="thumbnail")alt = image.altreturn Image(alt=alt, url=info.context.build_absolute_uri(url))- return None++ def _get_first_variant_image(all_medias):+ if image := next(+ (+ media+ for media in all_medias+ if media.type == ProductMediaTypes.IMAGE+ ),+ None,+ ):+ return image++ def _get_first_product_image(images):+ return _get_image_from_media(images[0]) if images else None++ def _resolve_thumbnail(result):+ product, variant_medias = result++ if image := _get_first_variant_image(variant_medias):+ return _get_image_from_media(image)++ # we failed to get image from variant, lets use first from product+ return (+ ProductImageByProductIdLoader(info.context)+ .load(product.id)+ .then(_get_first_product_image)+ )++ variants_product = ProductByVariantIdLoader(info.context).load(root.variant_id)+ variant_medias = MediaByProductVariantIdLoader(info.context).load(+ root.variant_id+ )+ return Promise.all([variants_product, variant_medias]).then(_resolve_thumbnail)@staticmethoddef resolve_unit_price(root: models.OrderLine, _info):diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.pyindex d941c03f9d..587e6b4811 100644--- a/saleor/graphql/product/dataloaders/__init__.py+++ b/saleor/graphql/product/dataloaders/__init__.py@@ -23,6 +23,7 @@ from .products import (ProductChannelListingByIdLoader,ProductChannelListingByProductIdAndChannelSlugLoader,ProductChannelListingByProductIdLoader,+ ProductImageByProductIdLoader,ProductMediaByIdLoader,ProductTypeByIdLoader,ProductTypeByProductIdLoader,@@ -56,6 +57,7 @@ __all__ = ["ProductTypeByVariantIdLoader","ProductChannelListingByIdLoader","ProductChannelListingByProductIdLoader",+ "ProductImageByProductIdLoader","ProductChannelListingByProductIdAndChannelSlugLoader","ProductTypeByIdLoader","ProductVariantByIdLoader",diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.pyindex c68a18abee..266f3b36ae 100644--- a/saleor/graphql/product/dataloaders/products.py+++ b/saleor/graphql/product/dataloaders/products.py@@ -382,6 +382,19 @@ class ProductImageByIdLoader(DataLoader):return [images.get(product_image_id) for product_image_id in keys]+class ProductImageByProductIdLoader(DataLoader):+ context_key = "product_image_by_product_id"++ def batch_load(self, keys):+ medias = ProductMedia.objects.filter(+ type=ProductMediaTypes.IMAGE, product_id__in=keys+ )+ product_id_medias_map = defaultdict(list)+ for media in medias:+ product_id_medias_map[media.product_id].append(media)+ return [product_id_medias_map.get(product_id, []) for product_id in keys]++class MediaByProductVariantIdLoader(DataLoader):context_key = "media_by_product_variant"diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 07a006ec49..57850b08f9 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -539,11 +539,6 @@ class ProductVariant(SortableModel, ModelWithMetadata):)return smart_text(product_display)- def get_first_image(self) -> "ProductMedia":- all_media = self.media.all()- images = [media for media in all_media if media.type == ProductMediaTypes.IMAGE]- return images[0] if images else self.product.get_first_image()-def get_ordering_queryset(self):return self.product.variants.all()