fix(modules-sdk): Fix remote query selection (#6989)
Fix remote query selection for different relations levels as part of a single processed query
This commit is contained in:
1 parent
4d6306f57b
commit
667c8609cc
4 files changed
+124
-49
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/modules-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(modules-sdk): Fix remote query selection
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
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,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should properly handle fields and relations transformation for top level entity", () => {
|
|
||||||
const expand = {
|
|
||||||
fields: ["*"],
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = RemoteQuery.getAllFieldsAndRelations(expand)
|
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
select: undefined,
|
|
||||||
relations: [],
|
|
||||||
args: {
|
|
||||||
"": undefined,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
import { RemoteQuery } from "../remote-query"
|
||||||
|
|
||||||
|
describe("Remote query", () => {
|
||||||
|
it("should properly handle fields and relations transformation", () => {
|
||||||
|
let expand = {
|
||||||
|
fields: ["name", "age"],
|
||||||
|
expands: {
|
||||||
|
friend: {
|
||||||
|
fields: ["name"],
|
||||||
|
expands: {
|
||||||
|
ball: {
|
||||||
|
fields: ["*"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = RemoteQuery.getAllFieldsAndRelations(expand)
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
select: ["name", "age", "friend.name"],
|
||||||
|
relations: ["friend", "friend.ball"],
|
||||||
|
args: {
|
||||||
|
"": undefined,
|
||||||
|
friend: undefined,
|
||||||
|
"friend.ball": undefined,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expand = {
|
||||||
|
fields: [],
|
||||||
|
expands: {
|
||||||
|
friend: {
|
||||||
|
fields: ["name"],
|
||||||
|
expands: {
|
||||||
|
ball: {
|
||||||
|
fields: ["*"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result = RemoteQuery.getAllFieldsAndRelations(expand)
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
select: ["friend.name"],
|
||||||
|
relations: ["friend", "friend.ball"],
|
||||||
|
args: {
|
||||||
|
"": undefined,
|
||||||
|
friend: undefined,
|
||||||
|
"friend.ball": undefined,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expand = {
|
||||||
|
fields: [],
|
||||||
|
expands: {
|
||||||
|
friend: {
|
||||||
|
fields: ["*"],
|
||||||
|
expands: {
|
||||||
|
ball: {
|
||||||
|
fields: ["*"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result = RemoteQuery.getAllFieldsAndRelations(expand)
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
select: [],
|
||||||
|
relations: ["friend", "friend.ball"],
|
||||||
|
args: {
|
||||||
|
"": undefined,
|
||||||
|
friend: undefined,
|
||||||
|
"friend.ball": undefined,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expand = {
|
||||||
|
fields: [],
|
||||||
|
expands: {
|
||||||
|
friend: {
|
||||||
|
fields: [],
|
||||||
|
expands: {
|
||||||
|
ball: {
|
||||||
|
fields: ["*"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result = RemoteQuery.getAllFieldsAndRelations(expand)
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
select: [],
|
||||||
|
relations: ["friend", "friend.ball"],
|
||||||
|
args: {
|
||||||
|
"": undefined,
|
||||||
|
friend: undefined,
|
||||||
|
"friend.ball": undefined,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -93,9 +93,11 @@ export class RemoteQuery {
|
|||||||
let fields: Set<string> = new Set()
|
let fields: Set<string> = new Set()
|
||||||
let relations: string[] = []
|
let relations: string[] = []
|
||||||
|
|
||||||
|
let shouldSelectAll = false
|
||||||
|
|
||||||
for (const field of expand.fields ?? []) {
|
for (const field of expand.fields ?? []) {
|
||||||
if (field === "*") {
|
if (field === "*") {
|
||||||
expand.fields = undefined
|
shouldSelectAll = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
fields.add(prefix ? `${prefix}.${field}` : field)
|
fields.add(prefix ? `${prefix}.${field}` : field)
|
||||||
@@ -120,7 +122,13 @@ export class RemoteQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const allFields = Array.from(fields)
|
const allFields = Array.from(fields)
|
||||||
return { select: allFields.length ? allFields : undefined, relations, args }
|
const select =
|
||||||
|
allFields.length && !shouldSelectAll
|
||||||
|
? allFields
|
||||||
|
: shouldSelectAll
|
||||||
|
? undefined
|
||||||
|
: []
|
||||||
|
return { select, relations, args }
|
||||||
}
|
}
|
||||||
|
|
||||||
private hasPagination(options: { [attr: string]: unknown }): boolean {
|
private hasPagination(options: { [attr: string]: unknown }): boolean {
|
||||||
|
|||||||
Reference in new issue
Block a user