fix(workflows-sdk): use loaded modules when container is empty (#7422)

This commit is contained in:
Carlos R. L. Rodrigues
2024-05-23 08:26:16 -03:00
committed by GitHub
parent 05077bab32
commit f3d19f5394
13 changed files with 15 additions and 154 deletions

View File

@@ -244,18 +244,6 @@ export class RemoteJoiner {
{ fieldAlias, relationships },
] of expandedRelationships) {
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`)
}

View File

@@ -3,11 +3,6 @@ export type JoinerRelationship = {
foreignKey: string
primaryKey: 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
*/

View File

@@ -220,10 +220,6 @@ export type ModuleJoinerConfig = Omit<
}
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
*/

View File

@@ -1,12 +1,11 @@
import { MedusaModule } from "@medusajs/modules-sdk"
import {
LocalWorkflow,
TransactionHandlerType,
TransactionState,
} from "@medusajs/orchestration"
import { LoadedModule, MedusaContainer } from "@medusajs/types"
import { MedusaModule } from "@medusajs/modules-sdk"
import { MedusaContextType } from "@medusajs/utils"
import { MedusaContextType, isPresent } from "@medusajs/utils"
import { EOL } from "os"
import { ulid } from "ulid"
import { MedusaWorkflow } from "../medusa-workflow"
@@ -62,13 +61,16 @@ function createContextualWorkflowRunner<
},
...args
) => {
if (!executionContainer && !flow.container) {
executionContainer = MedusaModule.getLoadedModules().map(
(mod) => Object.values(mod)[0]
)
if (!executionContainer) {
const container_ = flow.container as MedusaContainer
if (!container_ || !isPresent(container_?.registrations)) {
executionContainer = MedusaModule.getLoadedModules().map(
(mod) => Object.values(mod)[0]
)
}
}
if (!flow.container) {
if (executionContainer) {
flow.container = executionContainer
}

View File

@@ -20,7 +20,6 @@ import {
} from "@medusajs/utils"
import { asValue } from "awilix"
import { remoteQueryFetchData } from "../utils/remote-query-fetch-data"
export function mergeDefaultModules(
modulesConfig: CommonTypes.ConfigModule["modules"]
@@ -93,7 +92,6 @@ async function runMedusaAppMigrations({
if (revert) {
await MedusaAppMigrateDown({
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,
@@ -101,7 +99,6 @@ async function runMedusaAppMigrations({
} else {
await MedusaAppMigrateUp({
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,
@@ -179,7 +176,6 @@ export const loadMedusaApp = async (
const medusaApp = await MedusaApp({
workerMode: configModule.projectConfig.worker_mode,
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,
@@ -254,7 +250,6 @@ export async function runModulesLoader({
await MedusaApp({
modulesConfig: configModules,
remoteFetchData: remoteQueryFetchData(container),
sharedContainer: container,
sharedResourcesConfig,
injectedDependencies,

View File

@@ -1,6 +1,5 @@
export * from "./clean-response-data"
export * from "./exception-formatter"
export * from "./omit-deep"
export * from "./remote-query-fetch-data"
export * from "./remove-undefined-properties"
export * from "./middlewares"
export * from "./omit-deep"
export * from "./remove-undefined-properties"

View File

@@ -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
}
}

View File

@@ -27,7 +27,6 @@ export const CustomerAuth: ModuleJoinerConfig = {
},
{
serviceName: Modules.AUTH,
isInternalService: true,
primaryKey: "id",
foreignKey: "auth_identity_id",
alias: "auth",

View File

@@ -27,7 +27,6 @@ export const ProductSalesChannel: ModuleJoinerConfig = {
},
{
serviceName: Modules.SALES_CHANNEL,
isInternalService: true,
primaryKey: "id",
foreignKey: "sales_channel_id",
alias: "sales_channel",
@@ -51,7 +50,6 @@ export const ProductSalesChannel: ModuleJoinerConfig = {
serviceName: Modules.SALES_CHANNEL,
relationship: {
serviceName: LINKS.ProductSalesChannel,
isInternalService: true,
primaryKey: "sales_channel_id",
foreignKey: "id",
alias: "products_link",

View File

@@ -21,8 +21,6 @@ export const ProductVariantPriceSet: ModuleJoinerConfig = {
relationships: [
{
serviceName: Modules.PRODUCT,
// TODO: Remove this when product module is the default product service
isInternalService: true,
primaryKey: "id",
foreignKey: "variant_id",
alias: "variant",

View File

@@ -27,7 +27,6 @@ export const UserAuth: ModuleJoinerConfig = {
},
{
serviceName: Modules.AUTH,
isInternalService: true,
primaryKey: "id",
foreignKey: "auth_identity_id",
alias: "auth",

View File

@@ -99,10 +99,8 @@ export const initialize = async (
continue
}
} else if (
(!primary.isInternalService &&
!modulesLoadedKeys.includes(primary.serviceName)) ||
(!foreign.isInternalService &&
!modulesLoadedKeys.includes(foreign.serviceName))
!modulesLoadedKeys.includes(primary.serviceName) ||
!modulesLoadedKeys.includes(foreign.serviceName)
) {
continue
}

View File

@@ -32,11 +32,7 @@ export function generateGraphQLSchema(
for (const extend of joinerConfig.extends ?? []) {
const extendedModule = MedusaModule.getModuleInstance(extend.serviceName)
if (
!isReadOnlyLink &&
!extendedModule &&
!extend.relationship.isInternalService
) {
if (!extendedModule) {
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.`
)