Skip to content

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

Added capability to search product types by value (through the name)

NyanKiyoshi0dd36817a7fa
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4bf78a3d9e..c451fa37c1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -52,6 +52,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Remove dashboard 2.0 files - #4631 by @dominik-zeglen
- Refactoring of password recovery system - #4617 by @fowczarek
- Added capability to filter attributes per global ID - #4640 by @NyanKiyoshi.
+- Added capability to search product types by value (through the name) - #4647 by @NyanKiyoshi.
## 2.8.0
diff --git a/saleor/graphql/descriptions.py b/saleor/graphql/descriptions.py
index 266bb12b17..e8f54afc79 100644
--- a/saleor/graphql/descriptions.py
+++ b/saleor/graphql/descriptions.py
@@ -8,6 +8,7 @@ from .product.resolvers import (
CATEGORY_SEARCH_FIELDS,
COLLECTION_SEARCH_FIELDS,
PRODUCT_SEARCH_FIELDS,
+ PRODUCT_TYPE_SEARCH_FIELDS,
)
from .utils import generate_query_argument_description
@@ -20,6 +21,7 @@ DESCRIPTIONS = {
"order": generate_query_argument_description(ORDER_SEARCH_FIELDS),
"page": generate_query_argument_description(PAGE_SEARCH_FIELDS),
"product": generate_query_argument_description(PRODUCT_SEARCH_FIELDS),
+ "product_type": generate_query_argument_description(PRODUCT_TYPE_SEARCH_FIELDS),
"voucher": generate_query_argument_description(VOUCHER_SEARCH_FIELDS),
"sale": generate_query_argument_description(SALE_SEARCH_FIELDS),
"user": generate_query_argument_description(USER_SEARCH_FIELDS),
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index 84e9c7fb4a..0a18bb16da 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -22,6 +22,17 @@ from .enums import (
from .types.attributes import AttributeInput
+def filter_fields_containing_value(*search_fields: str):
+ """Create a icontains filters through given fields on a given query set object."""
+
+ def _filter_qs(qs, _, value):
+ if value:
+ qs = filter_by_query_param(qs, value, search_fields)
+ return qs
+
+ return _filter_qs
+
+
def filter_products_by_attributes(qs, filter_value):
attributes = Attribute.objects.prefetch_related("values")
attributes_map = {attribute.slug: attribute.pk for attribute in attributes}
@@ -139,13 +150,6 @@ def filter_collection_publish(qs, _, value):
return qs
-def filter_collection_search(qs, _, value):
- search_fields = ("name", "slug")
- if value:
- qs = filter_by_query_param(qs, value, search_fields)
- return qs
-
-
def filter_product_type_configurable(qs, _, value):
if value == ProductTypeConfigurable.CONFIGURABLE:
qs = qs.filter(has_variants=True)
@@ -162,13 +166,6 @@ 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)
@@ -201,7 +198,9 @@ class CollectionFilter(django_filters.FilterSet):
published = EnumFilter(
input_class=CollectionPublished, method=filter_collection_publish
)
- search = django_filters.CharFilter(method=filter_collection_search)
+ search = django_filters.CharFilter(
+ method=filter_fields_containing_value("slug", "name")
+ )
class Meta:
model = Collection
@@ -209,6 +208,8 @@ class CollectionFilter(django_filters.FilterSet):
class ProductTypeFilter(django_filters.FilterSet):
+ search = django_filters.CharFilter(method=filter_fields_containing_value("name"))
+
configurable = EnumFilter(
input_class=ProductTypeConfigurable, method=filter_product_type_configurable
)
@@ -217,12 +218,14 @@ class ProductTypeFilter(django_filters.FilterSet):
class Meta:
model = ProductType
- fields = ["configurable", "product_type"]
+ fields = ["search", "configurable", "product_type"]
class AttributeFilter(django_filters.FilterSet):
# Search by attribute name and slug
- search = django_filters.CharFilter(method=filter_attributes_search)
+ search = django_filters.CharFilter(
+ method=filter_fields_containing_value("slug", "name")
+ )
ids = GlobalIDMultipleChoiceFilter(field_name="id")
class Meta:
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.py
index 733833e65f..05840c3fcc 100644
--- a/saleor/graphql/product/resolvers.py
+++ b/saleor/graphql/product/resolvers.py
@@ -17,6 +17,7 @@ from .filters import (
)
PRODUCT_SEARCH_FIELDS = ("name", "description")
+PRODUCT_TYPE_SEARCH_FIELDS = ("name",)
CATEGORY_SEARCH_FIELDS = ("name", "slug", "description", "parent__name")
COLLECTION_SEARCH_FIELDS = ("name", "slug")
ATTRIBUTES_SEARCH_FIELDS = ("name", "slug")
@@ -140,8 +141,9 @@ def resolve_products(
return gql_optimizer.query(qs, info)
-def resolve_product_types(info):
+def resolve_product_types(info, query):
qs = models.ProductType.objects.all()
+ qs = filter_by_query_param(qs, query, PRODUCT_TYPE_SEARCH_FIELDS)
qs = qs.order_by("name")
return gql_optimizer.query(qs, info)
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 93664c712a..f76b71a14d 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -216,6 +216,7 @@ class ProductQueries(graphene.ObjectType):
product_types = FilterInputConnectionField(
ProductType,
filter=ProductTypeFilterInput(),
+ query=graphene.String(description=DESCRIPTIONS["product_type"]),
description="List of the shop's product types.",
)
product_variant = graphene.Field(
@@ -271,8 +272,8 @@ class ProductQueries(graphene.ObjectType):
def resolve_product_type(self, info, id):
return graphene.Node.get_node_from_global_id(info, id, ProductType)
- def resolve_product_types(self, info, **_kwargs):
- return resolve_product_types(info)
+ def resolve_product_types(self, info, query=None, **_kwargs):
+ return resolve_product_types(info, query)
def resolve_product_variant(self, info, id):
return graphene.Node.get_node_from_global_id(info, id, ProductVariant)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index a17cae798d..a496127633 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2749,6 +2749,7 @@ enum ProductTypeEnum {
}
input ProductTypeFilterInput {
+ search: String
configurable: ProductTypeConfigurable
productType: ProductTypeEnum
}
@@ -2926,7 +2927,7 @@ type Query {
product(id: ID!): Product
products(filter: ProductFilterInput, attributes: [AttributeScalar], categories: [ID], collections: [ID], priceLte: Float, priceGte: Float, sortBy: ProductOrder, stockAvailability: StockAvailability, query: String, before: String, after: String, first: Int, last: Int): ProductCountableConnection
productType(id: ID!): ProductType
- productTypes(filter: ProductTypeFilterInput, before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection
+ productTypes(filter: ProductTypeFilterInput, query: String, before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection
productVariant(id: ID!): ProductVariant
productVariants(ids: [ID], before: String, after: String, first: Int, last: Int): ProductVariantCountableConnection
reportProductSales(period: ReportingPeriod!, before: String, after: String, first: Int, last: Int): ProductVariantCountableConnection
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index e9b7ab0660..c28ab0cee7 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -2449,3 +2449,50 @@ def test_product_type_filter_unassigned_attributes(
_, attribute_id = graphene.Node.from_global_id(found_attributes[0]["node"]["id"])
assert attribute_id == str(expected_attribute.pk)
+
+
+QUERY_FILTER_PRODUCT_TYPES = """
+ query($filters: ProductTypeFilterInput) {
+ productTypes(first: 10, filter: $filters) {
+ edges {
+ node {
+ name
+ }
+ }
+ }
+ }
+"""
+
+
+ "search, expected_names",
+ (
+ ("", ["The best juices", "The best beers", "The worst beers"]),
+ ("best", ["The best juices", "The best beers"]),
+ ("worst", ["The worst beers"]),
+ ("average", []),
+ ),
+)
+def test_filter_product_types_by_custom_search_value(
+ api_client, search, expected_names
+):
+ query = QUERY_FILTER_PRODUCT_TYPES
+
+ ProductType.objects.bulk_create(
+ [
+ ProductType(name="The best juices"),
+ ProductType(name="The best beers"),
+ ProductType(name="The worst beers"),
+ ]
+ )
+
+ variables = {"filters": {"search": search}}
+
+ results = get_graphql_content(api_client.post_graphql(query, variables))["data"][
+ "productTypes"
+ ]["edges"]
+
+ assert len(results) == len(expected_names)
+ matched_names = sorted([result["node"]["name"] for result in results])
+
+ assert matched_names == sorted(expected_names)