Skip to content

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

Always return all the product/variant attributes even if no values

NyanKiyoshidca55221cf12
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1ae1837bb2..d5a9ebffe7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ All notable, unreleased changes to this project will be documented in this file.
- 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 and sorting capabilities to the top-level attributes' field. - #4387 by @NyanKiyoshi
+- The `SelectedAttribute` type now returns multiple values and will always return all the attributes assigned to the product type. - #4394 by @NyanKiyoshi
## [Unreleased]
diff --git a/saleor/graphql/product/types/attributes.py b/saleor/graphql/product/types/attributes.py
index 27e15f0904..97136220e1 100644
--- a/saleor/graphql/product/types/attributes.py
+++ b/saleor/graphql/product/types/attributes.py
@@ -191,8 +191,11 @@ class SelectedAttribute(graphene.ObjectType):
value = graphene.Field(
AttributeValue,
default_value=None,
- description="Value of an attribute.",
- required=True,
+ description="The value or the first value of an attribute.",
+ deprecation_reason="Use values instead.",
+ )
+ values = graphene.List(
+ AttributeValue, description="Values of an attribute.", required=True
)
class Meta:
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index e34796ec48..788c125811 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -81,23 +81,21 @@ def resolve_attribute_list(attributes_json, attributes_qs):
`attributes_qs` is the queryset of attribute objects. If it's prefetch
beforehand along with the values, it saves database queries.
"""
- attributes_map = {} # type: Dict[int, models.Attribute]
- values_map = {}
+ attributes_map = {} # type: Dict[str, models.Attribute]
+ values_map = {} # type: Dict[str, models.AttributeValue]
for attr in attributes_qs:
- attributes_map[attr.pk] = attr
+ attributes_map[str(attr.pk)] = attr
for val in attr.values.all():
- values_map[val.pk] = val
+ values_map[str(val.pk)] = val
attributes_list = []
- for k, values in attributes_json.items():
- attribute = attributes_map.get(int(k))
-
- for v_pk in values:
- value = values_map.get(int(v_pk))
- if attribute and value:
- attributes_list.append(
- SelectedAttribute(attribute=attribute, value=value)
- )
+ for attr_pk, attr in attributes_map.items():
+ values = attributes_json.get(attr_pk, [])
+ values = [values_map[v_pk] for v_pk in values if v_pk in values_map]
+ value = values[0] if values else None
+ attributes_list.append(
+ SelectedAttribute(attribute=attr, value=value, values=values)
+ )
return attributes_list
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index b7742e76e5..cebe575663 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3007,7 +3007,8 @@ type SaleUpdate {
type SelectedAttribute {
attribute: Attribute!
- value: AttributeValue!
+ value: AttributeValue @deprecated(reason: "Use values instead.")
+ values: [AttributeValue]!
}
input SeoInput {
diff --git a/tests/api/benchmark/test_product.py b/tests/api/benchmark/test_product.py
index d17d60ab8e..8d111bc27d 100644
--- a/tests/api/benchmark/test_product.py
+++ b/tests/api/benchmark/test_product.py
@@ -36,7 +36,7 @@ def test_product_details(product, api_client, count_queries):
id
name
}
- value {
+ values {
id
name
value: name
diff --git a/tests/api/benchmark/test_variant.py b/tests/api/benchmark/test_variant.py
index 52d7653211..6e2e6110da 100644
--- a/tests/api/benchmark/test_variant.py
+++ b/tests/api/benchmark/test_variant.py
@@ -36,7 +36,7 @@ def test_retrieve_variant_list(product_variant_list, api_client, count_queries):
id
name
}
- value {
+ values {
id
name
value: name
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index ea7c314b88..53ece72b92 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -263,9 +263,6 @@ QUERY_PRODUCT_ATTRIBUTES = """
edges {
node {
attributes {
- value {
- name
- }
attribute {
name
}
@@ -317,9 +314,6 @@ QUERY_VARIANT_ATTRIBUTES = """
node {
variants {
attributes {
- value {
- name
- }
attribute {
name
}
@@ -375,11 +369,6 @@ def test_attributes_in_category_query(user_api_client, product):
id
name
slug
- values {
- id
- name
- slug
- }
}
}
}
@@ -408,11 +397,6 @@ def test_attributes_in_collection_query(user_api_client, sale):
id
name
slug
- values {
- id
- name
- slug
- }
}
}
}
@@ -954,15 +938,99 @@ def test_resolve_attribute_list(color_attribute):
attributes_json = {str(color_attribute.pk): [str(value.pk)]}
res = resolve_attribute_list(attributes_json, Attribute.objects.all())
assert len(res) == 1
+ assert len(res[0].values) == 1
assert res[0].attribute.name == color_attribute.name
- assert res[0].value.name == value.name
+ assert res[0].values[0].name == value.name
# test passing invalid json should resolve to empty list
attr_pk = str(Attribute.objects.order_by("pk").last().pk + 1)
val_pk = str(AttributeValue.objects.order_by("pk").last().pk + 1)
attributes_json = {attr_pk: [val_pk]}
res = resolve_attribute_list(attributes_json, Attribute.objects.all())
- assert res == []
+ assert len(res) == 1
+ assert res[0].attribute.pk == color_attribute.pk
+ assert res[0].values == []
+
+
+# FIXME: this field (value) is deprecated
+def test_resolve_attribute_list_deprecated_single_value(color_attribute):
+ value = color_attribute.values.first()
+
+ # Test passing a value
+ attributes_json = {str(color_attribute.pk): [str(value.pk)]}
+ res = resolve_attribute_list(attributes_json, Attribute.objects.all())
+ assert len(res) == 1
+ assert len(res[0].values) == 1
+ assert res[0].value.pk == value.pk
+
+ # Test passing no values
+ attributes_json = {str(color_attribute.pk): []}
+ res = resolve_attribute_list(attributes_json, Attribute.objects.all())
+ assert len(res) == 1
+ assert res[0].values == []
+ assert res[0].value is None
+
+
+def test_resolve_assigned_attribute_without_values(api_client, product_type, product):
+ # Remove all attributes and values from product
+ product.attributes = {}
+ product.save(update_fields=["attributes"])
+
+ # Remove all attributes and values from variant
+ variant = product.variants.get()
+ variant.attributes = {}
+ variant.save(update_fields=["attributes"])
+
+ # Retrieve the product and variant's attributes
+ products = get_graphql_content(
+ api_client.post_graphql(
+ """
+ {
+ products(first: 10) {
+ edges {
+ node {
+ attributes {
+ attribute {
+ slug
+ }
+ values {
+ value
+ }
+ }
+ variants {
+ attributes {
+ attribute {
+ slug
+ }
+ values {
+ value
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ """
+ )
+ )["data"]["products"]["edges"]
+
+ # Ensure we are only working on one product and variant, the ones we are testing
+ assert len(products) == 1
+ assert len(products[0]["node"]["variants"]) == 1
+
+ # Retrieve the nodes data
+ product = products[0]["node"]
+ variant = product["variants"][0]
+
+ # Ensure the product attributes values are all None
+ assert len(product["attributes"]) == 1
+ assert product["attributes"][0]["attribute"]["slug"] == "color"
+ assert product["attributes"][0]["values"] == []
+
+ # Ensure the variant attributes values are all None
+ assert variant["attributes"][0]["attribute"]["slug"] == "size"
+ assert variant["attributes"][0]["values"] == []
ASSIGN_ATTR_QUERY = """
@@ -1258,7 +1326,7 @@ def test_retrieve_product_attributes_input_type(
edges {
node {
attributes {
- value {
+ values {
type
inputType
}
@@ -1275,8 +1343,9 @@ def test_retrieve_product_attributes_input_type(
assert len(found_products) == 1
for gql_attr in found_products[0]["node"]["attributes"]:
- assert gql_attr["value"]["type"] == "STRING"
- assert gql_attr["value"]["inputType"] == "DROPDOWN"
+ assert len(gql_attr["values"]) == 1
+ assert gql_attr["values"][0]["type"] == "STRING"
+ assert gql_attr["values"][0]["inputType"] == "DROPDOWN"
@pytest.mark.parametrize(
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 492ee44f66..8ce4c8271e 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -705,7 +705,7 @@ def test_create_product(
attribute {
slug
}
- value {
+ values {
slug
}
}
@@ -777,8 +777,8 @@ def test_create_product(
assert data["product"]["productType"]["name"] == product_type.name
assert data["product"]["category"]["name"] == category.name
values = (
- data["product"]["attributes"][0]["value"]["slug"],
- data["product"]["attributes"][1]["value"]["slug"],
+ data["product"]["attributes"][0]["values"][0]["slug"],
+ data["product"]["attributes"][1]["values"][0]["slug"],
)
assert slugify(non_existent_attr_value) in values
assert color_value_slug in values
@@ -1024,7 +1024,7 @@ def test_update_product(
attribute {
name
}
- value {
+ values {
name
}
}
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index 48712b8ab7..67ce6c04a9 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -20,7 +20,7 @@ def test_fetch_variant(staff_api_client, product, permission_manage_products):
slug
}
}
- value {
+ values {
id
name
slug
@@ -90,7 +90,7 @@ def test_create_variant(
attribute {
slug
}
- value {
+ values {
slug
}
}
@@ -145,7 +145,7 @@ def test_create_variant(
assert data["priceOverride"]["amount"] == price_override
assert data["sku"] == sku
assert data["attributes"][0]["attribute"]["slug"] == variant_slug
- assert data["attributes"][0]["value"]["slug"] == variant_value
+ assert data["attributes"][0]["values"][0]["slug"] == variant_value
assert data["weight"]["unit"] == "kg"
assert data["weight"]["value"] == weight