fis(orchestration): Properly handle select all (#6742)
**What** Fix select `*` without breaking select none `[]`
This commit is contained in:
committed by
GitHub
parent
05e857d256
commit
06f22bb48a
6
.changeset/cold-mice-deliver.md
Normal file
6
.changeset/cold-mice-deliver.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/modules-sdk": patch
|
||||
"@medusajs/orchestration": patch
|
||||
---
|
||||
|
||||
fix(orchestration): Properly handle select all
|
||||
31
packages/modules-sdk/src/__tests__/remote-quer.spec.ts
Normal file
31
packages/modules-sdk/src/__tests__/remote-quer.spec.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { RemoteQuery } from "../remote-query"
|
||||
|
||||
describe("Remote query", () => {
|
||||
it("should properly handle fields and relations transformation", () => {
|
||||
const expand = {
|
||||
fields: ["name", "age"],
|
||||
expands: {
|
||||
friend: {
|
||||
fields: ["name"],
|
||||
expands: {
|
||||
ball: {
|
||||
fields: ["*"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const result = RemoteQuery.getAllFieldsAndRelations(expand)
|
||||
|
||||
expect(result).toEqual({
|
||||
select: ["name", "age", "friend.name"],
|
||||
relations: ["friend", "friend.ball"],
|
||||
args: {
|
||||
"": undefined,
|
||||
friend: undefined,
|
||||
"friend.ball": undefined,
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -4,12 +4,14 @@ import {
|
||||
toRemoteJoinerQuery,
|
||||
} from "@medusajs/orchestration"
|
||||
import {
|
||||
JoinerArgument,
|
||||
JoinerRelationship,
|
||||
JoinerServiceConfig,
|
||||
LoadedModule,
|
||||
ModuleJoinerConfig,
|
||||
RemoteExpandProperty,
|
||||
RemoteJoinerQuery,
|
||||
RemoteNestedExpands,
|
||||
} from "@medusajs/types"
|
||||
import { isString, toPascalCase } from "@medusajs/utils"
|
||||
|
||||
@@ -78,42 +80,43 @@ export class RemoteQuery {
|
||||
}
|
||||
|
||||
public static getAllFieldsAndRelations(
|
||||
data: any,
|
||||
expand: RemoteExpandProperty | RemoteNestedExpands[number],
|
||||
prefix = "",
|
||||
args: Record<string, unknown[]> = {}
|
||||
args: JoinerArgument = {} as JoinerArgument
|
||||
): {
|
||||
select: string[]
|
||||
relations: string[]
|
||||
args: Record<string, unknown[]>
|
||||
args: JoinerArgument
|
||||
} {
|
||||
expand = JSON.parse(JSON.stringify(expand))
|
||||
|
||||
let fields: Set<string> = new Set()
|
||||
let relations: string[] = []
|
||||
|
||||
data.fields?.forEach((field: string) => {
|
||||
for (const field of expand.fields ?? []) {
|
||||
if (field === "*") {
|
||||
// Select all, so we don't specify any field and rely on relation only
|
||||
return
|
||||
expand.fields = []
|
||||
break
|
||||
}
|
||||
fields.add(prefix ? `${prefix}.${field}` : field)
|
||||
})
|
||||
args[prefix] = data.args
|
||||
}
|
||||
|
||||
if (data.expands) {
|
||||
for (const property in data.expands) {
|
||||
const newPrefix = prefix ? `${prefix}.${property}` : property
|
||||
args[prefix] = expand.args
|
||||
|
||||
relations.push(newPrefix)
|
||||
fields.delete(newPrefix)
|
||||
for (const property in expand.expands ?? {}) {
|
||||
const newPrefix = prefix ? `${prefix}.${property}` : property
|
||||
|
||||
const result = RemoteQuery.getAllFieldsAndRelations(
|
||||
data.expands[property],
|
||||
newPrefix,
|
||||
args
|
||||
)
|
||||
relations.push(newPrefix)
|
||||
fields.delete(newPrefix)
|
||||
|
||||
result.select.forEach(fields.add, fields)
|
||||
relations = relations.concat(result.relations)
|
||||
}
|
||||
const result = RemoteQuery.getAllFieldsAndRelations(
|
||||
expand.expands![property],
|
||||
newPrefix,
|
||||
args
|
||||
)
|
||||
|
||||
result.select.forEach(fields.add, fields)
|
||||
relations = relations.concat(result.relations)
|
||||
}
|
||||
|
||||
return { select: [...fields], relations, args }
|
||||
|
||||
@@ -45,6 +45,7 @@ export class RemoteJoiner {
|
||||
let filteredData: Record<string, unknown> = {}
|
||||
|
||||
if (fields.includes("*")) {
|
||||
// select all fields
|
||||
filteredData = data
|
||||
} else {
|
||||
filteredData = fields.reduce((acc: any, field: string) => {
|
||||
|
||||
Reference in New Issue
Block a user