fix(workflows-sdk): use loaded modules when container is empty (#7422)
This commit is contained in:
1 parent
05077bab32
commit
f3d19f5394
13 files changed
+12
-151
No files matched your search
@@ -244,18 +244,6 @@ export class RemoteJoiner {
|
|||||||
{ fieldAlias, relationships },
|
{ fieldAlias, relationships },
|
||||||
] of expandedRelationships) {
|
] of expandedRelationships) {
|
||||||
if (!this.serviceConfigCache.has(serviceName)) {
|
if (!this.serviceConfigCache.has(serviceName)) {
|
||||||
// If true, the relationship is an internal service from the medusa core
|
|
||||||
// If modules are being used ouside of the core, we should not be throwing
|
|
||||||
// errors when the core services are not found in cache.
|
|
||||||
// TODO: Remove when there are no more "internal" services
|
|
||||||
const isInternalServicePresent = relationships.some(
|
|
||||||
(rel) => rel.isInternalService === true
|
|
||||||
)
|
|
||||||
|
|
||||||
if (isInternalServicePresent) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`Service "${serviceName}" was not found`)
|
throw new Error(`Service "${serviceName}" was not found`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,6 @@ export type JoinerRelationship = {
|
|||||||
foreignKey: string
|
foreignKey: string
|
||||||
primaryKey: string
|
primaryKey: string
|
||||||
serviceName: string
|
serviceName: string
|
||||||
/**
|
|
||||||
* If true, the relationship is an internal service from the medusa core
|
|
||||||
* TODO: Remove when there are no more "internal" services
|
|
||||||
*/
|
|
||||||
isInternalService?: boolean
|
|
||||||
/**
|
/**
|
||||||
* In an inverted relationship the foreign key is on the other service and the primary key is on the current service
|
* In an inverted relationship the foreign key is on the other service and the primary key is on the current service
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -220,10 +220,6 @@ export type ModuleJoinerConfig = Omit<
|
|||||||
}
|
}
|
||||||
|
|
||||||
export declare type ModuleJoinerRelationship = JoinerRelationship & {
|
export declare type ModuleJoinerRelationship = JoinerRelationship & {
|
||||||
/**
|
|
||||||
* If true, the relationship is an internal service from the medusa core TODO: Remove when there are no more "internal" services
|
|
||||||
*/
|
|
||||||
isInternalService?: boolean
|
|
||||||
/**
|
/**
|
||||||
* If true, the link joiner will cascade deleting the relationship
|
* If true, the link joiner will cascade deleting the relationship
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
|
import { MedusaModule } from "@medusajs/modules-sdk"
|
||||||
import {
|
import {
|
||||||
LocalWorkflow,
|
LocalWorkflow,
|
||||||
TransactionHandlerType,
|
TransactionHandlerType,
|
||||||
TransactionState,
|
TransactionState,
|
||||||
} from "@medusajs/orchestration"
|
} from "@medusajs/orchestration"
|
||||||
import { LoadedModule, MedusaContainer } from "@medusajs/types"
|
import { LoadedModule, MedusaContainer } from "@medusajs/types"
|
||||||
|
import { MedusaContextType, isPresent } from "@medusajs/utils"
|
||||||
import { MedusaModule } from "@medusajs/modules-sdk"
|
|
||||||
import { MedusaContextType } from "@medusajs/utils"
|
|
||||||
import { EOL } from "os"
|
import { EOL } from "os"
|
||||||
import { ulid } from "ulid"
|
import { ulid } from "ulid"
|
||||||
import { MedusaWorkflow } from "../medusa-workflow"
|
import { MedusaWorkflow } from "../medusa-workflow"
|
||||||
@@ -62,13 +61,16 @@ function createContextualWorkflowRunner<
|
|||||||
},
|
},
|
||||||
...args
|
...args
|
||||||
) => {
|
) => {
|
||||||
if (!executionContainer && !flow.container) {
|
if (!executionContainer) {
|
||||||
|
const container_ = flow.container as MedusaContainer
|
||||||
|
if (!container_ || !isPresent(container_?.registrations)) {
|
||||||
executionContainer = MedusaModule.getLoadedModules().map(
|
executionContainer = MedusaModule.getLoadedModules().map(
|
||||||
(mod) => Object.values(mod)[0]
|
(mod) => Object.values(mod)[0]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!flow.container) {
|
if (executionContainer) {
|
||||||
flow.container = executionContainer
|
flow.container = executionContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import {
|
|||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { asValue } from "awilix"
|
import { asValue } from "awilix"
|
||||||
import { remoteQueryFetchData } from "../utils/remote-query-fetch-data"
|
|
||||||
|
|
||||||
export function mergeDefaultModules(
|
export function mergeDefaultModules(
|
||||||
modulesConfig: CommonTypes.ConfigModule["modules"]
|
modulesConfig: CommonTypes.ConfigModule["modules"]
|
||||||
@@ -93,7 +92,6 @@ async function runMedusaAppMigrations({
|
|||||||
if (revert) {
|
if (revert) {
|
||||||
await MedusaAppMigrateDown({
|
await MedusaAppMigrateDown({
|
||||||
modulesConfig: configModules,
|
modulesConfig: configModules,
|
||||||
remoteFetchData: remoteQueryFetchData(container),
|
|
||||||
sharedContainer: container,
|
sharedContainer: container,
|
||||||
sharedResourcesConfig,
|
sharedResourcesConfig,
|
||||||
injectedDependencies,
|
injectedDependencies,
|
||||||
@@ -101,7 +99,6 @@ async function runMedusaAppMigrations({
|
|||||||
} else {
|
} else {
|
||||||
await MedusaAppMigrateUp({
|
await MedusaAppMigrateUp({
|
||||||
modulesConfig: configModules,
|
modulesConfig: configModules,
|
||||||
remoteFetchData: remoteQueryFetchData(container),
|
|
||||||
sharedContainer: container,
|
sharedContainer: container,
|
||||||
sharedResourcesConfig,
|
sharedResourcesConfig,
|
||||||
injectedDependencies,
|
injectedDependencies,
|
||||||
@@ -179,7 +176,6 @@ export const loadMedusaApp = async (
|
|||||||
const medusaApp = await MedusaApp({
|
const medusaApp = await MedusaApp({
|
||||||
workerMode: configModule.projectConfig.worker_mode,
|
workerMode: configModule.projectConfig.worker_mode,
|
||||||
modulesConfig: configModules,
|
modulesConfig: configModules,
|
||||||
remoteFetchData: remoteQueryFetchData(container),
|
|
||||||
sharedContainer: container,
|
sharedContainer: container,
|
||||||
sharedResourcesConfig,
|
sharedResourcesConfig,
|
||||||
injectedDependencies,
|
injectedDependencies,
|
||||||
@@ -254,7 +250,6 @@ export async function runModulesLoader({
|
|||||||
|
|
||||||
await MedusaApp({
|
await MedusaApp({
|
||||||
modulesConfig: configModules,
|
modulesConfig: configModules,
|
||||||
remoteFetchData: remoteQueryFetchData(container),
|
|
||||||
sharedContainer: container,
|
sharedContainer: container,
|
||||||
sharedResourcesConfig,
|
sharedResourcesConfig,
|
||||||
injectedDependencies,
|
injectedDependencies,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
export * from "./clean-response-data"
|
export * from "./clean-response-data"
|
||||||
export * from "./exception-formatter"
|
export * from "./exception-formatter"
|
||||||
export * from "./omit-deep"
|
|
||||||
export * from "./remote-query-fetch-data"
|
|
||||||
export * from "./remove-undefined-properties"
|
|
||||||
export * from "./middlewares"
|
export * from "./middlewares"
|
||||||
|
export * from "./omit-deep"
|
||||||
|
export * from "./remove-undefined-properties"
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
import { MedusaModule, RemoteQuery } from "@medusajs/modules-sdk"
|
|
||||||
import { MedusaContainer } from "@medusajs/types"
|
|
||||||
|
|
||||||
function hasPagination(options: { [attr: string]: unknown }): boolean {
|
|
||||||
if (!options) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const attrs = ["skip"]
|
|
||||||
return Object.keys(options).some((key) => attrs.includes(key))
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildPagination(options, count) {
|
|
||||||
return {
|
|
||||||
skip: options.skip,
|
|
||||||
take: options.take,
|
|
||||||
count,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function remoteQueryFetchData(container: MedusaContainer) {
|
|
||||||
return async (expand, keyField, ids, relationship) => {
|
|
||||||
const serviceConfig = expand.serviceConfig
|
|
||||||
const service = container.resolve(serviceConfig.serviceName, {
|
|
||||||
allowUnregistered: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (MedusaModule.isInstalled(serviceConfig.serviceName)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let filters = {}
|
|
||||||
const options = {
|
|
||||||
...RemoteQuery.getAllFieldsAndRelations(expand),
|
|
||||||
}
|
|
||||||
|
|
||||||
const availableOptions = [
|
|
||||||
"skip",
|
|
||||||
"take",
|
|
||||||
"limit",
|
|
||||||
"offset",
|
|
||||||
"order",
|
|
||||||
"sort",
|
|
||||||
"withDeleted",
|
|
||||||
]
|
|
||||||
const availableOptionsAlias = new Map([
|
|
||||||
["limit", "take"],
|
|
||||||
["offset", "skip"],
|
|
||||||
["sort", "order"],
|
|
||||||
])
|
|
||||||
for (const arg of expand.args || []) {
|
|
||||||
if (arg.name === "filters" && arg.value) {
|
|
||||||
filters = { ...arg.value }
|
|
||||||
} else if (availableOptions.includes(arg.name)) {
|
|
||||||
const argName = availableOptionsAlias.has(arg.name)
|
|
||||||
? availableOptionsAlias.get(arg.name)
|
|
||||||
: arg.name
|
|
||||||
options[argName] = arg.value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const expandRelations = Object.keys(expand.expands ?? {})
|
|
||||||
|
|
||||||
options.relations = options.relations.filter(
|
|
||||||
(relation) => !expandRelations.some((ex) => relation.startsWith(ex))
|
|
||||||
)
|
|
||||||
|
|
||||||
options.select = options.relations.filter(
|
|
||||||
(field) => !expandRelations.some((ex) => field.startsWith(ex))
|
|
||||||
)
|
|
||||||
|
|
||||||
if (ids) {
|
|
||||||
filters[keyField] = ids
|
|
||||||
}
|
|
||||||
|
|
||||||
const hasPagination_ = hasPagination(options)
|
|
||||||
|
|
||||||
let methodName = hasPagination_ ? "listAndCount" : "list"
|
|
||||||
|
|
||||||
if (relationship?.args?.methodSuffix) {
|
|
||||||
methodName += relationship.args.methodSuffix
|
|
||||||
} else if (serviceConfig?.args?.methodSuffix) {
|
|
||||||
methodName += serviceConfig.args.methodSuffix
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await service[methodName](filters, options)
|
|
||||||
|
|
||||||
if (hasPagination_) {
|
|
||||||
const [data, count] = result
|
|
||||||
return {
|
|
||||||
data: {
|
|
||||||
rows: data,
|
|
||||||
metadata: buildPagination(options, count),
|
|
||||||
},
|
|
||||||
path: "rows",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
data: result,
|
|
||||||
} as any
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -27,7 +27,6 @@ export const CustomerAuth: ModuleJoinerConfig = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
serviceName: Modules.AUTH,
|
serviceName: Modules.AUTH,
|
||||||
isInternalService: true,
|
|
||||||
primaryKey: "id",
|
primaryKey: "id",
|
||||||
foreignKey: "auth_identity_id",
|
foreignKey: "auth_identity_id",
|
||||||
alias: "auth",
|
alias: "auth",
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ export const ProductSalesChannel: ModuleJoinerConfig = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
serviceName: Modules.SALES_CHANNEL,
|
serviceName: Modules.SALES_CHANNEL,
|
||||||
isInternalService: true,
|
|
||||||
primaryKey: "id",
|
primaryKey: "id",
|
||||||
foreignKey: "sales_channel_id",
|
foreignKey: "sales_channel_id",
|
||||||
alias: "sales_channel",
|
alias: "sales_channel",
|
||||||
@@ -51,7 +50,6 @@ export const ProductSalesChannel: ModuleJoinerConfig = {
|
|||||||
serviceName: Modules.SALES_CHANNEL,
|
serviceName: Modules.SALES_CHANNEL,
|
||||||
relationship: {
|
relationship: {
|
||||||
serviceName: LINKS.ProductSalesChannel,
|
serviceName: LINKS.ProductSalesChannel,
|
||||||
isInternalService: true,
|
|
||||||
primaryKey: "sales_channel_id",
|
primaryKey: "sales_channel_id",
|
||||||
foreignKey: "id",
|
foreignKey: "id",
|
||||||
alias: "products_link",
|
alias: "products_link",
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ export const ProductVariantPriceSet: ModuleJoinerConfig = {
|
|||||||
relationships: [
|
relationships: [
|
||||||
{
|
{
|
||||||
serviceName: Modules.PRODUCT,
|
serviceName: Modules.PRODUCT,
|
||||||
// TODO: Remove this when product module is the default product service
|
|
||||||
isInternalService: true,
|
|
||||||
primaryKey: "id",
|
primaryKey: "id",
|
||||||
foreignKey: "variant_id",
|
foreignKey: "variant_id",
|
||||||
alias: "variant",
|
alias: "variant",
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ export const UserAuth: ModuleJoinerConfig = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
serviceName: Modules.AUTH,
|
serviceName: Modules.AUTH,
|
||||||
isInternalService: true,
|
|
||||||
primaryKey: "id",
|
primaryKey: "id",
|
||||||
foreignKey: "auth_identity_id",
|
foreignKey: "auth_identity_id",
|
||||||
alias: "auth",
|
alias: "auth",
|
||||||
|
|||||||
@@ -99,10 +99,8 @@ export const initialize = async (
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else if (
|
} else if (
|
||||||
(!primary.isInternalService &&
|
!modulesLoadedKeys.includes(primary.serviceName) ||
|
||||||
!modulesLoadedKeys.includes(primary.serviceName)) ||
|
!modulesLoadedKeys.includes(foreign.serviceName)
|
||||||
(!foreign.isInternalService &&
|
|
||||||
!modulesLoadedKeys.includes(foreign.serviceName))
|
|
||||||
) {
|
) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,11 +32,7 @@ export function generateGraphQLSchema(
|
|||||||
|
|
||||||
for (const extend of joinerConfig.extends ?? []) {
|
for (const extend of joinerConfig.extends ?? []) {
|
||||||
const extendedModule = MedusaModule.getModuleInstance(extend.serviceName)
|
const extendedModule = MedusaModule.getModuleInstance(extend.serviceName)
|
||||||
if (
|
if (!extendedModule) {
|
||||||
!isReadOnlyLink &&
|
|
||||||
!extendedModule &&
|
|
||||||
!extend.relationship.isInternalService
|
|
||||||
) {
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Module ${extend.serviceName} not found. Please verify that the module is configured and installed, also the module must be loaded before the link modules.`
|
`Module ${extend.serviceName} not found. Please verify that the module is configured and installed, also the module must be loaded before the link modules.`
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user