Skip to content

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

Filter attributes by category ID instead of PK

Patryk Zawadzki37059a3a7f84
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 401fe73fc6..595454cabe 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -31,7 +31,7 @@ class Query(graphene.ObjectType):
info, id, only_type=Product)
def resolve_attributes(self, info, in_category=None):
- return resolve_attributes(in_category)
+ return resolve_attributes(in_category, info)
schema = graphene.Schema(Query)
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 77e67b1bcf..be3b3353d6 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -167,13 +167,16 @@ def resolve_product(id, info):
return products.first()
-def resolve_attributes(category_pk):
+def resolve_attributes(category_id, info):
queryset = models.ProductAttribute.objects.prefetch_related('values')
- if category_pk:
+ if category_id:
# Get attributes that are used with product types
# within the given category.
- tree = models.Category.objects.get(pk=category_pk).get_descendants(
- include_self=True)
+ category = graphene.Node.get_node_from_global_id(
+ info, category_id, only_type=Category)
+ if category is None:
+ return queryset.none()
+ tree = category.get_descendants(include_self=True)
product_types = {
obj[0]
for obj in models.Product.objects.filter(
diff --git a/tests/test_graphql.py b/tests/test_graphql.py
index fdcfada79f..74ad837460 100644
--- a/tests/test_graphql.py
+++ b/tests/test_graphql.py
@@ -217,7 +217,7 @@ def test_attributes_in_category_query(client, product_in_stock):
category = Category.objects.first()
query = """
query {
- attributes(inCategory: %(category_pk)s) {
+ attributes(inCategory: "%(category_id)s") {
edges {
node {
id
@@ -232,7 +232,9 @@ def test_attributes_in_category_query(client, product_in_stock):
}
}
}
- """ % {'category_pk': category.pk}
+ """ % {'category_id': graphene.Node.to_global_id('Category', category.id)}
response = client.post('/graphql/', {'query': query})
content = get_content(response)
assert 'errors' not in content
+ attributes_data = content['data']['attributes']['edges']
+ assert len(attributes_data) == ProductAttribute.objects.count()