fix(orchestration): Infinite loop by reference update (#5023)
* fix(orchestration): Infinite loop by reference update * fix * handle conflict alias config * rm field alias * Create wet-gorillas-laugh.md
This commit is contained in:
1 parent
0953bdfe84
commit
107aaa371c
3 files changed
+51
-13
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/orchestration": patch
|
||||||
|
"@medusajs/types": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(orchestration, types): Improve fieldAlias and prevent infinite loop by validating at development time
|
||||||
@@ -128,15 +128,18 @@ export class RemoteJoiner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private buildReferences(serviceConfigs: ModuleJoinerConfig[]) {
|
private buildReferences(serviceConfigs: ModuleJoinerConfig[]) {
|
||||||
const expandedRelationships: Map<string, JoinerRelationship[]> = new Map()
|
const expandedRelationships: Map<
|
||||||
|
string,
|
||||||
|
{ fieldAlias; relationships: JoinerRelationship[] }
|
||||||
|
> = new Map()
|
||||||
for (const service of serviceConfigs) {
|
for (const service of serviceConfigs) {
|
||||||
if (this.serviceConfigCache.has(service.serviceName!)) {
|
if (this.serviceConfigCache.has(service.serviceName!)) {
|
||||||
throw new Error(`Service "${service.serviceName}" is already defined.`)
|
throw new Error(`Service "${service.serviceName}" is already defined.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!service.relationships) {
|
service.fieldAlias ??= {}
|
||||||
service.relationships = []
|
service.relationships ??= []
|
||||||
}
|
service.extends ??= []
|
||||||
|
|
||||||
// add aliases
|
// add aliases
|
||||||
const isReadOnlyDefinition =
|
const isReadOnlyDefinition =
|
||||||
@@ -175,26 +178,47 @@ export class RemoteJoiner {
|
|||||||
this.cacheServiceConfig(serviceConfigs, service.serviceName)
|
this.cacheServiceConfig(serviceConfigs, service.serviceName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!service.extends) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const extend of service.extends) {
|
for (const extend of service.extends) {
|
||||||
if (!expandedRelationships.has(extend.serviceName)) {
|
if (!expandedRelationships.has(extend.serviceName)) {
|
||||||
expandedRelationships.set(extend.serviceName, [])
|
expandedRelationships.set(extend.serviceName, {
|
||||||
|
fieldAlias: {},
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
expandedRelationships.get(extend.serviceName)!.push(extend.relationship)
|
const service_ = expandedRelationships.get(extend.serviceName)!
|
||||||
|
service_.relationships.push(extend.relationship)
|
||||||
|
Object.assign(service_.fieldAlias ?? {}, extend.fieldAlias)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [serviceName, relationships] of expandedRelationships) {
|
for (const [
|
||||||
|
serviceName,
|
||||||
|
{ fieldAlias, relationships },
|
||||||
|
] of expandedRelationships) {
|
||||||
if (!this.serviceConfigCache.has(serviceName)) {
|
if (!this.serviceConfigCache.has(serviceName)) {
|
||||||
throw new Error(`Service "${serviceName}" was not found`)
|
throw new Error(`Service "${serviceName}" was not found`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const service = this.serviceConfigCache.get(serviceName)
|
const service_ = this.serviceConfigCache.get(serviceName)!
|
||||||
service!.relationships?.push(...relationships)
|
service_.relationships?.push(...relationships)
|
||||||
|
Object.assign(service_.fieldAlias!, fieldAlias ?? {})
|
||||||
|
|
||||||
|
if (Object.keys(service_.fieldAlias!).length) {
|
||||||
|
const conflictAliases = service_.relationships!.filter(
|
||||||
|
(relationship) => {
|
||||||
|
return fieldAlias[relationship.alias]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (conflictAliases.length) {
|
||||||
|
throw new Error(
|
||||||
|
`Conflict configuration for service "${serviceName}". The following aliases are already defined as relationships: ${conflictAliases
|
||||||
|
.map((relationship) => relationship.alias)
|
||||||
|
.join(", ")}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return serviceConfigs
|
return serviceConfigs
|
||||||
|
|||||||
@@ -124,6 +124,14 @@ export type ModuleJoinerConfig = Omit<
|
|||||||
relationships?: ModuleJoinerRelationship[]
|
relationships?: ModuleJoinerRelationship[]
|
||||||
extends?: {
|
extends?: {
|
||||||
serviceName: string
|
serviceName: string
|
||||||
|
fieldAlias?: Record<
|
||||||
|
string,
|
||||||
|
| string
|
||||||
|
| {
|
||||||
|
path: string
|
||||||
|
forwardArgumentsOnPath: string[]
|
||||||
|
}
|
||||||
|
> // alias for deeper nested relationships (e.g. { 'price': 'prices.calculated_price_set.amount' })
|
||||||
relationship: ModuleJoinerRelationship
|
relationship: ModuleJoinerRelationship
|
||||||
}[]
|
}[]
|
||||||
serviceName?: string
|
serviceName?: string
|
||||||
|
|||||||
Reference in new issue
Block a user