fix(orchestration): handle multiple shortcuts with same name (#12066)
This commit is contained in:
committed by
GitHub
parent
cc71dc2620
commit
92c7baa561
5
.changeset/tame-hats-crash.md
Normal file
5
.changeset/tame-hats-crash.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/orchestration": patch
|
||||
---
|
||||
|
||||
fix(orchestration): handle multiple field aliases with same name
|
||||
@@ -317,7 +317,8 @@ export class RemoteJoiner {
|
||||
service_.relationships?.set(aliasName, rel)
|
||||
}
|
||||
|
||||
Object.assign(service_.fieldAlias ?? {}, extend.fieldAlias)
|
||||
// Multiple "fieldAlias" w/ same name need the entity to handle different paths
|
||||
this.mergeFieldAlias(service_, extend)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,6 +365,33 @@ export class RemoteJoiner {
|
||||
return serviceConfigs
|
||||
}
|
||||
|
||||
private mergeFieldAlias(service_, extend) {
|
||||
for (const [alias, fieldAlias] of Object.entries(extend.fieldAlias ?? {})) {
|
||||
const objAlias = isString(fieldAlias)
|
||||
? { path: fieldAlias }
|
||||
: (fieldAlias as object)
|
||||
|
||||
if (service_.fieldAlias[alias]) {
|
||||
if (!Array.isArray(service_.fieldAlias[alias])) {
|
||||
service_.fieldAlias[alias] = [service_.fieldAlias[alias]]
|
||||
}
|
||||
|
||||
if (
|
||||
service_.fieldAlias[alias].some((f) => f.entity === extend.entity)
|
||||
) {
|
||||
throw new Error(
|
||||
`Cannot add alias "${alias}" for "${extend.serviceName}". It is already defined for Entity "${extend.entity}".`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
service_.fieldAlias[alias] = {
|
||||
...objAlias,
|
||||
entity: extend.entity,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private getServiceConfig({
|
||||
serviceName,
|
||||
serviceAlias,
|
||||
@@ -1081,7 +1109,23 @@ export class RemoteJoiner {
|
||||
}) {
|
||||
const serviceConfig = currentServiceConfig
|
||||
const fieldAlias = currentServiceConfig.fieldAlias ?? {}
|
||||
const alias = fieldAlias[property] as any
|
||||
let alias = fieldAlias[property] as any
|
||||
|
||||
// Handle multiple shortcuts for the same property
|
||||
if (Array.isArray(alias)) {
|
||||
const currentPathEntity = parsedExpands.get(
|
||||
[BASE_PATH, ...currentPath].join(".")
|
||||
)?.entity
|
||||
|
||||
alias = alias.find((a) => a.entity == currentPathEntity)
|
||||
if (!alias) {
|
||||
throw new Error(
|
||||
`Cannot resolve alias path "${currentPath.join(
|
||||
"."
|
||||
)}" that matches entity ${currentPathEntity}.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const path = isString(alias) ? alias : alias.path
|
||||
const fieldAliasIsList = isString(alias) ? false : !!alias.isList
|
||||
|
||||
Reference in New Issue
Block a user