fix(medusa, utils): fix the way selects are consumed alongside the relations (#4389)
**What** There is actually an issue with using the `fields` query params with the way the repositories are using our custom query strategy. This pr aims to fix this issue by reworking the strategy. What we had to do was to rework the way the selects are built for each subquery in order to follow the aliasing convention and to be taken into consideration. Alongside these changes, the join used to always select everything, this needed to be changed so that if there are any selects provided for a join, the join should not select everything and let the query select the fields that are requested. Another notable change is that all the repositories are now using the repository util in order to centralize the customization and to have a single place to update when this kind of issue arises. This means that the eager relations when using the query builder are not necessarily taken into account. For that reason, I have removed the `shipping_option` eager option in favor of explicitly asking for the relations like we started to do it in some places. FIXES CORE-1413
This commit is contained in:
committed by
GitHub
parent
9760d4a96c
commit
9dcdc0041a
@@ -109,8 +109,6 @@ describe("Batchjob with type order-export", () => {
|
||||
|
||||
expect(batchJob.status).toBe("completed")
|
||||
|
||||
expect(batchJob.status).toBe("completed")
|
||||
|
||||
exportFilePath = path.resolve(__dirname, batchJob.result.file_key)
|
||||
const isFileExists = (await fs.stat(exportFilePath)).isFile()
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ describe("Batch job of product-export type", () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
|
||||
// @ts-ignore
|
||||
try {
|
||||
const isFileExists = (await fs.stat(exportFilePath))?.isFile()
|
||||
|
||||
@@ -74,8 +75,8 @@ describe("Batch job of product-export type", () => {
|
||||
|
||||
await fs.unlink(exportFilePath)
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
} catch (err) {
|
||||
// noop
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ describe("Database options", () => {
|
||||
|
||||
// Idle time is 1000 ms so this should timeout
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(() => resolve(console.log("")), 2000)
|
||||
setTimeout(() => resolve(undefined), 2000)
|
||||
)
|
||||
|
||||
// This query should fail with a QueryRunnerAlreadyReleasedError
|
||||
|
||||
@@ -293,16 +293,19 @@ describe("/store/customers", () => {
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.orders).toEqual([
|
||||
expect.objectContaining({
|
||||
display_id: 3,
|
||||
status: "canceled",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
display_id: 1,
|
||||
status: "completed",
|
||||
}),
|
||||
])
|
||||
expect(response.data.orders.length).toEqual(2)
|
||||
expect(response.data.orders).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
display_id: 3,
|
||||
status: "canceled",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
display_id: 1,
|
||||
status: "completed",
|
||||
}),
|
||||
])
|
||||
)
|
||||
expect(response.data.orders.length).toEqual(2)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -10,6 +10,10 @@ const {
|
||||
|
||||
const productSeeder = require("../../helpers/store-product-seeder")
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
const {
|
||||
allowedStoreProductsFields,
|
||||
defaultStoreProductsRelations,
|
||||
} = require("@medusajs/medusa/dist")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -988,23 +992,28 @@ describe("/store/products", () => {
|
||||
it("response contains only fields defined with `fields` param", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const fields = allowedStoreProductsFields
|
||||
|
||||
const response = await api.get(
|
||||
"/store/products/test-product?fields=handle"
|
||||
`/store/products/test-product?fields=${fields.join(",")}`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
expect(Object.keys(response.data.product)).toEqual([
|
||||
// fields
|
||||
"handle",
|
||||
// relations
|
||||
"variants",
|
||||
"options",
|
||||
"images",
|
||||
"tags",
|
||||
"collection",
|
||||
"type",
|
||||
])
|
||||
const expectedProperties = [...fields, ...defaultStoreProductsRelations]
|
||||
const actualProperties = [
|
||||
...Object.keys(response.data.product),
|
||||
...Object.keys(response.data.product.variants[0]).map(
|
||||
(key) => `variants.${key}`
|
||||
),
|
||||
"variants.prices.amount",
|
||||
"options.values",
|
||||
]
|
||||
|
||||
expect(Object.keys(response.data.product).length).toEqual(31)
|
||||
expect(actualProperties).toEqual(
|
||||
expect.arrayContaining(expectedProperties)
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,7 +5,6 @@ const { initDb, useDb } = require("../../../../helpers/use-db")
|
||||
const { setPort, useApi } = require("../../../../helpers/use-api")
|
||||
|
||||
const adminSeeder = require("../../../helpers/admin-seeder")
|
||||
const cartSeeder = require("../../../helpers/cart-seeder")
|
||||
const {
|
||||
simpleProductFactory,
|
||||
simpleCustomerFactory,
|
||||
|
||||
Reference in New Issue
Block a user