Skip to content

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

Prefetch staff related with product_updated webhook

Damian W5a8386c0af48
diff --git a/saleor/graphql/meta/mutations.py b/saleor/graphql/meta/mutations.py
index 9f2d39e6cb..50c38c57a6 100644
--- a/saleor/graphql/meta/mutations.py
+++ b/saleor/graphql/meta/mutations.py
@@ -47,7 +47,8 @@ class BaseMetadataMutation(BaseMutation):
@classmethod
def get_instance(cls, info, **data):
object_id = data.get("id")
- return cls.get_node_or_error(info, object_id)
+ qs = data.get("qs", None)
+ return cls.get_node_or_error(info, object_id, qs=qs)
@classmethod
def validate_model_is_model_with_metadata(cls, model, object_id):
@@ -114,6 +115,10 @@ class BaseMetadataMutation(BaseMutation):
"""Run extra metadata method based on mutating model."""
type_name, _ = graphene.Node.from_global_id(data["id"])
if MODEL_EXTRA_METHODS.get(type_name):
+ if type_name == product_models.Product.__name__:
+ data[
+ "qs"
+ ] = product_models.Product.objects.prefetched_product_for_webhook()
instance = cls.get_instance(info, **data)
MODEL_EXTRA_METHODS[type_name](instance, info, **data)
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index b598e676aa..480538d23f 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -74,9 +74,7 @@ class CollectionBulkDelete(ModelBulkDeleteMutation):
def bulk_action(cls, info, queryset):
collections_ids = queryset.values_list("id", flat=True)
products = list(
- models.Product.objects.prefetch_related(
- "attributes", "collections", "variants", "category"
- )
+ models.Product.objects.prefetched_product_for_webhook()
.filter(collections__in=collections_ids)
.distinct()
)
diff --git a/saleor/graphql/product/mutations/channels.py b/saleor/graphql/product/mutations/channels.py
index cf54b4ca96..3caaa1033a 100644
--- a/saleor/graphql/product/mutations/channels.py
+++ b/saleor/graphql/product/mutations/channels.py
@@ -8,11 +8,9 @@ from django.db import transaction
from ....core.permissions import ProductPermissions
from ....product.error_codes import CollectionErrorCode, ProductErrorCode
-from ....product.models import (
- CollectionChannelListing,
- ProductChannelListing,
- ProductVariantChannelListing,
-)
+from ....product.models import CollectionChannelListing
+from ....product.models import Product as ProductModel
+from ....product.models import ProductChannelListing, ProductVariantChannelListing
from ....product.tasks import update_product_discounted_price_task
from ...channel import ChannelContext
from ...channel.mutations import BaseChannelListingMutation
@@ -30,7 +28,6 @@ from ..types.products import Collection, Product, ProductVariant
if TYPE_CHECKING:
from ....channel.models import Channel as ChannelModel
from ....product.models import Collection as CollectionModel
- from ....product.models import Product as ProductModel
from ....product.models import ProductVariant as ProductVariantModel
ErrorType = DefaultDict[str, List[ValidationError]]
@@ -178,7 +175,8 @@ class ProductChannelListingUpdate(BaseChannelListingMutation):
@classmethod
def perform_mutation(cls, _root, info, id, input):
- product = cls.get_node_or_error(info, id, only_type=Product, field="id")
+ qs = ProductModel.objects.prefetched_product_for_webhook()
+ product = cls.get_node_or_error(info, id, only_type=Product, field="id", qs=qs)
errors = defaultdict(list)
cleaned_input = cls.clean_channels(
@@ -339,8 +337,9 @@ class ProductVariantChannelListingUpdate(BaseMutation):
@classmethod
def perform_mutation(cls, _root, info, id, input):
+ qs = ProductVariantModel.objects.prefetch_variant_updated()
variant: "ProductVariantModel" = cls.get_node_or_error( # type: ignore
- info, id, only_type=ProductVariant, field="id"
+ info, id, only_type=ProductVariant, field="id", qs=qs
)
errors = defaultdict(list)
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index f06aab462b..78705e85ab 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -230,9 +230,7 @@ class CollectionCreate(ModelMutation):
@classmethod
def post_save_action(cls, info, instance, cleaned_input):
- products = instance.products.prefetch_related(
- "attributes", "collections", "variants", "category"
- ).all()
+ products = instance.products.prefetched_product_for_webhook().all()
for product in products:
info.context.plugins.product_updated(product)
@@ -285,11 +283,7 @@ class CollectionDelete(ModelDeleteMutation):
node_id = kwargs.get("id")
instance = cls.get_node_or_error(info, node_id, only_type=Collection)
- products = list(
- instance.products.prefetch_related(
- "attributes", "collections", "variants", "category"
- ).all()
- )
+ products = list(instance.products.prefetched_product_for_webhook().all())
result = super().perform_mutation(_root, info, **kwargs)
for product in products:
@@ -412,9 +406,7 @@ class CollectionAddProducts(BaseMutation):
products,
"products",
Product,
- qs=models.Product.objects.prefetch_related(
- "attributes", "collections", "variants", "category"
- ),
+ qs=models.Product.objects.prefetched_product_for_webhook(),
)
cls.clean_products(products)
collection.products.add(*products)
@@ -476,9 +468,7 @@ class CollectionRemoveProducts(BaseMutation):
products,
"products",
only_type=Product,
- qs=models.Product.objects.prefetch_related(
- "attributes", "collections", "variants", "category"
- ),
+ qs=models.Product.objects.prefetched_product_for_webhook(),
)
collection.products.remove(*products)
for product in products:
@@ -645,7 +635,7 @@ class ProductCreate(ModelMutation):
qs = cls.Meta.model.objects.prefetch_related(
"product_type__product_attributes__values",
"product_type__attributeproduct",
- )
+ ).prefetched_product_for_webhook()
return cls.get_node_or_error(info, object_id, only_type="Product", qs=qs)
return super().get_instance(info, **data)
@@ -706,6 +696,7 @@ class ProductUpdate(ProductCreate):
@classmethod
def post_save_action(cls, info, instance, cleaned_input):
+ # TODO: add prefetch related
info.context.plugins.product_updated(instance)
@@ -1433,8 +1424,9 @@ class ProductVariantSetDefault(BaseMutation):
@classmethod
def perform_mutation(cls, _root, info, product_id, variant_id):
+ qs = models.Product.objects.prefetched_product_for_webhook()
product = cls.get_node_or_error(
- info, product_id, field="product_id", only_type=Product
+ info, product_id, field="product_id", only_type=Product, qs=qs
)
variant = cls.get_node_or_error(
info,
@@ -1488,7 +1480,7 @@ class ProductVariantReorder(BaseMutation):
pk = from_global_id_strict_type(product_id, only_type=Product, field="id")
try:
- product = models.Product.objects.prefetch_related("variants").get(pk=pk)
+ product = models.Product.objects.prefetched_product_for_webhook().get(pk=pk)
except ObjectDoesNotExist:
raise ValidationError(
{
@@ -1574,8 +1566,9 @@ class VariantMediaAssign(BaseMutation):
media = cls.get_node_or_error(
info, media_id, field="media_id", only_type=ProductMedia
)
+ qs = models.ProductVariant.objects.prefetch_variant_updated()
variant = cls.get_node_or_error(
- info, variant_id, field="variant_id", only_type=ProductVariant
+ info, variant_id, field="variant_id", only_type=ProductVariant, qs=qs
)
if media and variant:
# check if the given image and variant can be matched together
@@ -1621,8 +1614,9 @@ class VariantMediaUnassign(BaseMutation):
media = cls.get_node_or_error(
info, media_id, field="image_id", only_type=ProductMedia
)
+ qs = models.ProductVariant.objects.prefetch_variant_updated()
variant = cls.get_node_or_error(
- info, variant_id, field="variant_id", only_type=ProductVariant
+ info, variant_id, field="variant_id", only_type=ProductVariant, qs=qs
)
try:
diff --git a/saleor/graphql/translations/mutations.py b/saleor/graphql/translations/mutations.py
index b88fad4673..bdc920dd97 100644
--- a/saleor/graphql/translations/mutations.py
+++ b/saleor/graphql/translations/mutations.py
@@ -146,7 +146,9 @@ class ProductVariantTranslate(BaseTranslateMutation):
)
variant_pk = from_global_id_strict_type(data["id"], ProductVariant, field="id")
- variant = product_models.ProductVariant.objects.get(pk=variant_pk)
+ variant = product_models.ProductVariant.objects.prefetch_variant_updated().get(
+ pk=variant_pk
+ )
variant.translations.update_or_create(
language_code=data["language_code"], defaults=data["input"]
)
diff --git a/saleor/product/models.py b/saleor/product/models.py
index fd35d69124..0416669b07 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -299,6 +299,16 @@ class ProductsQueryset(models.QuerySet):
f"{ordering}name",
)
+ def prefetched_product_for_webhook(self):
+ return self.prefetch_related(
+ "attributes__values",
+ "attributes__assignment__attribute",
+ "collections",
+ "variants__stocks",
+ "variants__stocks__allocations",
+ "category",
+ )
+
class Product(SeoModel, ModelWithMetadata):
product_type = models.ForeignKey(
@@ -407,6 +417,15 @@ class ProductVariantQueryset(models.QuerySet):
),
)
+ def prefetch_variant_updated(self):
+ return self.prefetch_related(
+ "channel_listings",
+ "attributes__values",
+ "attributes__assignment__attribute",
+ "variant_media__media",
+ "product",
+ )
+
class ProductChannelListing(PublishableModel):
product = models.ForeignKey(
diff --git a/saleor/product/utils/__init__.py b/saleor/product/utils/__init__.py
index 2df6cf34ec..9fb287abab 100644
--- a/saleor/product/utils/__init__.py
+++ b/saleor/product/utils/__init__.py
@@ -44,9 +44,7 @@ def delete_categories(categories_ids: List[str], manager):
categories = Category.objects.select_for_update().filter(pk__in=categories_ids)
categories.prefetch_related("products")
- products = Product.objects.prefetch_related(
- "attributes", "collections", "variants", "category"
- ).none()
+ products = Product.objects.prefetched_product_for_webhook().none()
for category in categories:
products = products | collect_categories_tree_products(category)