fis(orchestration): Properly handle select all (#6742)

**What**
Fix select `*` without breaking select none `[]`
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-03-20 12:09:54 +00:00
1 parent 05e857d256
commit 06f22bb48a
4 files changed
+53 -12

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
---
"@medusajs/modules-sdk": patch
"@medusajs/orchestration": patch
---
fix(orchestration): Properly handle select all
@@ -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,
},
})
})
})
+15 -12
View File
@@ -4,12 +4,14 @@ import {
toRemoteJoinerQuery, toRemoteJoinerQuery,
} from "@medusajs/orchestration" } from "@medusajs/orchestration"
import { import {
JoinerArgument,
JoinerRelationship, JoinerRelationship,
JoinerServiceConfig, JoinerServiceConfig,
LoadedModule, LoadedModule,
ModuleJoinerConfig, ModuleJoinerConfig,
RemoteExpandProperty, RemoteExpandProperty,
RemoteJoinerQuery, RemoteJoinerQuery,
RemoteNestedExpands,
} from "@medusajs/types" } from "@medusajs/types"
import { isString, toPascalCase } from "@medusajs/utils" import { isString, toPascalCase } from "@medusajs/utils"
@@ -78,35 +80,37 @@ export class RemoteQuery {
} }
public static getAllFieldsAndRelations( public static getAllFieldsAndRelations(
data: any, expand: RemoteExpandProperty | RemoteNestedExpands[number],
prefix = "", prefix = "",
args: Record<string, unknown[]> = {} args: JoinerArgument = {} as JoinerArgument
): { ): {
select: string[] select: string[]
relations: string[] relations: string[]
args: Record<string, unknown[]> args: JoinerArgument
} { } {
expand = JSON.parse(JSON.stringify(expand))
let fields: Set<string> = new Set() let fields: Set<string> = new Set()
let relations: string[] = [] let relations: string[] = []
data.fields?.forEach((field: string) => { for (const field of expand.fields ?? []) {
if (field === "*") { if (field === "*") {
// Select all, so we don't specify any field and rely on relation only expand.fields = []
return break
} }
fields.add(prefix ? `${prefix}.${field}` : field) fields.add(prefix ? `${prefix}.${field}` : field)
}) }
args[prefix] = data.args
if (data.expands) { args[prefix] = expand.args
for (const property in data.expands) {
for (const property in expand.expands ?? {}) {
const newPrefix = prefix ? `${prefix}.${property}` : property const newPrefix = prefix ? `${prefix}.${property}` : property
relations.push(newPrefix) relations.push(newPrefix)
fields.delete(newPrefix) fields.delete(newPrefix)
const result = RemoteQuery.getAllFieldsAndRelations( const result = RemoteQuery.getAllFieldsAndRelations(
data.expands[property], expand.expands![property],
newPrefix, newPrefix,
args args
) )
@@ -114,7 +118,6 @@ export class RemoteQuery {
result.select.forEach(fields.add, fields) result.select.forEach(fields.add, fields)
relations = relations.concat(result.relations) relations = relations.concat(result.relations)
} }
}
return { select: [...fields], relations, args } return { select: [...fields], relations, args }
} }
@@ -45,6 +45,7 @@ export class RemoteJoiner {
let filteredData: Record<string, unknown> = {} let filteredData: Record<string, unknown> = {}
if (fields.includes("*")) { if (fields.includes("*")) {
// select all fields
filteredData = data filteredData = data
} else { } else {
filteredData = fields.reduce((acc: any, field: string) => { filteredData = fields.reduce((acc: any, field: string) => {