chore(framework): slightly improve maybe apply link filter middleware (#12113)
**What** - Use the resource id filtering when possible instead of relying on programmatic intersection checks over potential hundreds thousands of resources from the link when in fact it is not necessary to fetch everything to check in memory but instead check in the db - Also fix normalizeDataForContext middlewares
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
"@medusajs/framework": patch
|
||||
---
|
||||
|
||||
chore(framework): slightly improve maybe apply link filter middleware
|
||||
@@ -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<string, unknown> = {
|
||||
[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()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user