Skip to content

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

Use dataloader to load product types

Marcin Gębalaeaa2cb41dce7
diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.py
index 964a099d29..9bbc033eed 100644
--- a/saleor/graphql/product/dataloaders/__init__.py
+++ b/saleor/graphql/product/dataloaders/__init__.py
@@ -10,6 +10,7 @@ from .products import (
ImagesByProductIdLoader,
ImagesByProductVariantIdLoader,
ProductByIdLoader,
+ ProductTypeByIdLoader,
ProductVariantByIdLoader,
ProductVariantsByProductIdLoader,
)
@@ -21,6 +22,7 @@ __all__ = [
"CollectionsByProductIdLoader",
"ImagesByProductIdLoader",
"ProductByIdLoader",
+ "ProductTypeByIdLoader",
"ProductVariantByIdLoader",
"ProductVariantsByProductIdLoader",
"ImagesByProductVariantIdLoader",
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index a61fb89e4b..a124d85208 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -6,6 +6,7 @@ from ....product.models import (
CollectionProduct,
Product,
ProductImage,
+ ProductType,
ProductVariant,
VariantImage,
)
@@ -28,6 +29,14 @@ class ProductByIdLoader(DataLoader):
return [products.get(product_id) for product_id in keys]
+class ProductTypeByIdLoader(DataLoader):
+ context_key = "product_type_by_id"
+
+ def batch_load(self, keys):
+ product_types = ProductType.objects.in_bulk(keys)
+ return [product_types.get(product_type_id) for product_type_id in keys]
+
+
class ImagesByProductIdLoader(DataLoader):
context_key = "images_by_product"
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 962cfdb1a8..8819265ea4 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -53,6 +53,7 @@ from ..dataloaders import (
ImagesByProductIdLoader,
ImagesByProductVariantIdLoader,
ProductByIdLoader,
+ ProductTypeByIdLoader,
ProductVariantByIdLoader,
ProductVariantsByProductIdLoader,
SelectedAttributesByProductIdLoader,
@@ -653,6 +654,10 @@ class Product(CountableDjangoObjectType):
def resolve_is_available_for_purchase(root: models.Product, _info):
return root.is_available_for_purchase()
+ @staticmethod
+ def resolve_product_type(root: models.Product, info):
+ return ProductTypeByIdLoader(info.context).load(root.product_type_id)
+
@key(fields="id")
class ProductType(CountableDjangoObjectType):