fix(orchestration): Throw if not exists using filters (#9275)
This commit is contained in:
@@ -231,6 +231,40 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it(`should throw if not exists`, async () => {
|
||||||
|
const err = await query
|
||||||
|
.graph(
|
||||||
|
{
|
||||||
|
entity: "product",
|
||||||
|
fields: ["id", "title", "variants.*", "variants.prices.amount"],
|
||||||
|
filters: {
|
||||||
|
id: "non-existing-id",
|
||||||
|
variants: {
|
||||||
|
prices: {
|
||||||
|
amount: {
|
||||||
|
$gt: 100,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
throwIfKeyNotFound: true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(err).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
message: expect.stringContaining(
|
||||||
|
"Product id not found: non-existing-id"
|
||||||
|
),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it(`should perform cross module query and apply filters correctly to the correct modules [1]`, async () => {
|
it(`should perform cross module query and apply filters correctly to the correct modules [1]`, async () => {
|
||||||
const { data } = await query.graph({
|
const { data } = await query.graph({
|
||||||
entity: "product",
|
entity: "product",
|
||||||
|
|||||||
@@ -357,7 +357,6 @@ describe("RemoteJoiner", () => {
|
|||||||
|
|
||||||
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
||||||
expect(serviceMock.userService).toHaveBeenCalledWith({
|
expect(serviceMock.userService).toHaveBeenCalledWith({
|
||||||
args: [],
|
|
||||||
fields: ["id", "name", "email"],
|
fields: ["id", "name", "email"],
|
||||||
options: { id: ["1"] },
|
options: { id: ["1"] },
|
||||||
})
|
})
|
||||||
@@ -379,7 +378,6 @@ describe("RemoteJoiner", () => {
|
|||||||
|
|
||||||
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
||||||
expect(serviceMock.userService).toHaveBeenCalledWith({
|
expect(serviceMock.userService).toHaveBeenCalledWith({
|
||||||
args: [],
|
|
||||||
fields: ["id"],
|
fields: ["id"],
|
||||||
options: { id: ["1"] },
|
options: { id: ["1"] },
|
||||||
})
|
})
|
||||||
@@ -432,7 +430,6 @@ describe("RemoteJoiner", () => {
|
|||||||
|
|
||||||
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
||||||
expect(serviceMock.userService).toHaveBeenCalledWith({
|
expect(serviceMock.userService).toHaveBeenCalledWith({
|
||||||
args: [],
|
|
||||||
fields: ["username", "email"],
|
fields: ["username", "email"],
|
||||||
options: { id: ["1"] },
|
options: { id: ["1"] },
|
||||||
})
|
})
|
||||||
@@ -464,7 +461,6 @@ describe("RemoteJoiner", () => {
|
|||||||
|
|
||||||
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
|
||||||
expect(serviceMock.userService).toHaveBeenCalledWith({
|
expect(serviceMock.userService).toHaveBeenCalledWith({
|
||||||
args: [],
|
|
||||||
fields: ["username", "email", "products"],
|
fields: ["username", "email", "products"],
|
||||||
expands: {
|
expands: {
|
||||||
products: {
|
products: {
|
||||||
|
|||||||
@@ -1189,34 +1189,32 @@ export class RemoteJoiner {
|
|||||||
throw new Error(`Service "${queryObj.service}" was not found.`)
|
throw new Error(`Service "${queryObj.service}" was not found.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
let pkName = serviceConfig.primaryKeys[0]
|
const { primaryKeyArg, otherArgs, pkName } = gerPrimaryKeysAndOtherFilters({
|
||||||
const primaryKeyArg = queryObj.args?.find((arg) => {
|
serviceConfig,
|
||||||
const inc = serviceConfig.primaryKeys.includes(arg.name)
|
queryObj,
|
||||||
if (inc) {
|
|
||||||
pkName = arg.name
|
|
||||||
}
|
|
||||||
return inc
|
|
||||||
})
|
})
|
||||||
const otherArgs = queryObj.args?.filter(
|
|
||||||
(arg) => !serviceConfig.primaryKeys.includes(arg.name)
|
|
||||||
)
|
|
||||||
|
|
||||||
const implodeMapping: InternalImplodeMapping[] = []
|
const implodeMapping: InternalImplodeMapping[] = []
|
||||||
const parsedExpands = this.parseExpands({
|
const parseExpandsConfig: Parameters<typeof this.parseExpands>[0] = {
|
||||||
initialService: {
|
initialService: {
|
||||||
property: "",
|
property: "",
|
||||||
parent: "",
|
parent: "",
|
||||||
serviceConfig,
|
serviceConfig,
|
||||||
entity: serviceConfig.entity,
|
entity: serviceConfig.entity,
|
||||||
fields: queryObj.fields,
|
fields: queryObj.fields,
|
||||||
args: otherArgs,
|
|
||||||
},
|
},
|
||||||
query: queryObj,
|
query: queryObj,
|
||||||
serviceConfig,
|
serviceConfig,
|
||||||
expands: queryObj.expands!,
|
expands: queryObj.expands!,
|
||||||
implodeMapping,
|
implodeMapping,
|
||||||
options,
|
options,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if (otherArgs) {
|
||||||
|
parseExpandsConfig.initialService.args = otherArgs
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedExpands = this.parseExpands(parseExpandsConfig)
|
||||||
|
|
||||||
const root = parsedExpands.get(BASE_PATH)!
|
const root = parsedExpands.get(BASE_PATH)!
|
||||||
|
|
||||||
@@ -1239,3 +1237,49 @@ export class RemoteJoiner {
|
|||||||
return response.data
|
return response.data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function gerPrimaryKeysAndOtherFilters({ serviceConfig, queryObj }): {
|
||||||
|
primaryKeyArg: { name: string; value: any } | undefined
|
||||||
|
otherArgs: { name: string; value: any }[] | undefined
|
||||||
|
pkName: string
|
||||||
|
} {
|
||||||
|
let pkName = serviceConfig.primaryKeys[0]
|
||||||
|
let primaryKeyArg = queryObj.args?.find((arg) => {
|
||||||
|
const include = serviceConfig.primaryKeys.includes(arg.name)
|
||||||
|
if (include) {
|
||||||
|
pkName = arg.name
|
||||||
|
}
|
||||||
|
return include
|
||||||
|
})
|
||||||
|
|
||||||
|
let otherArgs = queryObj.args?.filter(
|
||||||
|
(arg) => !serviceConfig.primaryKeys.includes(arg.name)
|
||||||
|
)
|
||||||
|
|
||||||
|
const filters =
|
||||||
|
queryObj.args?.find((arg) => arg.name === "filters")?.value ?? {}
|
||||||
|
|
||||||
|
if (!primaryKeyArg) {
|
||||||
|
const primaryKeyFilter = Object.keys(filters).find((key) => {
|
||||||
|
return serviceConfig.primaryKeys.includes(key)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (primaryKeyFilter) {
|
||||||
|
pkName = primaryKeyFilter
|
||||||
|
primaryKeyArg = {
|
||||||
|
name: primaryKeyFilter,
|
||||||
|
value: filters[primaryKeyFilter],
|
||||||
|
}
|
||||||
|
|
||||||
|
delete filters[primaryKeyFilter]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
otherArgs = otherArgs?.length ? otherArgs : undefined
|
||||||
|
|
||||||
|
return {
|
||||||
|
primaryKeyArg,
|
||||||
|
otherArgs,
|
||||||
|
pkName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ import { accessSync } from "fs"
|
|||||||
import * as path from "path"
|
import * as path from "path"
|
||||||
import { dirname, join, normalize } from "path"
|
import { dirname, join, normalize } from "path"
|
||||||
import {
|
import {
|
||||||
MapToConfig,
|
|
||||||
camelToSnakeCase,
|
camelToSnakeCase,
|
||||||
deduplicate,
|
deduplicate,
|
||||||
getCallerFilePath,
|
getCallerFilePath,
|
||||||
isObject,
|
isObject,
|
||||||
lowerCaseFirst,
|
lowerCaseFirst,
|
||||||
|
MapToConfig,
|
||||||
pluralize,
|
pluralize,
|
||||||
toCamelCase,
|
toCamelCase,
|
||||||
upperCaseFirst,
|
upperCaseFirst,
|
||||||
|
|||||||
Reference in New Issue
Block a user