Skip to content

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

Optimize products query; include products from subcategories when resolving a category

Marcin Gębala18311ca9211d
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 36d5cf64e7..c607d53c41 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -338,8 +338,10 @@ class Category(CountableDjangoObjectType):
return self.get_absolute_url()
def resolve_products(self, info, **kwargs):
- qs = models.Product.objects.available_products()
- qs = qs.filter(category=self)
+ qs = models.Product.objects.available_products().prefetch_related(
+ 'variants', 'images', 'product_type')
+ categories_tree = self.get_descendants(include_self=True)
+ qs = qs.filter(category__in=categories_tree)
return qs.distinct()