Skip to content

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

Use replica in resolve_ancestors resolver (#16461)

Marcin Gębala5a157ba32ce5
diff --git a/saleor/graphql/product/tests/test_category.py b/saleor/graphql/product/tests/test_category.py
index 9860ccf6be..e95d076e75 100644
--- a/saleor/graphql/product/tests/test_category.py
+++ b/saleor/graphql/product/tests/test_category.py
@@ -78,6 +78,28 @@ def test_category_query_by_id(user_api_client, product, channel_USD):
assert len(category_data["children"]["edges"]) == category.get_children().count()
+def test_category_query_with_ancestors(user_api_client, product, channel_USD):
+ # given
+ category = Category.objects.first()
+ child = Category.objects.create(
+ name="Child Category", slug="child-category", parent=category
+ )
+
+ # when
+ variables = {
+ "id": graphene.Node.to_global_id("Category", child.pk),
+ "channel": channel_USD.slug,
+ }
+ response = user_api_client.post_graphql(QUERY_CATEGORY, variables=variables)
+ content = get_graphql_content(response)
+ category_data = content["data"]["category"]
+
+ # then
+ assert category_data is not None
+ assert len(category_data["ancestors"]["edges"]) == child.get_ancestors().count()
+ assert len(category_data["children"]["edges"]) == child.get_children().count()
+
+
def test_category_query_invalid_id(user_api_client, product, channel_USD):
category_id = "'"
variables = {
diff --git a/saleor/graphql/product/types/categories.py b/saleor/graphql/product/types/categories.py
index 8e7ba024a6..ea2423cc50 100644
--- a/saleor/graphql/product/types/categories.py
+++ b/saleor/graphql/product/types/categories.py
@@ -110,8 +110,12 @@ class Category(ModelObjectType[models.Category]):
@staticmethod
def resolve_ancestors(root: models.Category, info, **kwargs):
+ database_connection_name = get_database_connection_name(info.context)
return create_connection_slice(
- root.get_ancestors(), info, kwargs, CategoryCountableConnection
+ root.get_ancestors().using(database_connection_name),
+ info,
+ kwargs,
+ CategoryCountableConnection,
)
@staticmethod