Skip to content

Commit 74b095f

Browse files
committed
allow fluent syntax for defining workflows
1 parent 72cb23c commit 74b095f

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed

example/convex/nestedWorkflow.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { workflow } from "./example";
33
import { internal } from "./_generated/api";
44
import { internalMutation } from "./_generated/server";
55

6-
export const parentWorkflow = workflow.define({
7-
args: { prompt: v.string() },
8-
handler: async (ctx, args) => {
6+
export const parentWorkflow = workflow
7+
.define({
8+
args: { prompt: v.string() },
9+
returns: v.number(),
10+
})
11+
.handler(async (ctx, args) => {
912
console.log("Starting confirmation workflow");
1013
const length = await ctx.runWorkflow(
1114
internal.nestedWorkflow.childWorkflow,
@@ -16,12 +19,11 @@ export const parentWorkflow = workflow.define({
1619
foo: args.prompt,
1720
});
1821
console.log("Step result:", stepResult);
19-
},
20-
});
22+
return stepResult;
23+
});
2124

2225
export const childWorkflow = workflow.define({
2326
args: { foo: v.string() },
24-
returns: v.number(),
2527
handler: async (_ctx, args) => {
2628
console.log("Starting nested workflow");
2729
return args.foo.length;

src/client/index.ts

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ import {
1515
type RegisteredMutation,
1616
type ReturnValueForOptionalValidator,
1717
} from "convex/server";
18-
import type {
19-
Infer,
20-
ObjectType,
21-
PropertyValidators,
22-
Validator,
23-
} from "convex/values";
18+
import type { ObjectType, PropertyValidators, Validator } from "convex/values";
2419
import type { Step } from "../component/schema.js";
2520
import type {
2621
EventId,
@@ -118,15 +113,59 @@ export class WorkflowManager {
118113
fn: "You should not call this directly, call workflow.start instead";
119114
args: ObjectType<ArgsValidator>;
120115
},
121-
ReturnsValidator extends Validator<unknown, "required", string>
122-
? Infer<ReturnsValidator>
123-
: void
124-
> {
125-
return workflowMutation(
126-
this.component,
127-
workflow,
128-
this.options?.workpoolOptions,
129-
);
116+
ReturnValueForOptionalValidator<ReturnsValidator>
117+
>;
118+
define<
119+
ArgsValidator extends PropertyValidators,
120+
ReturnsValidator extends Validator<unknown, "required", string> | void,
121+
>(
122+
workflow: Omit<
123+
WorkflowDefinition<ArgsValidator, ReturnsValidator>,
124+
"handler"
125+
>,
126+
): {
127+
handler: (
128+
handler: (
129+
step: WorkflowCtx,
130+
args: ObjectType<ArgsValidator>,
131+
) => Promise<ReturnValueForOptionalValidator<ReturnsValidator>>,
132+
) => RegisteredMutation<
133+
"internal",
134+
{
135+
fn: "You should not call this directly, call workflow.start instead";
136+
args: ObjectType<ArgsValidator>;
137+
},
138+
ReturnValueForOptionalValidator<ReturnsValidator>
139+
>;
140+
};
141+
define<
142+
ArgsValidator extends PropertyValidators,
143+
ReturnsValidator extends Validator<unknown, "required", string> | void,
144+
>(
145+
workflow:
146+
| Omit<WorkflowDefinition<ArgsValidator, ReturnsValidator>, "handler">
147+
| WorkflowDefinition<ArgsValidator, ReturnsValidator>,
148+
): unknown {
149+
if ("handler" in workflow) {
150+
return workflowMutation(
151+
this.component,
152+
workflow,
153+
this.options?.workpoolOptions,
154+
);
155+
}
156+
return {
157+
handler: (
158+
handler: (
159+
step: WorkflowCtx,
160+
args: ObjectType<ArgsValidator>,
161+
) => Promise<ReturnValueForOptionalValidator<ReturnsValidator>>,
162+
) =>
163+
workflowMutation(
164+
this.component,
165+
{ ...workflow, handler },
166+
this.options?.workpoolOptions,
167+
),
168+
};
130169
}
131170

132171
/**

0 commit comments

Comments
 (0)