Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add filtering attributes by collection
Marcin Gębalaac5910935711
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.pyindex 341012c860..713cea8ae8 100644--- a/saleor/graphql/product/resolvers.py+++ b/saleor/graphql/product/resolvers.py@@ -17,24 +17,38 @@ COLLECTION_SEARCH_FIELDS = ('name', 'slug')ATTRIBUTES_SEARCH_FIELDS = ('name', 'slug')-def resolve_attributes(info, category_id, query):+def _filter_attributes_by_product_types(attribute_qs, product_qs):+ product_types = set(product_qs.values_list('product_type_id', flat=True))+ return attribute_qs.filter(+ Q(product_type__in=product_types)+ | Q(product_variant_type__in=product_types))+++def resolve_attributes(info, category_id=None, collection_id=None, query=None):qs = models.Attribute.objects.all()qs = filter_by_query_param(qs, query, ATTRIBUTES_SEARCH_FIELDS)+if category_id:- # Get attributes that are used with product types- # within the given category.+ # Filter attributes by product types belonging to the given category.category = graphene.Node.get_node_from_global_id(info, category_id, Category)- if category is None:- return qs.none()- tree = category.get_descendants(include_self=True)- product_types = {- obj[0]- for obj in models.Product.objects.filter(- category__in=tree).values_list('product_type_id')}- qs = qs.filter(- Q(product_type__in=product_types)- | Q(product_variant_type__in=product_types))+ if category:+ tree = category.get_descendants(include_self=True)+ product_qs = models.Product.objects.filter(category__in=tree)+ qs = _filter_attributes_by_product_types(qs, product_qs)+ else:+ qs = qs.none()++ if collection_id:+ # Filter attributes by product types belonging to the given collection.+ collection = graphene.Node.get_node_from_global_id(+ info, collection_id, Collection)+ if collection:+ product_qs = collection.products.all()+ qs = _filter_attributes_by_product_types(qs, product_qs)+ else:+ qs = qs.none()+qs = qs.order_by('name')qs = qs.distinct()return gql_optimizer.query(qs, info)diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.pyindex fe780143af..e3e6a3ce8b 100644--- a/saleor/graphql/product/schema.py+++ b/saleor/graphql/product/schema.py@@ -1,5 +1,6 @@import graphenefrom graphql_jwt.decorators import permission_required+from textwrap import dedentfrom ..core.fields import PrefetchingConnectionFieldfrom ..core.types import ReportingPeriod@@ -28,9 +29,16 @@ from .types import (class ProductQueries(graphene.ObjectType):attributes = PrefetchingConnectionField(Attribute,+ description='List of the shop\'s attributes.',query=graphene.String(description=DESCRIPTIONS['attributes']),- in_category=graphene.Argument(graphene.ID),- description='List of the shop\'s attributes.')+ in_category=graphene.Argument(+ graphene.ID, description=dedent(+ '''Return attributes for products belonging to the given+ category.''')),+ in_collection=graphene.Argument(+ graphene.ID, description=dedent(+ '''Return attributes for products belonging to the given+ collection.''')),)categories = PrefetchingConnectionField(Category, query=graphene.String(description=DESCRIPTIONS['category']),@@ -85,8 +93,9 @@ class ProductQueries(graphene.ObjectType):ReportingPeriod, required=True, description='Span of time.'),description='List of top selling products.')- def resolve_attributes(self, info, in_category=None, query=None, **kwargs):- return resolve_attributes(info, in_category, query)+ def resolve_attributes(+ self, info, in_category=None, in_collection=None, query=None, **kwargs):+ return resolve_attributes(info, in_category, in_collection, query)def resolve_categories(self, info, level=None, query=None, **kwargs):return resolve_categories(info, level=level, query=query)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex a2950118ec..5f955b48eb 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1576,7 +1576,7 @@ type Query {shop: ShopshippingZone(id: ID!): ShippingZoneshippingZones(before: String, after: String, first: Int, last: Int): ShippingZoneCountableConnection- attributes(query: String, inCategory: ID, before: String, after: String, first: Int, last: Int): AttributeCountableConnection+ attributes(query: String, inCategory: ID, inCollection: ID, before: String, after: String, first: Int, last: Int): AttributeCountableConnectioncategories(query: String, level: Int, before: String, after: String, first: Int, last: Int): CategoryCountableConnectioncategory(id: ID!): Categorycollection(id: ID!): Collectiondiff --git a/saleor/static/dashboard-next/collections/types/CollectionUpdateWithHomepage.ts b/saleor/static/dashboard-next/collections/types/CollectionUpdateWithHomepage.tsindex 29bb28b055..d5bb095c07 100644--- a/saleor/static/dashboard-next/collections/types/CollectionUpdateWithHomepage.ts+++ b/saleor/static/dashboard-next/collections/types/CollectionUpdateWithHomepage.ts@@ -25,7 +25,7 @@ export interface CollectionUpdateWithHomepage_homepageCollectionUpdate_shop {export interface CollectionUpdateWithHomepage_homepageCollectionUpdate {__typename: "HomepageCollectionUpdate";- errors: (CollectionUpdateWithHomepage_homepageCollectionUpdate_errors | null)[] | null;+ errors: CollectionUpdateWithHomepage_homepageCollectionUpdate_errors[] | null;shop: CollectionUpdateWithHomepage_homepageCollectionUpdate_shop | null;}@@ -54,7 +54,7 @@ export interface CollectionUpdateWithHomepage_collectionUpdate_collection {export interface CollectionUpdateWithHomepage_collectionUpdate {__typename: "CollectionUpdate";- errors: (CollectionUpdateWithHomepage_collectionUpdate_errors | null)[] | null;+ errors: CollectionUpdateWithHomepage_collectionUpdate_errors[] | null;collection: CollectionUpdateWithHomepage_collectionUpdate_collection | null;}diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.pyindex 3795803c6e..04732767ad 100644--- a/tests/api/test_attributes.py+++ b/tests/api/test_attributes.py@@ -1,5 +1,6 @@import grapheneimport pytest+from django.db.models import Qfrom django.template.defaultfilters import slugifyfrom saleor.graphql.product.types import (@@ -87,6 +88,37 @@ def test_attributes_in_category_query(user_api_client, product):assert len(attributes_data) == Attribute.objects.count()+def test_attributes_in_collection_query(user_api_client, sale):+ product_types = set(+ sale.products.all().values_list('product_type_id', flat=True))+ expected_attrs = Attribute.objects.filter(+ Q(product_type__in=product_types)+ | Q(product_variant_type__in=product_types))++ query = """+ query {+ attributes(inCollection: "%(collection_id)s", first: 20) {+ edges {+ node {+ id+ name+ slug+ values {+ id+ name+ slug+ }+ }+ }+ }+ }+ """ % {'collection_id': graphene.Node.to_global_id('Collection', sale.id)}+ response = user_api_client.post_graphql(query)+ content = get_graphql_content(response)+ attributes_data = content['data']['attributes']['edges']+ assert len(attributes_data) == len(expected_attrs)++CREATE_ATTRIBUTES_QUERY = """mutation createAttribute($name: String!, $values: [AttributeValueCreateInput],