Skip to content

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

Saleor 4019 add with choice field to attribute (#7733)

Dawid Matuszczyk6c928b72daa4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 880e43ab62..f3625e601c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -128,6 +128,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Modify order of auth middleware calls - #7572 by @tomaszszymanski129
- Add app support for events - #7622 by @IKarbowiak
- Add date & date time attributes - #7500 by @piotrgrundas
+- Add `withChoices` flag for Attribute type - #7733 by @dexon44
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/attribute/descriptions.py b/saleor/graphql/attribute/descriptions.py
index b663c5c3f4..f3fabfb466 100644
--- a/saleor/graphql/attribute/descriptions.py
+++ b/saleor/graphql/attribute/descriptions.py
@@ -20,6 +20,7 @@ class AttributeDescriptions:
AVAILABLE_IN_GRID = (
"Whether the attribute can be displayed in the admin product list."
)
+ WITH_CHOICES = "Flag indicating that attribute has predefined choices."
class AttributeValueDescriptions:
diff --git a/saleor/graphql/attribute/tests/queries/test_attribute_query.py b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
index 2c9ebc54a9..8204b65e16 100644
--- a/saleor/graphql/attribute/tests/queries/test_attribute_query.py
+++ b/saleor/graphql/attribute/tests/queries/test_attribute_query.py
@@ -3,6 +3,7 @@ import pytest
from django.db.models import Q
from graphene.utils.str_converters import to_camel_case
+from .....attribute import AttributeInputType, AttributeType
from .....attribute.models import Attribute
from .....product.models import Category, Collection, Product, ProductType
from .....tests.utils import dummy_editorjs
@@ -573,3 +574,51 @@ def test_attributes_in_collection_query(
expected_flat_attributes_data = list(expected_qs.values_list("slug", flat=True))
assert flat_attributes_data == expected_flat_attributes_data
+
+
+ "input_type, expected_with_choice_return",
+ [
+ (AttributeInputType.DROPDOWN, True),
+ (AttributeInputType.MULTISELECT, True),
+ (AttributeInputType.FILE, False),
+ (AttributeInputType.REFERENCE, False),
+ (AttributeInputType.NUMERIC, False),
+ (AttributeInputType.RICH_TEXT, False),
+ (AttributeInputType.BOOLEAN, False),
+ ],
+)
+def test_attributes_with_choice_flag(
+ user_api_client,
+ input_type,
+ expected_with_choice_return,
+):
+ attribute = Attribute.objects.create(
+ slug=input_type,
+ name=input_type.upper(),
+ type=AttributeType.PRODUCT_TYPE,
+ input_type=input_type,
+ filterable_in_storefront=True,
+ filterable_in_dashboard=True,
+ available_in_grid=True,
+ )
+
+ attribute_gql_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ query = """
+ query($id: ID!) {
+ attribute(id: $id) {
+ id
+ inputType
+ withChoices
+
+ }
+ }
+ """
+ content = get_graphql_content(
+ user_api_client.post_graphql(query, {"id": attribute_gql_id})
+ )
+ assert content["data"]["attribute"]["id"] == attribute_gql_id
+ assert content["data"]["attribute"]["inputType"] == input_type.upper().replace(
+ "-", "_"
+ )
+ assert content["data"]["attribute"]["withChoices"] == expected_with_choice_return
diff --git a/saleor/graphql/attribute/types.py b/saleor/graphql/attribute/types.py
index 647581aee1..843862f13f 100644
--- a/saleor/graphql/attribute/types.py
+++ b/saleor/graphql/attribute/types.py
@@ -150,6 +150,9 @@ class Attribute(CountableDjangoObjectType):
storefront_search_position = graphene.Int(
description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION, required=True
)
+ with_choices = graphene.Boolean(
+ description=AttributeDescriptions.WITH_CHOICES, required=True
+ )
class Meta:
description = (
@@ -196,6 +199,10 @@ class Attribute(CountableDjangoObjectType):
def resolve_available_in_grid(root: models.Attribute, *_args):
return root.available_in_grid
+ @staticmethod
+ def resolve_with_choices(root: models.Attribute, *_args):
+ return root.input_type in AttributeInputType.TYPES_WITH_CHOICES
+
class SelectedAttribute(graphene.ObjectType):
attribute = graphene.Field(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 03309e2f22..9fc5219e59 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -421,6 +421,7 @@ type Attribute implements Node & ObjectWithMetadata {
availableInGrid: Boolean!
translation(languageCode: LanguageCodeEnum!): AttributeTranslation
storefrontSearchPosition: Int!
+ withChoices: Boolean!
}
type AttributeBulkDelete {