Skip to content

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

Update method for setting filter descriptions. (#10240)

Dominik Kozaczko65643ec7c37f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 258e468eaa..704209eed2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,7 @@ All notable, unreleased changes to this project will be documented in this file.
### GraphQL API
- Add synchronous tax calculation via webhooks - #9526 by @fowczarek, @mateuszgrzyb, @stnatic
- Add `CHECKOUT_CALCULATE_TAXES` and `ORDER_CALCULATE_TAXES` to `WebhookEventTypeSyncEnum`
+- Add descriptions for some filters - #10240 by @dekoza
### Plugins
- Add synchronous tax calculation via webhooks - #9526 by @fowczarek, @mateuszgrzyb, @stnatic
diff --git a/saleor/graphql/core/tests/test_converters.py b/saleor/graphql/core/tests/test_converters.py
new file mode 100644
index 0000000000..1ba450c2dc
--- /dev/null
+++ b/saleor/graphql/core/tests/test_converters.py
@@ -0,0 +1,92 @@
+import graphene
+import pytest
+from django import forms
+
+from ..types.converter import convert_form_field, get_form_field_description
+
+nonempty_text = "Some text"
+
+
+def mockfield():
+ class Field:
+ pass
+
+ return Field()
+
+
+def test_get_form_field_description_empty_help_text(mockfield):
+ # given
+ mockfield.help_text = ""
+
+ # when
+ result = get_form_field_description(mockfield)
+
+ # then
+ assert result is None
+
+
+def test_get_form_field_description_nonempty_help_text(mockfield):
+ # given
+ mockfield.help_text = nonempty_text
+
+ # when
+ result = get_form_field_description(mockfield)
+
+ # then
+ assert result == nonempty_text
+
+
+def test_get_form_field_description_empty_extra_help_text(mockfield):
+ # given
+ mockfield.extra = {"help_text": ""}
+
+ # when
+ result = get_form_field_description(mockfield)
+
+ # then
+ assert result is None
+
+
+def test_get_form_field_description_nonempty_extra_help_text(mockfield):
+ # given
+ mockfield.extra = {"help_text": nonempty_text}
+
+ # when
+ result = get_form_field_description(mockfield)
+
+ # then
+ assert result == nonempty_text
+
+
+ "infield,outfield_type,expected_description",
+ [
+ # given
+ (forms.CharField(), graphene.String, None),
+ (forms.CharField(help_text=nonempty_text), graphene.String, nonempty_text),
+ (forms.NullBooleanField(), graphene.Boolean, None),
+ (
+ forms.NullBooleanField(help_text=nonempty_text),
+ graphene.Boolean,
+ nonempty_text,
+ ),
+ (forms.DecimalField(), graphene.Float, None),
+ (forms.DecimalField(help_text=nonempty_text), graphene.Float, nonempty_text),
+ (forms.FloatField(), graphene.Float, None),
+ (forms.FloatField(help_text=nonempty_text), graphene.Float, nonempty_text),
+ ],
+)
+def test_conversion(infield, outfield_type, expected_description):
+ # when
+ converted = convert_form_field(infield)
+
+ # then
+ assert isinstance(converted, outfield_type)
+ if expected_description is not None:
+ if not hasattr(converted, "description"):
+ assert converted.kwargs["description"] == expected_description
+ else:
+ assert converted.description == expected_description
+ elif hasattr(converted, "description"):
+ assert converted.description is None
diff --git a/saleor/graphql/core/types/converter.py b/saleor/graphql/core/types/converter.py
index 38d6c5014b..7b10ef5982 100644
--- a/saleor/graphql/core/types/converter.py
+++ b/saleor/graphql/core/types/converter.py
@@ -17,7 +17,10 @@ from .common import NonNullList
def get_form_field_description(field):
- return str(field.help_text) if field.help_text else None
+ if hasattr(field, "help_text"):
+ return field.help_text or None
+ elif hasattr(field, "extra"):
+ return field.extra.get("help_text") or None
@singledispatch
@@ -43,13 +46,15 @@ def convert_form_field_to_nullboolean(field):
@convert_form_field.register(forms.DecimalField)
@convert_form_field.register(forms.FloatField)
def convert_form_field_to_float(field):
- return graphene.Float(description=field.help_text, required=field.required)
+ return graphene.Float(
+ description=get_form_field_description(field), required=field.required
+ )
@convert_form_field.register(ObjectTypeFilter)
@convert_form_field.register(EnumFilter)
def convert_convert_enum(field):
- return field.input_class()
+ return field.input_class(description=get_form_field_description(field))
@convert_form_field.register(GlobalIDFormField)
@@ -59,9 +64,13 @@ def convert_form_field_to_id(field):
@convert_form_field.register(ListObjectTypeFilter)
def convert_list_object_type(field):
- return NonNullList(field.input_class)
+ return NonNullList(field.input_class, description=get_form_field_description(field))
@convert_form_field.register(GlobalIDMultipleChoiceField)
def convert_form_field_to_list(field):
- return NonNullList(graphene.ID, required=field.required)
+ return NonNullList(
+ graphene.ID,
+ required=field.required,
+ description=get_form_field_description(field),
+ )
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index b5b00c800e..5b5f67d441 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -601,21 +601,29 @@ class ProductFilter(MetadataFilterBase):
input_class=PriceRangeInput,
method="filter_minimal_price",
field_name="minimal_price_amount",
+ help_text="Filter by the lowest variant price after discounts.",
)
attributes = ListObjectTypeFilter(
input_class="saleor.graphql.attribute.types.AttributeInput",
method="filter_attributes",
)
stock_availability = EnumFilter(
- input_class=StockAvailability, method="filter_stock_availability"
+ input_class=StockAvailability,
+ method="filter_stock_availability",
+ help_text="Filter by variants having specific stock status.",
)
updated_at = ObjectTypeFilter(
- input_class=DateTimeRangeInput, method=filter_updated_at_range
+ input_class=DateTimeRangeInput,
+ method=filter_updated_at_range,
+ help_text="Filter by when was the most recent update.",
)
product_types = GlobalIDMultipleChoiceFilter(method=filter_product_types)
stocks = ObjectTypeFilter(input_class=ProductStockFilterInput, method=filter_stocks)
search = django_filters.CharFilter(method=filter_search)
- gift_card = django_filters.BooleanFilter(method=filter_gift_card)
+ gift_card = django_filters.BooleanFilter(
+ method=filter_gift_card,
+ help_text="Filter on whether product is a gift card or not.",
+ )
ids = GlobalIDMultipleChoiceFilter(method=filter_by_id("Product"))
has_preordered_variants = django_filters.BooleanFilter(
method=filter_has_preordered_variants
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 8169c6c21a..6f97d07bc6 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -5952,14 +5952,22 @@ input ProductFilterInput {
categories: [ID!]
hasCategory: Boolean
attributes: [AttributeInput!]
+
+ """Filter by variants having specific stock status."""
stockAvailability: StockAvailability
stocks: ProductStockFilterInput
search: String
metadata: [MetadataFilter!]
price: PriceRangeInput
+
+ """Filter by the lowest variant price after discounts."""
minimalPrice: PriceRangeInput
+
+ """Filter by when was the most recent update."""
updatedAt: DateTimeRangeInput
productTypes: [ID!]
+
+ """Filter on whether product is a gift card or not."""
giftCard: Boolean
ids: [ID!]
hasPreorderedVariants: Boolean
@@ -7282,7 +7290,6 @@ type StockCountableEdge {
}
input StockFilterInput {
- """"""
quantity: Float
search: String
}