Skip to content

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

Add `attribute` field to `AttributeValueTranslatableContent` type (#11028)

Piotr Zabieglikbcd9f60d2aef
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5a417dc71f..7c572cefdf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Re-enable 5 minute database connection persistence by default - #11074 + #11100 by @NyanKiyoshi
<br/>Set `DB_CONN_MAX_AGE=0` to disable this behavior (adds overhead to requests)
- Bump cryptography to 38.0.3: use OpenSSL 3.0.7 - #11126 by @NyanKiyoshi
+- Add `attribute` field to `AttributeValueTranslatableContent` type. #11028 by @zedzior
...
diff --git a/saleor/graphql/core/descriptions.py b/saleor/graphql/core/descriptions.py
index 1a14d14e23..4cd4919df2 100644
--- a/saleor/graphql/core/descriptions.py
+++ b/saleor/graphql/core/descriptions.py
@@ -19,6 +19,7 @@ ADDED_IN_35 = "\n\nAdded in Saleor 3.5."
ADDED_IN_36 = "\n\nAdded in Saleor 3.6."
ADDED_IN_37 = "\n\nAdded in Saleor 3.7."
ADDED_IN_38 = "\n\nAdded in Saleor 3.8."
+ADDED_IN_39 = "\n\nAdded in Saleor 3.9."
PREVIEW_FEATURE = (
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 25f41b63bc..1ed4cd326a 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -6620,6 +6620,27 @@ type AttributeValueTranslatableContent implements Node {
"""Represents a value of an attribute."""
attributeValue: AttributeValue @deprecated(reason: "This field will be removed in Saleor 4.0. Get model fields from the root level queries.")
+
+ """
+ Associated attribute that can be translated.
+
+ Added in Saleor 3.9.
+ """
+ attribute: AttributeTranslatableContent
+}
+
+type AttributeTranslatableContent implements Node {
+ id: ID!
+ name: String!
+
+ """Returns translated attribute fields for the given language code."""
+ translation(
+ """A language code to return the translation for attribute."""
+ languageCode: LanguageCodeEnum!
+ ): AttributeTranslation
+
+ """Custom attribute of a product."""
+ attribute: Attribute @deprecated(reason: "This field will be removed in Saleor 4.0. Get model fields from the root level queries.")
}
type CollectionTranslatableContent implements Node {
@@ -6682,20 +6703,6 @@ type CategoryTranslatableContent implements Node {
category: Category @deprecated(reason: "This field will be removed in Saleor 4.0. Get model fields from the root level queries.")
}
-type AttributeTranslatableContent implements Node {
- id: ID!
- name: String!
-
- """Returns translated attribute fields for the given language code."""
- translation(
- """A language code to return the translation for attribute."""
- languageCode: LanguageCodeEnum!
- ): AttributeTranslation
-
- """Custom attribute of a product."""
- attribute: Attribute @deprecated(reason: "This field will be removed in Saleor 4.0. Get model fields from the root level queries.")
-}
-
type ProductVariantTranslatableContent implements Node {
id: ID!
name: String!
diff --git a/saleor/graphql/translations/tests/test_translations.py b/saleor/graphql/translations/tests/test_translations.py
index 8283ffd7a7..eb303db399 100644
--- a/saleor/graphql/translations/tests/test_translations.py
+++ b/saleor/graphql/translations/tests/test_translations.py
@@ -3587,6 +3587,12 @@ PRODUCT_ATTRIBUTE_VALUES_TRANSLATION_QUERY = """
richText
plainText
}
+ attribute {
+ name
+ translation(languageCode: $languageCode) {
+ name
+ }
+ }
}
}
}
@@ -3671,6 +3677,46 @@ def test_product_attribute_value_plain_text_translation(
assert attribute_value_response["translation"]["plainText"] == text
+def test_nested_product_attribute_translation(
+ staff_api_client,
+ plain_text_attribute,
+ product,
+ permission_manage_translations,
+):
+ # given
+ product_type = product.product_type
+ product_type.product_attributes.set([plain_text_attribute])
+
+ product_attr_value = plain_text_attribute.values.first()
+ associate_attribute_values_to_instance(
+ product, plain_text_attribute, product_attr_value
+ )
+
+ text = "Test attribute translation"
+ plain_text_attribute.translations.create(language_code="pl", name=text)
+
+ product_id = graphene.Node.to_global_id("Product", product.id)
+
+ query = PRODUCT_ATTRIBUTE_VALUES_TRANSLATION_QUERY
+ variables = {
+ "id": product_id,
+ "kind": TranslatableKinds.PRODUCT.name,
+ "languageCode": LanguageCodeEnum.PL.name,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_translations]
+ )
+
+ # then
+ data = get_graphql_content(response)["data"]
+
+ attribute_response = data["translation"]["attributeValues"][0]["attribute"]
+ assert attribute_response["name"] == plain_text_attribute.name
+ assert attribute_response["translation"]["name"] == text
+
+
PRODUCT_VARIANT_ATTRIBUTE_VALUES_TRANSLATION_QUERY = """
query translation(
$kind: TranslatableKinds!
@@ -3689,6 +3735,12 @@ PRODUCT_VARIANT_ATTRIBUTE_VALUES_TRANSLATION_QUERY = """
richText
plainText
}
+ attribute {
+ name
+ translation(languageCode: $languageCode) {
+ name
+ }
+ }
}
}
}
@@ -3774,6 +3826,46 @@ def test_product_variant_attribute_value_plain_text_translation(
assert translations_response["translation"]["plainText"] == text
+def test_nested_product_variant_attribute_translation(
+ staff_api_client,
+ variant,
+ plain_text_attribute,
+ permission_manage_translations,
+):
+ # given
+ product_type = variant.product.product_type
+ product_type.variant_attributes.set([plain_text_attribute])
+
+ attribute_value = plain_text_attribute.values.first()
+ associate_attribute_values_to_instance(
+ variant, plain_text_attribute, attribute_value
+ )
+
+ text = "Test attribute translation"
+ plain_text_attribute.translations.create(language_code="pl", name=text)
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+
+ query = PRODUCT_VARIANT_ATTRIBUTE_VALUES_TRANSLATION_QUERY
+ variables = {
+ "id": variant_id,
+ "kind": TranslatableKinds.VARIANT.name,
+ "languageCode": LanguageCodeEnum.PL.name,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_translations]
+ )
+
+ # then
+ data = get_graphql_content(response)["data"]
+
+ attribute_response = data["translation"]["attributeValues"][0]["attribute"]
+ assert attribute_response["name"] == plain_text_attribute.name
+ assert attribute_response["translation"]["name"] == text
+
+
PAGE_ATTRIBUTE_VALUES_TRANSLATION_QUERY = """
query translation(
$kind: TranslatableKinds!
@@ -3791,6 +3883,12 @@ PAGE_ATTRIBUTE_VALUES_TRANSLATION_QUERY = """
richText
plainText
}
+ attribute {
+ name
+ translation(languageCode: $languageCode) {
+ name
+ }
+ }
}
}
}
@@ -3871,3 +3969,44 @@ def test_page_attribute_value_plain_text_translation(
assert attribute_value_response["name"] == attribute_value.name
assert attribute_value_response["plainText"] == attribute_value.plain_text
assert attribute_value_response["translation"]["plainText"] == text
+
+
+def test_nested_page_attribute_translation(
+ staff_api_client,
+ plain_text_attribute_page_type,
+ page,
+ permission_manage_translations,
+ permission_manage_pages,
+):
+ # given
+ page_type = page.page_type
+ page_type.page_attributes.set([plain_text_attribute_page_type])
+ attribute_value = plain_text_attribute_page_type.values.first()
+
+ associate_attribute_values_to_instance(
+ page, plain_text_attribute_page_type, attribute_value
+ )
+
+ text = "Test attribute translation"
+ plain_text_attribute_page_type.translations.create(language_code="pl", name=text)
+
+ page_id = graphene.Node.to_global_id("Page", page.id)
+
+ query = PAGE_ATTRIBUTE_VALUES_TRANSLATION_QUERY
+ variables = {
+ "id": page_id,
+ "kind": TranslatableKinds.PAGE.name,
+ "languageCode": LanguageCodeEnum.PL.name,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_translations]
+ )
+
+ # then
+ data = get_graphql_content(response)["data"]
+
+ attribute_response = data["translation"]["attributeValues"][0]["attribute"]
+ assert attribute_response["name"] == plain_text_attribute_page_type.name
+ assert attribute_response["translation"]["name"] == text
diff --git a/saleor/graphql/translations/types.py b/saleor/graphql/translations/types.py
index f4d25b92d5..769d49d973 100644
--- a/saleor/graphql/translations/types.py
+++ b/saleor/graphql/translations/types.py
@@ -14,8 +14,9 @@ from ...page import models as page_models
from ...product import models as product_models
from ...shipping import models as shipping_models
from ...site import models as site_models
+from ..attribute.dataloaders import AttributesByAttributeId
from ..channel import ChannelContext
-from ..core.descriptions import DEPRECATED_IN_3X_FIELD, RICH_CONTENT
+from ..core.descriptions import ADDED_IN_39, DEPRECATED_IN_3X_FIELD, RICH_CONTENT
from ..core.enums import LanguageCodeEnum
from ..core.fields import JSONString, PermissionsField
from ..core.types import LanguageDisplay, ModelObjectType, NonNullList
@@ -76,31 +77,6 @@ class AttributeValueTranslation(BaseTranslationType):
interfaces = [graphene.relay.Node]
-class AttributeValueTranslatableContent(ModelObjectType):
- id = graphene.GlobalID(required=True)
- name = graphene.String(required=True)
- rich_text = JSONString(description="Attribute value." + RICH_CONTENT)
- plain_text = graphene.String(description="Attribute plain text value.")
- translation = TranslationField(
- AttributeValueTranslation, type_name="attribute value"
- )
- attribute_value = graphene.Field(
- "saleor.graphql.attribute.types.AttributeValue",
- description="Represents a value of an attribute.",
- deprecation_reason=(
- f"{DEPRECATED_IN_3X_FIELD} Get model fields from the root level queries."
- ),
- )
-
- class Meta:
- model = attribute_models.AttributeValue
- interfaces = [graphene.relay.Node]
-
- @staticmethod
- def resolve_attribute_value(root: attribute_models.AttributeValue, _info):
- return root
-
-
class AttributeTranslation(BaseTranslationType):
id = graphene.GlobalID(required=True)
name = graphene.String(required=True)
@@ -131,6 +107,39 @@ class AttributeTranslatableContent(ModelObjectType):
return root
+class AttributeValueTranslatableContent(ModelObjectType):
+ id = graphene.GlobalID(required=True)
+ name = graphene.String(required=True)
+ rich_text = JSONString(description="Attribute value." + RICH_CONTENT)
+ plain_text = graphene.String(description="Attribute plain text value.")
+ translation = TranslationField(
+ AttributeValueTranslation, type_name="attribute value"
+ )
+ attribute_value = graphene.Field(
+ "saleor.graphql.attribute.types.AttributeValue",
+ description="Represents a value of an attribute.",
+ deprecation_reason=(
+ f"{DEPRECATED_IN_3X_FIELD} Get model fields from the root level queries."
+ ),
+ )
+ attribute = graphene.Field(
+ AttributeTranslatableContent,
+ description="Associated attribute that can be translated." + ADDED_IN_39,
+ )
+
+ class Meta:
+ model = attribute_models.AttributeValue
+ interfaces = [graphene.relay.Node]
+
+ @staticmethod
+ def resolve_attribute_value(root: attribute_models.AttributeValue, _info):
+ return root
+
+ @staticmethod
+ def resolve_attribute(root: attribute_models.AttributeValue, info):
+ return AttributesByAttributeId(info.context).load(root.attribute_id)
+
+
class ProductVariantTranslation(BaseTranslationType):
id = graphene.GlobalID(required=True)
name = graphene.String(required=True)