feat(translation): Add support for locale to the graph query (#14454)
* feat(translation): Add support for locale to the graph query * feat(translation): Add support for locale to the graph query * feat(translation): Add support for locale to the graph query * feat(translation): fix import * fix * cleanup * fix context in product routes * Create dull-onions-punch.md
This commit is contained in:
1 parent
0490a1c67f
commit
0ffd790109
20 files changed
+531
-236
No files matched your search
@@ -1,26 +1,37 @@
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { refetchCollection } from "../helpers"
|
||||
import { applyTranslations } from "@medusajs/framework/utils"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.SelectParams>,
|
||||
res: MedusaResponse<HttpTypes.StoreCollectionResponse>
|
||||
) => {
|
||||
const collection = await refetchCollection(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.queryConfig.fields
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: collections } = await query.graph(
|
||||
{
|
||||
entity: "product_collection",
|
||||
filters: { id: req.params.id },
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: [collection],
|
||||
container: req.scope,
|
||||
})
|
||||
const collection = collections[0]
|
||||
if (!collection) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Collection with id: ${req.params.id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
res.status(200).json({ collection })
|
||||
res.status(200).json({ collection: collection })
|
||||
}
|
||||
@@ -1,42 +1,33 @@
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.StoreCollectionListParams>,
|
||||
res: MedusaResponse<HttpTypes.StoreCollectionListResponse>
|
||||
) => {
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "product_collection",
|
||||
variables: {
|
||||
const { data: collections, metadata } = await query.graph(
|
||||
{
|
||||
entity: "product_collection",
|
||||
filters: req.filterableFields,
|
||||
...req.queryConfig.pagination,
|
||||
pagination: req.queryConfig.pagination,
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
|
||||
const { rows: collections, metadata } = await remoteQuery(query)
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: collections,
|
||||
container: req.scope,
|
||||
})
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
res.json({
|
||||
collections,
|
||||
count: metadata.count,
|
||||
offset: metadata.skip,
|
||||
limit: metadata.take,
|
||||
count: metadata!.count,
|
||||
offset: metadata!.skip,
|
||||
limit: metadata!.take,
|
||||
})
|
||||
}
|
||||
@@ -1,22 +1,30 @@
|
||||
import { StoreProductCategoryResponse } from "@medusajs/framework/types"
|
||||
import { applyTranslations, MedusaError } from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
refetchEntity,
|
||||
} from "@medusajs/framework/http"
|
||||
import { StoreProductCategoryResponse } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { StoreProductCategoryParamsType } from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<StoreProductCategoryParamsType>,
|
||||
res: MedusaResponse<StoreProductCategoryResponse>
|
||||
) => {
|
||||
const category = await refetchEntity({
|
||||
entity: "product_category",
|
||||
idOrFilter: { id: req.params.id, ...req.filterableFields },
|
||||
scope: req.scope,
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: category } = await query.graph(
|
||||
{
|
||||
entity: "product_category",
|
||||
filters: { id: req.params.id, ...req.filterableFields },
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
if (!category) {
|
||||
throw new MedusaError(
|
||||
@@ -25,11 +33,5 @@ export const GET = async (
|
||||
)
|
||||
}
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: [category],
|
||||
container: req.scope,
|
||||
})
|
||||
|
||||
res.json({ product_category: category })
|
||||
res.json({ product_category: category[0] })
|
||||
}
|
||||
@@ -6,10 +6,7 @@ import {
|
||||
StoreProductCategoryListParams,
|
||||
StoreProductCategoryListResponse,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<StoreProductCategoryListParams>,
|
||||
@@ -17,18 +14,17 @@ export const GET = async (
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: product_categories, metadata } = await query.graph({
|
||||
entity: "product_category",
|
||||
fields: req.queryConfig.fields,
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
})
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: product_categories,
|
||||
container: req.scope,
|
||||
})
|
||||
const { data: product_categories, metadata } = await query.graph(
|
||||
{
|
||||
entity: "product_category",
|
||||
fields: req.queryConfig.fields,
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
},
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
res.json({
|
||||
product_categories,
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { StoreProductTagResponse } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { StoreProductTagResponse } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
import { StoreProductTagParamsType } from "../validators"
|
||||
|
||||
@@ -17,13 +16,18 @@ export const GET = async (
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data } = await query.graph({
|
||||
entity: "product_tag",
|
||||
filters: {
|
||||
id: req.params.id,
|
||||
const { data } = await query.graph(
|
||||
{
|
||||
entity: "product_tag",
|
||||
filters: {
|
||||
id: req.params.id,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
if (!data.length) {
|
||||
throw new MedusaError(
|
||||
@@ -34,11 +38,5 @@ export const GET = async (
|
||||
|
||||
const productTag = data[0]
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: [productTag],
|
||||
container: req.scope,
|
||||
})
|
||||
|
||||
res.json({ product_tag: productTag })
|
||||
}
|
||||
@@ -1,12 +1,9 @@
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.StoreProductTagListParams>,
|
||||
@@ -14,18 +11,17 @@ export const GET = async (
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: product_tags, metadata } = await query.graph({
|
||||
entity: "product_tag",
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: product_tags,
|
||||
container: req.scope,
|
||||
})
|
||||
const { data: product_tags, metadata } = await query.graph(
|
||||
{
|
||||
entity: "product_tag",
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
res.json({
|
||||
product_tags,
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { StoreProductTypeResponse } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { StoreProductTypeResponse } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
import { StoreProductTypeParamsType } from "../validators"
|
||||
|
||||
@@ -17,13 +16,18 @@ export const GET = async (
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data } = await query.graph({
|
||||
entity: "product_type",
|
||||
filters: {
|
||||
id: req.params.id,
|
||||
const { data } = await query.graph(
|
||||
{
|
||||
entity: "product_type",
|
||||
filters: {
|
||||
id: req.params.id,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
if (!data.length) {
|
||||
throw new MedusaError(
|
||||
@@ -34,11 +38,5 @@ export const GET = async (
|
||||
|
||||
const productType = data[0]
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: [productType],
|
||||
container: req.scope,
|
||||
})
|
||||
|
||||
res.json({ product_type: productType })
|
||||
}
|
||||
@@ -1,12 +1,9 @@
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/framework/http"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<HttpTypes.StoreProductTypeListParams>,
|
||||
@@ -14,18 +11,17 @@ export const GET = async (
|
||||
) => {
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: product_types, metadata } = await query.graph({
|
||||
entity: "product_type",
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
fields: req.queryConfig.fields,
|
||||
})
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: product_types,
|
||||
container: req.scope,
|
||||
})
|
||||
const { data: product_types, metadata } = await query.graph(
|
||||
{
|
||||
entity: "product_type",
|
||||
filters: req.filterableFields,
|
||||
pagination: req.queryConfig.pagination,
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
res.json({
|
||||
product_types,
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
} from "@medusajs/framework/http"
|
||||
import { HttpTypes, QueryContextType } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
QueryContext,
|
||||
@@ -42,15 +41,20 @@ export const GET = async (
|
||||
context["calculated_price"] = QueryContext(req.pricingContext)
|
||||
}
|
||||
|
||||
const { data: variants = [] } = await query.graph({
|
||||
entity: "variant",
|
||||
filters: {
|
||||
...req.filterableFields,
|
||||
id: req.params.id,
|
||||
const { data: variants = [] } = await query.graph(
|
||||
{
|
||||
entity: "variant",
|
||||
filters: {
|
||||
...req.filterableFields,
|
||||
id: req.params.id,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
context,
|
||||
},
|
||||
fields: req.queryConfig.fields,
|
||||
context,
|
||||
})
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
const variant = variants[0]
|
||||
|
||||
@@ -61,12 +65,6 @@ export const GET = async (
|
||||
)
|
||||
}
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: [variant],
|
||||
container: req.scope,
|
||||
})
|
||||
|
||||
if (withInventoryQuantity) {
|
||||
await wrapVariantsWithInventoryQuantityForSalesChannel(req, [variant])
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
} from "@medusajs/framework/http"
|
||||
import { HttpTypes, QueryContextType } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
QueryContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
@@ -13,8 +12,7 @@ import { StoreRequestWithContext } from "../types"
|
||||
import { wrapVariantsWithTaxPrices } from "./helpers"
|
||||
|
||||
type StoreVariantListRequest<T = HttpTypes.StoreProductVariantParams> =
|
||||
StoreRequestWithContext<T> &
|
||||
AuthenticatedMedusaRequest<T>
|
||||
StoreRequestWithContext<T> & AuthenticatedMedusaRequest<T>
|
||||
|
||||
/**
|
||||
* @since 2.11.2
|
||||
@@ -52,15 +50,10 @@ export const GET = async (
|
||||
cache: {
|
||||
enable: true,
|
||||
},
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: variants,
|
||||
container: req.scope,
|
||||
})
|
||||
|
||||
if (withInventoryQuantity) {
|
||||
await wrapVariantsWithInventoryQuantityForSalesChannel(req, variants)
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import { MedusaResponse } from "@medusajs/framework/http"
|
||||
import { HttpTypes } from "@medusajs/framework/types"
|
||||
import { HttpTypes, QueryContextType } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
isPresent,
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
QueryContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { wrapVariantsWithInventoryQuantityForSalesChannel } from "../../../utils/middlewares"
|
||||
import {
|
||||
filterOutInternalProductCategories,
|
||||
refetchProduct,
|
||||
RequestWithContext,
|
||||
wrapProductsWithTaxPrices,
|
||||
} from "../helpers"
|
||||
@@ -33,12 +31,11 @@ export const GET = async (
|
||||
...req.filterableFields,
|
||||
}
|
||||
|
||||
if (isPresent(req.pricingContext)) {
|
||||
filters["context"] ??= {}
|
||||
filters["context"]["variants"] ??= {}
|
||||
filters["context"]["variants"]["calculated_price"] ??= QueryContext(
|
||||
req.pricingContext!
|
||||
)
|
||||
const context: QueryContextType = {}
|
||||
|
||||
if (req.pricingContext) {
|
||||
context["variants"] ??= {}
|
||||
context["variants"]["calculated_price"] ??= QueryContext(req.pricingContext)
|
||||
}
|
||||
|
||||
const includesCategoriesField = req.queryConfig.fields.some((field) =>
|
||||
@@ -49,11 +46,20 @@ export const GET = async (
|
||||
req.queryConfig.fields.push("categories.is_internal")
|
||||
}
|
||||
|
||||
const product = await refetchProduct(
|
||||
filters,
|
||||
req.scope,
|
||||
req.queryConfig.fields
|
||||
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const { data: products } = await query.graph(
|
||||
{
|
||||
entity: "product",
|
||||
filters,
|
||||
context,
|
||||
fields: req.queryConfig.fields,
|
||||
},
|
||||
{
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
const product = products[0]
|
||||
|
||||
if (!product) {
|
||||
throw new MedusaError(
|
||||
@@ -74,10 +80,5 @@ export const GET = async (
|
||||
}
|
||||
|
||||
await wrapProductsWithTaxPrices(req, [product])
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: [product],
|
||||
container: req.scope,
|
||||
})
|
||||
res.json({ product })
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { MedusaResponse } from "@medusajs/framework/http"
|
||||
import { HttpTypes, QueryContextType } from "@medusajs/framework/types"
|
||||
import {
|
||||
applyTranslations,
|
||||
ContainerRegistrationKeys,
|
||||
FeatureFlag,
|
||||
isPresent,
|
||||
@@ -76,6 +75,7 @@ async function getProductsWithIndexEngine(
|
||||
cache: {
|
||||
enable: true,
|
||||
},
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -88,12 +88,6 @@ async function getProductsWithIndexEngine(
|
||||
|
||||
await wrapProductsWithTaxPrices(req, products)
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: products,
|
||||
container: req.scope,
|
||||
})
|
||||
|
||||
res.json({
|
||||
products,
|
||||
count: metadata!.estimate_count,
|
||||
@@ -138,6 +132,7 @@ async function getProducts(
|
||||
cache: {
|
||||
enable: true,
|
||||
},
|
||||
locale: req.locale,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -150,12 +145,6 @@ async function getProducts(
|
||||
|
||||
await wrapProductsWithTaxPrices(req, products)
|
||||
|
||||
await applyTranslations({
|
||||
localeCode: req.locale,
|
||||
objects: products,
|
||||
container: req.scope,
|
||||
})
|
||||
|
||||
res.json({
|
||||
products,
|
||||
count: metadata!.count,
|
||||
|
||||
Reference in new issue
Block a user