Skip to content

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

Add translation query for product variant

fowczarekbe5360a0d689
diff --git a/saleor/graphql/translations/schema.py b/saleor/graphql/translations/schema.py
index b121027791..051ae7eec9 100644
--- a/saleor/graphql/translations/schema.py
+++ b/saleor/graphql/translations/schema.py
@@ -1,6 +1,13 @@
import graphene
-from ...product.models import Attribute, AttributeValue, Category, Collection, Product
+from ...product.models import (
+ Attribute,
+ AttributeValue,
+ Category,
+ Collection,
+ Product,
+ ProductVariant,
+)
from ..core.connection import CountableConnection
from ..core.fields import BaseConnectionField
from ..decorators import permission_required
@@ -49,6 +56,7 @@ class DefaultTranslationItem(graphene.Union):
translation_types.CategoryStrings,
translation_types.AttributeStrings,
translation_types.AttributeValueStrings,
+ translation_types.ProductVariantStrings,
)
@@ -118,10 +126,10 @@ class TranslationQueries(graphene.ObjectType):
@permission_required("site.manage_translations")
def resolve_translation(self, info, id, kind, **_kwargs):
# TODO Add other translatable items
- if kind == TranslatableKinds.PRODUCT:
+ if kind == TranslatableKinds.PRODUCT: # TODO test hidden data
_type, product_id = graphene.Node.from_global_id(id)
return Product.objects.filter(pk=product_id).first()
- elif kind == TranslatableKinds.COLLECTION:
+ elif kind == TranslatableKinds.COLLECTION: # TODO test hidden data
_type, collection_id = graphene.Node.from_global_id(id)
return Collection.objects.filter(pk=collection_id).first()
elif kind == TranslatableKinds.CATEGORY:
@@ -133,3 +141,6 @@ class TranslationQueries(graphene.ObjectType):
elif kind == TranslatableKinds.ATTRIBUTE_VALUE:
_type, attribute_value_id = graphene.Node.from_global_id(id)
return AttributeValue.objects.filter(pk=attribute_value_id).first()
+ elif kind == TranslatableKinds.VARIANT: # TODO test hidden data
+ _type, product_variant_id = graphene.Node.from_global_id(id)
+ return ProductVariant.objects.filter(pk=product_variant_id).first()
diff --git a/saleor/graphql/translations/types.py b/saleor/graphql/translations/types.py
index 0b2d15e140..b57b5b0e5c 100644
--- a/saleor/graphql/translations/types.py
+++ b/saleor/graphql/translations/types.py
@@ -112,6 +112,40 @@ class ProductVariantTranslation(BaseTranslationType):
only_fields = ["id", "name"]
+class ProductVariantStrings(CountableDjangoObjectType):
+ translation = graphene.Field(
+ ProductVariantTranslation,
+ language_code=graphene.Argument(
+ LanguageCodeEnum,
+ description="A language code to return the translation for.",
+ required=True,
+ ),
+ description=(
+ "Returns translated Product Variant fields " "for the given language code."
+ ),
+ resolver=resolve_translation,
+ )
+ product_variant = graphene.Field(
+ "saleor.graphql.product.types.products.ProductVariant",
+ description=(
+ "Represents a version of a product such as different size or color."
+ ),
+ )
+
+ class Meta:
+ model = product_models.ProductVariant
+ interfaces = [graphene.relay.Node]
+ only_fields = ["id", "name"]
+
+ def resolve_product_variant(self, info):
+ visible_products = product_models.Product.objects.visible_to_user(
+ info.context.user
+ ).values_list("pk", flat=True)
+ return product_models.ProductVariant.objects.filter(
+ product__id__in=visible_products, pk=self.id
+ ).first()
+
+
class ProductTranslation(BaseTranslationType):
class Meta:
model = product_models.ProductTranslation