Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add translation field on AssignedSwatchAttributeValue (#19326)
Lukasz Ostrowskidb5440f7994f
diff --git a/saleor/graphql/attribute/tests/queries/test_selected_attribute.py b/saleor/graphql/attribute/tests/queries/test_selected_attribute.pyindex 009d46c2ad..a93672a312 100644--- a/saleor/graphql/attribute/tests/queries/test_selected_attribute.py+++ b/saleor/graphql/attribute/tests/queries/test_selected_attribute.py@@ -1558,6 +1558,58 @@ def test_assigned_swatch_file_attribute(staff_api_client, page, swatch_attributeassert attr_value_data["file"]["contentType"] == attr_value.content_type+ASSIGNED_SWATCH_ATTRIBUTE_TRANSLATION_QUERY = """+query PageQuery($id: ID) {+ page(id: $id) {+ assignedAttributes(limit:10) {+ ... on AssignedSwatchAttribute {+ value {+ name+ slug+ translation(languageCode: FR)+ }+ }+ }+ }+}+"""+++def test_assigned_swatch_attribute_translation(+ staff_api_client,+ page,+ swatch_attribute,+):+ # given+ swatch_attribute.type = AttributeType.PAGE_TYPE+ swatch_attribute.save()++ page_type = page.page_type+ page_type.page_attributes.set([swatch_attribute])++ attr_value = swatch_attribute.values.first()+ translation = AttributeValueTranslation.objects.create(+ language_code="fr",+ attribute_value=attr_value,+ name="French Red",+ )++ associate_attribute_values_to_instance(page, {swatch_attribute.pk: [attr_value]})++ # when+ response = staff_api_client.post_graphql(+ ASSIGNED_SWATCH_ATTRIBUTE_TRANSLATION_QUERY,+ variables={"id": graphene.Node.to_global_id("Page", page.pk)},+ )++ # then+ content = get_graphql_content(response)++ assert len(content["data"]["page"]["assignedAttributes"]) == 1+ attr_value_data = content["data"]["page"]["assignedAttributes"][0]["value"]+ assert attr_value_data["translation"] == translation.name++ASSIGNED_BOOLEAN_ATTRIBUTE_QUERY = """query PageQuery($id: ID) {page(id: $id) {diff --git a/saleor/graphql/attribute/types.py b/saleor/graphql/attribute/types.pyindex c4ee131d23..687c8c9c80 100644--- a/saleor/graphql/attribute/types.py+++ b/saleor/graphql/attribute/types.py@@ -1496,6 +1496,14 @@ class AssignedSwatchAttributeValue(BaseObjectType):file = graphene.Field(File, description="File associated with the attribute.", required=False)+ translation = graphene.String(+ language_code=graphene.Argument(+ LanguageCodeEnum,+ required=True,+ ),+ description="Translation of the name." + ADDED_IN_322,+ required=False,+ )@staticmethoddef resolve_hex_color(@@ -1511,6 +1519,16 @@ class AssignedSwatchAttributeValue(BaseObjectType):return Nonereturn File(url=root.file_url, content_type=root.content_type)+ @staticmethod+ def resolve_translation(+ root: models.AttributeValue, info: ResolveInfo, *, language_code+ ) -> Promise[str | None] | None:+ return (+ AttributeValueTranslationByIdAndLanguageCodeLoader(info.context)+ .load((root.id, language_code))+ .then(lambda translation: translation.name if translation else None)+ )+class Meta:description = "Represents a single swatch value." + ADDED_IN_322doc_category = DOC_CATEGORY_ATTRIBUTESdiff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex ffe4c61faf..27db0e94ba 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -37934,6 +37934,13 @@ type AssignedSwatchAttributeValue @doc(category: "Attributes") {"""File associated with the attribute."""file: File++ """+ Translation of the name.++ Added in Saleor 3.22.+ """+ translation(languageCode: LanguageCodeEnum!): String}"""