chore: local workflow proxying methods to pass context (#6263)

What:
- When calling a module's method inside a Local Workflow the MedusaContext is passed as the last argument to the method if not provided
- Add `requestId` to req
- A couple of fixes on Remote Joiner and the data fetcher for internal services

Why:
- The context used to initialize the workflow has to be shared with all modules. properties like transactionId will be used to emit events and requestId to trace logs for example.
This commit is contained in:
Carlos R. L. Rodrigues
2024-02-01 10:37:26 -03:00
committed by GitHub
parent a2bf6756ac
commit 45134e4d11
40 changed files with 576 additions and 170 deletions

View File

@@ -9,6 +9,7 @@ import {
import {
ContainerRegistrationKeys,
createMedusaContainer,
MedusaModuleType,
} from "@medusajs/utils"
import { asFunction, asValue } from "awilix"
@@ -19,7 +20,7 @@ export async function loadInternalModule(
): Promise<{ error?: Error } | void> {
const registrationName = resolution.definition.registrationName
const { scope, resources } =
const { resources } =
resolution.moduleDeclaration as InternalModuleDeclaration
let loadedModule: ModuleExports
@@ -111,6 +112,7 @@ export async function loadInternalModule(
const moduleService = loadedModule.service
container.register({
[registrationName]: asFunction((cradle) => {
;(moduleService as any).__type = MedusaModuleType
return new moduleService(
localContainer.cradle,
resolution.options,

View File

@@ -1,3 +1,8 @@
import {
RemoteFetchDataCallback,
RemoteJoiner,
toRemoteJoinerQuery,
} from "@medusajs/orchestration"
import {
JoinerRelationship,
JoinerServiceConfig,
@@ -6,11 +11,6 @@ import {
RemoteExpandProperty,
RemoteJoinerQuery,
} from "@medusajs/types"
import {
RemoteFetchDataCallback,
RemoteJoiner,
toRemoteJoinerQuery,
} from "@medusajs/orchestration"
import { isString, toPascalCase } from "@medusajs/utils"
import { MedusaModule } from "./medusa-module"
@@ -30,7 +30,7 @@ export class RemoteQuery {
servicesConfig?: ModuleJoinerConfig[]
}) {
const servicesConfig_ = [...servicesConfig]
if (!modulesLoaded?.length) {
modulesLoaded = MedusaModule.getLoadedModules().map(
(mod) => Object.values(mod)[0]
@@ -164,6 +164,7 @@ export class RemoteQuery {
"offset",
"cursor",
"sort",
"withDeleted",
]
const availableOptionsAlias = new Map([
["limit", "take"],
@@ -228,7 +229,7 @@ export class RemoteQuery {
if (isString(query)) {
finalQuery = RemoteJoiner.parseQuery(query, variables)
} else if (!isString(finalQuery?.service) && !isString(finalQuery?.alias)) {
finalQuery = toRemoteJoinerQuery(query)
finalQuery = toRemoteJoinerQuery(query, variables)
}
return await this.remoteJoiner.query(finalQuery)