feat(orchestration,modules-sdk): options autoCreateServiceNameAlias (#6622)
What: Remote joiner options to register service name declared on joiner configs as alias. Default is true, Remote Query in modules-sdk sets it false.
This commit is contained in:
committed by
GitHub
parent
43399c8d0d
commit
4625bd1241
6
.changeset/tricky-ways-join.md
Normal file
6
.changeset/tricky-ways-join.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/orchestration": patch
|
||||
"@medusajs/modules-sdk": patch
|
||||
---
|
||||
|
||||
Remote joiner options autoCreateServiceNameAlias
|
||||
@@ -58,7 +58,8 @@ export class RemoteQuery {
|
||||
|
||||
this.remoteJoiner = new RemoteJoiner(
|
||||
servicesConfig_ as JoinerServiceConfig[],
|
||||
this.remoteFetchData.bind(this)
|
||||
this.remoteFetchData.bind(this),
|
||||
{ autoCreateServiceNameAlias: false }
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -768,4 +768,38 @@ describe("RemoteJoiner", () => {
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("It shouldn't register the service name as an alias if option autoCreateServiceNameAlias is false", async () => {
|
||||
const newJoiner = new RemoteJoiner(
|
||||
serviceConfigs,
|
||||
fetchServiceDataCallback,
|
||||
{ autoCreateServiceNameAlias: false }
|
||||
)
|
||||
|
||||
const query = {
|
||||
service: "user",
|
||||
fields: ["id", "name", "email"],
|
||||
}
|
||||
|
||||
const data = await newJoiner.query(query)
|
||||
|
||||
expect(data).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
id: 1,
|
||||
name: "John Doe",
|
||||
email: "johndoe@example.com",
|
||||
},
|
||||
])
|
||||
)
|
||||
|
||||
const queryWithAlias = {
|
||||
alias: "user",
|
||||
fields: ["id", "name", "email"],
|
||||
}
|
||||
|
||||
expect(newJoiner.query(queryWithAlias)).rejects.toThrowError(
|
||||
`Service with alias "user" was not found.`
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -119,9 +119,16 @@ export class RemoteJoiner {
|
||||
|
||||
constructor(
|
||||
private serviceConfigs: ModuleJoinerConfig[],
|
||||
private remoteFetchData: RemoteFetchDataCallback
|
||||
private remoteFetchData: RemoteFetchDataCallback,
|
||||
private options: {
|
||||
autoCreateServiceNameAlias?: boolean
|
||||
} = {}
|
||||
) {
|
||||
this.serviceConfigs = this.buildReferences(serviceConfigs)
|
||||
this.options.autoCreateServiceNameAlias ??= true
|
||||
|
||||
this.serviceConfigs = this.buildReferences(
|
||||
JSON.parse(JSON.stringify(serviceConfigs))
|
||||
)
|
||||
}
|
||||
|
||||
public setFetchDataCallback(remoteFetchData: RemoteFetchDataCallback): void {
|
||||
@@ -133,6 +140,7 @@ export class RemoteJoiner {
|
||||
string,
|
||||
{ fieldAlias; relationships: JoinerRelationship[] }
|
||||
> = new Map()
|
||||
|
||||
for (const service of serviceConfigs) {
|
||||
if (this.serviceConfigCache.has(service.serviceName!)) {
|
||||
throw new Error(`Service "${service.serviceName}" is already defined.`)
|
||||
@@ -152,7 +160,9 @@ export class RemoteJoiner {
|
||||
service.alias = [service.alias]
|
||||
}
|
||||
|
||||
service.alias.push({ name: service.serviceName! })
|
||||
if (this.options.autoCreateServiceNameAlias) {
|
||||
service.alias.push({ name: service.serviceName! })
|
||||
}
|
||||
|
||||
// handle alias.name as array
|
||||
for (let idx = 0; idx < service.alias.length; idx++) {
|
||||
@@ -197,7 +207,11 @@ export class RemoteJoiner {
|
||||
serviceName: service.serviceName!,
|
||||
args,
|
||||
})
|
||||
this.cacheServiceConfig(serviceConfigs, undefined, alias.name)
|
||||
this.cacheServiceConfig(
|
||||
serviceConfigs,
|
||||
undefined,
|
||||
alias.name as string
|
||||
)
|
||||
}
|
||||
|
||||
this.cacheServiceConfig(serviceConfigs, service.serviceName)
|
||||
@@ -276,7 +290,7 @@ export class RemoteJoiner {
|
||||
private cacheServiceConfig(
|
||||
serviceConfigs,
|
||||
serviceName?: string,
|
||||
serviceAlias?: string | string[]
|
||||
serviceAlias?: string
|
||||
): void {
|
||||
if (serviceAlias) {
|
||||
const name = `alias_${serviceAlias}`
|
||||
|
||||
Reference in New Issue
Block a user