Skip to content

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

Add filtering capabilities to the top-level attributes' field

NyanKiyoshife645a29f785
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c3c4e1d9b4..280453e831 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Attributes can now be flagged as required. - #4340 by @NyanKiyoshi.
- Added a search position field for the storefront navigation to the attributes. - #4343 by @NyanKiyoshi
- Deprecate the slug field for value input in attributes, and pass by ID instead. - #4348 by @NyanKiyoshi
+- Add filtering capabilities to the top-level attributes' field. - #4386 by @NyanKiyoshi
## [Unreleased]
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index 75ccde6b58..fc4b7d9351 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -163,6 +163,13 @@ def filter_product_type(qs, _, value):
return qs
+def filter_attributes_search(qs, _, value):
+ search_fields = ("name", "slug")
+ if value:
+ qs = filter_by_query_param(qs, value, search_fields)
+ return qs
+
+
class ProductFilter(django_filters.FilterSet):
is_published = django_filters.BooleanFilter()
collections = GlobalIDMultipleChoiceFilter(method=filter_collections)
@@ -212,3 +219,18 @@ class ProductTypeFilter(django_filters.FilterSet):
class Meta:
model = ProductType
fields = ["configurable", "product_type"]
+
+
+class AttributeFilter(django_filters.FilterSet):
+ # Search by attribute name and slug
+ search = django_filters.CharFilter(method=filter_attributes_search)
+
+ class Meta:
+ model = Attribute
+ fields = [
+ "value_required",
+ "is_variant_only",
+ "visible_in_storefront",
+ "filterable_in_storefront",
+ "filterable_in_dashboard",
+ ]
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 404aa49d05..6c10cb3a7c 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -25,7 +25,7 @@ from .bulk_mutations.products import (
ProductVariantBulkDelete,
)
from .enums import StockAvailability
-from .filters import CollectionFilter, ProductFilter, ProductTypeFilter
+from .filters import AttributeFilter, CollectionFilter, ProductFilter, ProductTypeFilter
from .mutations.attributes import (
AttributeAssign,
AttributeClearMeta,
@@ -131,6 +131,11 @@ class ProductTypeFilterInput(FilterInputObjectType):
filterset_class = ProductTypeFilter
+class AttributeFilterInput(FilterInputObjectType):
+ class Meta:
+ filterset_class = AttributeFilter
+
+
class ProductQueries(graphene.ObjectType):
digital_content = graphene.Field(
DigitalContent, id=graphene.Argument(graphene.ID, required=True)
@@ -141,7 +146,7 @@ class ProductQueries(graphene.ObjectType):
level=graphene.Argument(graphene.Int),
description="List of the digital contents.",
)
- attributes = PrefetchingConnectionField(
+ attributes = FilterInputConnectionField(
Attribute,
description="List of the shop's attributes.",
query=graphene.String(description=DESCRIPTIONS["attributes"]),
@@ -155,6 +160,7 @@ class ProductQueries(graphene.ObjectType):
description="""Return attributes for products
belonging to the given collection.""",
),
+ filter=AttributeFilterInput(),
)
attribute = graphene.Field(
Attribute,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 833a7580b0..f7da74b78b 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -218,6 +218,15 @@ type AttributeDelete {
attribute: Attribute
}
+input AttributeFilterInput {
+ valueRequired: Boolean
+ isVariantOnly: Boolean
+ visibleInStorefront: Boolean
+ filterableInStorefront: Boolean
+ filterableInDashboard: Boolean
+ search: String
+}
+
input AttributeInput {
slug: String!
value: String!
@@ -2824,7 +2833,7 @@ type Query {
shippingZones(before: String, after: String, first: Int, last: Int): ShippingZoneCountableConnection
digitalContent(id: ID!): DigitalContent
digitalContents(query: String, level: Int, before: String, after: String, first: Int, last: Int): DigitalContentCountableConnection
- attributes(query: String, inCategory: ID, inCollection: ID, before: String, after: String, first: Int, last: Int): AttributeCountableConnection
+ attributes(query: String, inCategory: ID, inCollection: ID, filter: AttributeFilterInput, before: String, after: String, first: Int, last: Int): AttributeCountableConnection
attribute(id: ID!): Attribute
categories(query: String, level: Int, before: String, after: String, first: Int, last: Int): CategoryCountableConnection
category(id: ID!): Category
diff --git a/saleor/static/dashboard-next/types/globalTypes.ts b/saleor/static/dashboard-next/types/globalTypes.ts
index a8a51baa26..0ac477a5e8 100644
--- a/saleor/static/dashboard-next/types/globalTypes.ts
+++ b/saleor/static/dashboard-next/types/globalTypes.ts
@@ -11,6 +11,11 @@ export enum AddressTypeEnum {
SHIPPING = "SHIPPING"
}
+export enum AttributeInputTypeEnum {
+ DROPDOWN = "DROPDOWN",
+ MULTISELECT = "MULTISELECT",
+}
+
export enum AttributeValueType {
COLOR = "COLOR",
GRADIENT = "GRADIENT",
@@ -240,9 +245,16 @@ export interface AddressInput {
}
export interface AttributeCreateInput {
+ inputType?: AttributeInputTypeEnum | null;
name: string;
slug?: string | null;
values?: (AttributeValueCreateInput | null)[] | null;
+ valueRequired?: boolean | null;
+ isVariantOnly?: boolean | null;
+ visibleInStorefront?: boolean | null;
+ filterableInStorefront?: boolean | null;
+ filterableInDashboard?: boolean | null;
+ storefrontSearchPosition?: number | null;
}
export interface AttributeInput {
@@ -255,6 +267,12 @@ export interface AttributeUpdateInput {
slug?: string | null;
removeValues?: (string | null)[] | null;
addValues?: (AttributeValueCreateInput | null)[] | null;
+ valueRequired?: boolean | null;
+ isVariantOnly?: boolean | null;
+ visibleInStorefront?: boolean | null;
+ filterableInStorefront?: boolean | null;
+ filterableInDashboard?: boolean | null;
+ storefrontSearchPosition?: number | null;
}
export interface AttributeValueCreateInput {
@@ -263,8 +281,10 @@ export interface AttributeValueCreateInput {
}
export interface AttributeValueInput {
- slug: string;
- value: string;
+ id?: string | null;
+ name?: string | null;
+ slug?: string | null;
+ values: (string | null)[];
}
export interface AuthorizationKeyInput {
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index 23f9c3dd23..2fe741ea99 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -1409,3 +1409,44 @@ def test_sort_variant_attributes_within_product_type(
assert gql_attr_id == str(db_attr.pk)
current_sort_pos += 1
+
+
+ATTRIBUTES_FILTER_QUERY = """
+ query($filters: AttributeFilterInput!) {
+ attributes(first: 10, filter: $filters) {
+ edges {
+ node {
+ name
+ slug
+ }
+ }
+ }
+ }
+"""
+
+
+def test_search_attributes(api_client, color_attribute, size_attribute):
+ variables = {"filters": {"search": "color"}}
+
+ attributes = get_graphql_content(
+ api_client.post_graphql(ATTRIBUTES_FILTER_QUERY, variables)
+ )["data"]["attributes"]["edges"]
+
+ assert len(attributes) == 1
+ assert attributes[0]["node"]["slug"] == "color"
+
+
+def test_filter_attributes_if_filterable_in_dashboard(
+ api_client, color_attribute, size_attribute
+):
+ color_attribute.filterable_in_dashboard = False
+ color_attribute.save(update_fields=["filterable_in_dashboard"])
+
+ variables = {"filters": {"filterableInDashboard": True}}
+
+ attributes = get_graphql_content(
+ api_client.post_graphql(ATTRIBUTES_FILTER_QUERY, variables)
+ )["data"]["attributes"]["edges"]
+
+ assert len(attributes) == 1
+ assert attributes[0]["node"]["slug"] == "size"