createWorkflow - Workflows API Reference
This documentation provides a reference to the createWorkflow
. It belongs to the @medusajs/workflows-sdk
package.
This function creates a workflow with the provided name and a constructor function.
The constructor function builds the workflow from steps created by the createStep function.
The returned workflow is an exported workflow of type ReturnWorkflow, meaning it's not executed right away. To execute it,
invoke the exported workflow, then run its run
method.
Example
import { createWorkflow } from "@medusajs/workflows-sdk"
import { MedusaRequest, MedusaResponse, Product } from "@medusajs/medusa"
import {
createProductStep,
getProductStep,
createPricesStep
} from "./steps"
interface WorkflowInput {
title: string
}
const myWorkflow = createWorkflow<
WorkflowInput,
Product
>("my-workflow", (input) => {
// Everything here will be executed and resolved later
// during the execution. Including the data access.
const product = createProductStep(input)
const prices = createPricesStep(product)
return getProductStep(product.id)
}
)
export async function GET(
req: MedusaRequest,
res: MedusaResponse
) {
const { result: product } = await myWorkflow(req.scope)
.run({
input: {
title: "Shirt"
}
})
res.json({
product
})
}
Type Parameters
TData
objectRequiredThe type of the input passed to the composer function.
TResult
objectRequiredThe type of the output returned by the composer function.
THooks
Record<string, Function>RequiredThe type of hooks defined in the workflow.
Parameters
nameOrConfig
string | object & TransactionModelOptionsRequiredThe name of the workflow or its configuration.
composer
(input: WorkflowData<TData>) => void | WorkflowData<TResult> | { [K in string | number | symbol]: WorkflowDataProperties<TResult[K]> | WorkflowData<TResult[K]> }RequiredThe constructor function that is executed when the
run
method in ReturnWorkflow is used.
The function can't be an arrow function or an asynchronus function. It also can't directly manipulate data.
You'll have to use the transform function if you need to directly manipulate data.Returns
The created workflow. You can later execute the workflow by invoking it, then using its
run
method.Was this section helpful?