Skip to content

Commit

Permalink
fis(orchestration): Properly handle select all (#6742)
Browse files Browse the repository at this point in the history
**What**
Fix select `*` without breaking select none `[]`
  • Loading branch information
adrien2p authored Mar 20, 2024
1 parent 05e857d commit 06f22bb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-mice-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/modules-sdk": patch
"@medusajs/orchestration": patch
---

fix(orchestration): Properly handle select all
31 changes: 31 additions & 0 deletions packages/modules-sdk/src/__tests__/remote-quer.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
},
})
})
})
45 changes: 24 additions & 21 deletions packages/modules-sdk/src/remote-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions packages/orchestration/src/joiner/remote-joiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 06f22bb

Please sign in to comment.