Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fis(orchestration): Properly handle select all #6742

Merged
merged 8 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
adrien2p marked this conversation as resolved.
Show resolved Hide resolved

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
Loading