diff --git a/.changeset/fair-forks-pretend.md b/.changeset/fair-forks-pretend.md new file mode 100644 index 0000000000..0e463e3b52 --- /dev/null +++ b/.changeset/fair-forks-pretend.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa": patch +"@medusajs/framework": patch +--- + +chore(framework): slightly improve maybe apply link filter middleware diff --git a/packages/core/framework/src/http/utils/maybe-apply-link-filter.ts b/packages/core/framework/src/http/utils/maybe-apply-link-filter.ts index 9f772f4466..762a60825a 100644 --- a/packages/core/framework/src/http/utils/maybe-apply-link-filter.ts +++ b/packages/core/framework/src/http/utils/maybe-apply-link-filter.ts @@ -1,8 +1,4 @@ -import { - arrayIntersection, - ContainerRegistrationKeys, - remoteQueryObjectFromString, -} from "@medusajs/utils" +import { ContainerRegistrationKeys } from "@medusajs/utils" import type { MedusaNextFunction, MedusaRequest, @@ -34,35 +30,29 @@ export function maybeApplyLinkFilter({ delete filterableFields[filterableField] - const remoteQuery = req.scope.resolve( - ContainerRegistrationKeys.REMOTE_QUERY - ) - - const queryObject = remoteQueryObjectFromString({ - entryPoint, - fields: [resourceId], - variables: { filters: { [filterableField]: idsToFilterBy } }, - }) - - const resources = await remoteQuery(queryObject) let existingFilters = filterableFields[filterByField] as | string[] | string | undefined - if (existingFilters) { - if (typeof existingFilters === "string") { - existingFilters = [existingFilters] - } + const query = req.scope.resolve(ContainerRegistrationKeys.QUERY) - filterableFields[filterByField] = arrayIntersection( - existingFilters, - resources.map((p) => p[resourceId]) - ) - } else { - filterableFields[filterByField] = resources.map((p) => p[resourceId]) + const filters: Record = { + [filterableField]: idsToFilterBy, } + if (existingFilters) { + filters[resourceId] = existingFilters + } + + const { data: resources } = await query.graph({ + entity: entryPoint, + fields: [resourceId], + filters, + }) + + filterableFields[filterByField] = resources.map((p) => p[resourceId]) + req.filterableFields = transformFilterableFields(filterableFields) return next() diff --git a/packages/medusa/src/api/utils/middlewares/products/normalize-data-for-context.ts b/packages/medusa/src/api/utils/middlewares/products/normalize-data-for-context.ts index 2a4567cdfb..9d583d1745 100644 --- a/packages/medusa/src/api/utils/middlewares/products/normalize-data-for-context.ts +++ b/packages/medusa/src/api/utils/middlewares/products/normalize-data-for-context.ts @@ -9,10 +9,17 @@ import { export function normalizeDataForContext() { return async (req: AuthenticatedMedusaRequest, _, next: NextFunction) => { // If the product pricing is not requested, we don't need region information - let withCalculatedPrice = req.queryConfig.fields.some((field) => + const calculatedPriceIndex = req.queryConfig.fields.findIndex((field) => field.startsWith("variants.calculated_price") ) + let withCalculatedPrice = false + if (calculatedPriceIndex !== -1) { + req.queryConfig.fields[calculatedPriceIndex] = + "variants.calculated_price.*" + withCalculatedPrice = true + } + // If the region is passed, we calculate the prices without requesting them. // TODO: This seems a bit messy, reconsider if we want to keep this logic. if (!withCalculatedPrice && req.filterableFields.region_id) {