Skip to content

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

Optimize children field on Category type (#7045)

Iga Karbowiak20c03618225b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e8aaa0ea5a..6659f929b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Remove redundant Opentracing spans - #6994 by @fowczarek
- Support setting value for AttributeValue mutations - #7037 by @piotrgrundas
- Validate discount value for percentage vouchers and sales - #7033 by @d-wysocki
+- Optimize children field on Category type - #7045 by @IKarbowiak
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/product/dataloaders/__init__.py b/saleor/graphql/product/dataloaders/__init__.py
index 0c3afea470..4a82c0e796 100644
--- a/saleor/graphql/product/dataloaders/__init__.py
+++ b/saleor/graphql/product/dataloaders/__init__.py
@@ -6,6 +6,7 @@ from .attributes import (
)
from .products import (
CategoryByIdLoader,
+ CategoryChildrenByCategoryIdLoader,
CollectionByIdLoader,
CollectionChannelListingByCollectionIdAndChannelSlugLoader,
CollectionChannelListingByCollectionIdLoader,
@@ -36,6 +37,7 @@ from .products import (
__all__ = [
"CategoryByIdLoader",
+ "CategoryChildrenByCategoryIdLoader",
"CollectionByIdLoader",
"CollectionChannelListingByCollectionIdAndChannelSlugLoader",
"CollectionChannelListingByCollectionIdLoader",
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index 27a45d02e0..e651cb0f9c 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -511,3 +511,15 @@ class CollectionChannelListingByCollectionIdAndChannelSlugLoader(DataLoader):
collections_channel_listings_by_collection_and_channel_map.get(key, None)
for key in keys
]
+
+
+class CategoryChildrenByCategoryIdLoader(DataLoader):
+ context_key = "categorychildren_by_category"
+
+ def batch_load(self, keys):
+ categories = Category.objects.filter(parent__isnull=False)
+ parent_to_children_mapping = defaultdict(list)
+ for category in categories:
+ parent_to_children_mapping[category.parent_id].append(category)
+
+ return [parent_to_children_mapping.get(key, []) for key in keys]
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 7414870521..5735229281 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -73,6 +73,7 @@ from ...warehouse.dataloaders import (
from ...warehouse.types import Stock
from ..dataloaders import (
CategoryByIdLoader,
+ CategoryChildrenByCategoryIdLoader,
CollectionChannelListingByCollectionIdAndChannelSlugLoader,
CollectionChannelListingByCollectionIdLoader,
CollectionsByProductIdLoader,
@@ -1137,7 +1138,7 @@ class Category(CountableDjangoObjectType):
@staticmethod
def resolve_children(root: models.Category, info, **_kwargs):
- return root.children.all()
+ return CategoryChildrenByCategoryIdLoader(info.context).load(root.pk)
@staticmethod
def resolve_url(root: models.Category, _info):