Skip to content

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

Add dataloaders for product and variant attribtues in product type

Marcin Gębala8641e44d4621
diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.py
index 9bbc033eed..e047b45640 100644
--- a/saleor/graphql/product/dataloaders/__init__.py
+++ b/saleor/graphql/product/dataloaders/__init__.py
@@ -1,7 +1,9 @@
from .attributes import (
AttributeValuesByAttributeIdLoader,
+ ProductAttributesByProductTypeIdLoader,
SelectedAttributesByProductIdLoader,
SelectedAttributesByProductVariantIdLoader,
+ VariantAttributesByProductTypeIdLoader,
)
from .products import (
CategoryByIdLoader,
@@ -21,6 +23,7 @@ __all__ = [
"CollectionByIdLoader",
"CollectionsByProductIdLoader",
"ImagesByProductIdLoader",
+ "ProductAttributesByProductTypeIdLoader",
"ProductByIdLoader",
"ProductTypeByIdLoader",
"ProductVariantByIdLoader",
@@ -28,4 +31,5 @@ __all__ = [
"ImagesByProductVariantIdLoader",
"SelectedAttributesByProductIdLoader",
"SelectedAttributesByProductVariantIdLoader",
+ "VariantAttributesByProductTypeIdLoader",
]
diff --git a/saleor/graphql/product/dataloaders/attributes.py b/saleor/graphql/product/dataloaders/attributes.py
index f2d610c2e0..4a552c2a1d 100644
--- a/saleor/graphql/product/dataloaders/attributes.py
+++ b/saleor/graphql/product/dataloaders/attributes.py
@@ -36,7 +36,67 @@ class AttributesByAttributeId(DataLoader):
return [attributes.get(key) for key in keys]
+class BaseProductAttributesByProductTypeIdLoader(DataLoader):
+ """Loads product attributes by product type ID."""
+
+ context_key = "product_attributes_by_producttype"
+ model_name = None
+
+ def batch_load(self, keys):
+ if not self.model_name:
+ raise ValueError("Provide a model_name for this dataloader.")
+
+ user = self.user
+ if user.is_active and user.has_perm(ProductPermissions.MANAGE_PRODUCTS):
+ qs = self.model_name.objects.all()
+ else:
+ qs = self.model_name.objects.filter(attribute__visible_in_storefront=True)
+ product_type_attribute_pairs = qs.filter(product_type_id__in=keys).values_list(
+ "product_type_id", "attribute_id"
+ )
+
+ product_type_to_attributes_map = defaultdict(list)
+ for product_type_id, attr_id in product_type_attribute_pairs:
+ product_type_to_attributes_map[product_type_id].append(attr_id)
+
+ def map_attributes(attributes):
+ attributes_map = {attr.id: attr for attr in attributes}
+ return [
+ [
+ attributes_map[attr_id]
+ for attr_id in product_type_to_attributes_map[product_type_id]
+ ]
+ for product_type_id in keys
+ ]
+
+ return (
+ AttributesByAttributeId(self.context)
+ .load_many(set(attr_id for _, attr_id in product_type_attribute_pairs))
+ .then(map_attributes)
+ )
+
+
+class ProductAttributesByProductTypeIdLoader(
+ BaseProductAttributesByProductTypeIdLoader
+):
+ """Loads product attributes by product type ID."""
+
+ context_key = "product_attributes_by_producttype"
+ model_name = AttributeProduct
+
+
+class VariantAttributesByProductTypeIdLoader(
+ BaseProductAttributesByProductTypeIdLoader
+):
+ """Loads variant attributes by product type ID."""
+
+ context_key = "variant_attributes_by_producttype"
+ model_name = AttributeVariant
+
+
class AttributeProductsByProductTypeIdLoader(DataLoader):
+ """Loads AttributeProduct objects by product type ID."""
+
context_key = "attributeproducts_by_producttype"
def batch_load(self, keys):
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 8819265ea4..a7f41b58ad 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -52,12 +52,14 @@ from ..dataloaders import (
CollectionsByProductIdLoader,
ImagesByProductIdLoader,
ImagesByProductVariantIdLoader,
+ ProductAttributesByProductTypeIdLoader,
ProductByIdLoader,
ProductTypeByIdLoader,
ProductVariantByIdLoader,
ProductVariantsByProductIdLoader,
SelectedAttributesByProductIdLoader,
SelectedAttributesByProductVariantIdLoader,
+ VariantAttributesByProductTypeIdLoader,
)
from ..filters import AttributeFilterInput, ProductFilterInput
from ..resolvers import resolve_attributes
@@ -709,12 +711,12 @@ class ProductType(CountableDjangoObjectType):
return root.get_value_from_metadata("vatlayer.code")
@staticmethod
- def resolve_product_attributes(root: models.ProductType, *_args, **_kwargs):
- return root.product_attributes.product_attributes_sorted().all()
+ def resolve_product_attributes(root: models.ProductType, info):
+ return ProductAttributesByProductTypeIdLoader(info.context).load(root.pk)
@staticmethod
- def resolve_variant_attributes(root: models.ProductType, *_args, **_kwargs):
- return root.variant_attributes.variant_attributes_sorted().all()
+ def resolve_variant_attributes(root: models.ProductType, info):
+ return VariantAttributesByProductTypeIdLoader(info.context).load(root.pk)
@staticmethod
def resolve_products(root: models.ProductType, info, **_kwargs):