fix(modules-sdk): Select all should send undefined instead of an empty array (#6982)

This commit is contained in:
Adrien de Peretti
2024-04-06 21:37:22 +02:00
committed by GitHub
parent 81ea044f31
commit 528ef4ca90
5 changed files with 32 additions and 9 deletions

View File

@@ -0,0 +1,7 @@
---
"@medusajs/modules-sdk": patch
"@medusajs/orchestration": patch
"@medusajs/types": patch
---
fix(modules-sdk): Select all should send undefined instead of an empty array

View File

@@ -28,4 +28,20 @@ describe("Remote query", () => {
},
})
})
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,
},
})
})
})

View File

@@ -10,12 +10,11 @@ import {
LoadedModule,
ModuleJoinerConfig,
RemoteExpandProperty,
RemoteJoinerOptions,
RemoteJoinerQuery,
RemoteNestedExpands,
} from "@medusajs/types"
import { isString, toPascalCase } from "@medusajs/utils"
import { RemoteJoinerOptions } from "@medusajs/types"
import { MedusaModule } from "./medusa-module"
export class RemoteQuery {
@@ -85,7 +84,7 @@ export class RemoteQuery {
prefix = "",
args: JoinerArgument = {} as JoinerArgument
): {
select: string[]
select?: string[]
relations: string[]
args: JoinerArgument
} {
@@ -96,7 +95,7 @@ export class RemoteQuery {
for (const field of expand.fields ?? []) {
if (field === "*") {
expand.fields = []
expand.fields = undefined
break
}
fields.add(prefix ? `${prefix}.${field}` : field)
@@ -116,11 +115,12 @@ export class RemoteQuery {
args
)
result.select.forEach(fields.add, fields)
result.select?.forEach(fields.add, fields)
relations = relations.concat(result.relations)
}
return { select: [...fields], relations, args }
const allFields = Array.from(fields)
return { select: allFields.length ? allFields : undefined, relations, args }
}
private hasPagination(options: { [attr: string]: unknown }): boolean {

View File

@@ -36,7 +36,7 @@ export class RemoteJoiner {
private static filterFields(
data: any,
fields: string[],
fields?: string[],
expands?: RemoteNestedExpands
): Record<string, unknown> | undefined {
if (!fields || !data) {

View File

@@ -90,7 +90,7 @@ export interface RemoteJoinerOptions {
export interface RemoteNestedExpands {
[key: string]: {
fields: string[]
fields?: string[]
args?: JoinerArgument[]
expands?: RemoteNestedExpands
}
@@ -101,7 +101,7 @@ export interface RemoteExpandProperty {
parent: string
parentConfig?: JoinerServiceConfig
serviceConfig: JoinerServiceConfig
fields: string[]
fields?: string[]
args?: JoinerArgument[]
expands?: RemoteNestedExpands
}