fix(workflows-sdk): Value resolver should resolve non StepResponse (#6726)

This commit is contained in:
Adrien de Peretti
2024-03-18 21:07:04 +01:00
committed by GitHub
parent 0c705d7bd4
commit 4b06c17dc0
5 changed files with 38 additions and 27 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---
fix(workflows-sdk): Value resolver should resolve non StepResponse

View File

@@ -1,7 +1,7 @@
import {
StepResponse,
createStep,
createWorkflow,
StepResponse,
} from "@medusajs/workflows-sdk"
const step_1 = createStep(
@@ -15,7 +15,6 @@ const step_1 = createStep(
return
}
console.log("reverted", compensateInput.compensate)
return new StepResponse({
reverted: true,
})
@@ -25,8 +24,6 @@ const step_1 = createStep(
const step_2 = createStep(
"step_2",
jest.fn((input, context) => {
console.log("triggered async request", context.metadata.idempotency_key)
if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}

View File

@@ -1,7 +1,7 @@
import {
StepResponse,
createStep,
createWorkflow,
StepResponse,
} from "@medusajs/workflows-sdk"
const step_1 = createStep(
@@ -15,22 +15,20 @@ const step_1 = createStep(
return
}
console.log("reverted", compensateInput.compensate)
return new StepResponse({
reverted: true,
})
})
)
export const workflow2Step2Invoke = jest.fn((input, context) => {
if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
})
const step_2 = createStep(
"step_2",
jest.fn((input, context) => {
console.log("triggered async request", context.metadata.idempotency_key)
if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
}),
workflow2Step2Invoke,
jest.fn((_, context) => {
return new StepResponse({
step: context.metadata.action,
@@ -40,16 +38,14 @@ const step_2 = createStep(
})
)
const step_3 = createStep(
"step_3",
jest.fn((res) => {
return new StepResponse({
done: {
inputFromSyncStep: res.notAsyncResponse,
},
})
export const workflow2Step3Invoke = jest.fn((res) => {
return new StepResponse({
done: {
inputFromSyncStep: res.notAsyncResponse,
},
})
)
})
const step_3 = createStep("step_3", workflow2Step3Invoke)
createWorkflow(
{
@@ -59,13 +55,13 @@ createWorkflow(
function (input) {
step_1(input)
const ret2 = step_2({ hey: "oh" })
step_2({ hey: "oh" })
step_2({ hey: "async hello" }).config({
const ret2_async = step_2({ hey: "async hello" }).config({
name: "new_step_name",
async: true,
})
return step_3(ret2)
return step_3(ret2_async)
}
)

View File

@@ -6,6 +6,7 @@ import { knex } from "knex"
import { setTimeout } from "timers/promises"
import "../__fixtures__"
import { DB_URL, TestDatabase } from "../utils"
import { workflow2Step2Invoke, workflow2Step3Invoke } from "../__fixtures__"
const sharedPgConnection = knex<any, any>({
client: "pg",
@@ -124,6 +125,17 @@ describe("Workflow Orchestrator module", function () {
stepResponse: { uhuuuu: "yeaah!" },
})
expect(workflow2Step2Invoke).toBeCalledTimes(2)
expect(workflow2Step2Invoke.mock.calls[0][0]).toEqual({ hey: "oh" })
expect(workflow2Step2Invoke.mock.calls[1][0]).toEqual({
hey: "async hello",
})
expect(workflow2Step3Invoke).toBeCalledTimes(1)
expect(workflow2Step3Invoke.mock.calls[0][0]).toEqual({
uhuuuu: "yeaah!",
})
executionsList = await query({
workflow_executions: {
fields: ["id"],

View File

@@ -1,4 +1,4 @@
import { OrchestrationUtils, deepCopy, promiseAll } from "@medusajs/utils"
import { deepCopy, OrchestrationUtils, promiseAll } from "@medusajs/utils"
async function resolveProperty(property, transactionContext) {
const { invoke: invokeRes } = transactionContext
@@ -12,7 +12,8 @@ async function resolveProperty(property, transactionContext) {
} else if (property?.__type === OrchestrationUtils.SymbolWorkflowHook) {
return await property.__value(transactionContext)
} else if (property?.__type === OrchestrationUtils.SymbolWorkflowStep) {
const output = invokeRes[property.__step__]?.output
const output =
invokeRes[property.__step__]?.output ?? invokeRes[property.__step__]
if (output?.__type === OrchestrationUtils.SymbolWorkflowStepResponse) {
return output.output
}