Skip to content

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

Add translation query for Product

fowczarek52730bf80b61
diff --git a/saleor/graphql/translations/schema.py b/saleor/graphql/translations/schema.py
index 37c49a85cb..36eacc992a 100644
--- a/saleor/graphql/translations/schema.py
+++ b/saleor/graphql/translations/schema.py
@@ -1,7 +1,9 @@
import graphene
+from ...product.models import Product
from ..core.connection import CountableConnection
from ..core.fields import BaseConnectionField
+from ..decorators import permission_required
from ..discount import types as discount_types
from ..discount.resolvers import resolve_sales, resolve_vouchers
from ..menu import types as menu_types
@@ -17,6 +19,7 @@ from ..product.resolvers import (
resolve_products,
)
from ..shipping import types as shipping_types
+from ..translations import types as translation_types
from .resolvers import resolve_attribute_values, resolve_shipping_methods
@@ -37,6 +40,12 @@ class TranslatableItem(graphene.Union):
)
+# TODO Consider name of this class
+class DefaultTranslationItem(graphene.Union):
+ class Meta:
+ types = (translation_types.ProductStrings,)
+
+
class TranslatableItemConnection(CountableConnection):
class Meta:
node = TranslatableItem
@@ -57,6 +66,7 @@ class TranslatableKinds(graphene.Enum):
class TranslationQueries(graphene.ObjectType):
+ # TODO We nead to change output of this query to new types
translations = BaseConnectionField(
TranslatableItemConnection,
description="Returns a list of all translatable items of a given kind.",
@@ -64,6 +74,15 @@ class TranslationQueries(graphene.ObjectType):
TranslatableKinds, required=True, description="Kind of objects to retrieve."
),
)
+ translation = graphene.Field(
+ DefaultTranslationItem,
+ id=graphene.Argument(
+ graphene.ID, description="ID of the objects to retrieve.", required=True
+ ),
+ kind=graphene.Argument(
+ TranslatableKinds, required=True, description="Kind of objects to retrieve."
+ ),
+ )
def resolve_translations(self, info, kind, **_kwargs):
if kind == TranslatableKinds.PRODUCT:
@@ -88,3 +107,10 @@ class TranslationQueries(graphene.ObjectType):
return resolve_menu_items(info, query=None)
elif kind == TranslatableKinds.SALE:
return resolve_sales(info, query=None)
+
+ @permission_required("site.manage_translations")
+ def resolve_translation(self, info, id, kind, **_kwargs):
+ if kind == TranslatableKinds.PRODUCT:
+ _type, product_id = graphene.Node.from_global_id(id)
+ return Product.objects.filter(pk=product_id).first()
+ # TODO Add other translatable items
diff --git a/saleor/graphql/translations/types.py b/saleor/graphql/translations/types.py
index 64aec0a2a6..bcd6fb746b 100644
--- a/saleor/graphql/translations/types.py
+++ b/saleor/graphql/translations/types.py
@@ -11,6 +11,7 @@ from ..core.connection import CountableDjangoObjectType
from ..core.types import LanguageDisplay
from ..core.utils import str_to_enum
from .enums import LanguageCodeEnum
+from .resolvers import resolve_translation
class BaseTranslationType(CountableDjangoObjectType):
@@ -71,6 +72,42 @@ class ProductTranslation(BaseTranslationType):
]
+class ProductStrings(CountableDjangoObjectType):
+ translation = graphene.Field(
+ ProductTranslation,
+ language_code=graphene.Argument(
+ LanguageCodeEnum,
+ description="A language code to return the translation for.",
+ required=True,
+ ),
+ description=("Returns translated Product fields for the given language code."),
+ resolver=resolve_translation,
+ )
+ product = graphene.Field(
+ "saleor.graphql.product.types.products.Product",
+ description="Represents an individual item for sale in the storefront.",
+ )
+
+ class Meta:
+ model = product_models.Product
+ interfaces = [graphene.relay.Node]
+ only_fields = [
+ "description",
+ "description_json",
+ "id",
+ "name",
+ "seo_title",
+ "seo_description",
+ ]
+
+ def resolve_product(self, info):
+ return (
+ product_models.Product.objects.visible_to_user(info.context.user)
+ .filter(pk=self.id)
+ .first()
+ )
+
+
class CollectionTranslation(BaseTranslationType):
class Meta:
model = product_models.CollectionTranslation