Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions example/convex/nestedWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { workflow } from "./example";
import { internal } from "./_generated/api";
import { internalMutation } from "./_generated/server";

export const parentWorkflow = workflow.define({
args: { prompt: v.string() },
handler: async (ctx, args) => {
export const parentWorkflow = workflow
.define({
args: { prompt: v.string() },
returns: v.number(),
})
.handler(async (ctx, args) => {
Comment on lines +10 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi @thomasballinger fluent definitions work well and break the circular type errors.
I even made this an overload so you can keep providing the handler inline, or upgrade to fluent. but the overload makes for uglier type errors (the overload 1 / 2 wording, maybe not a big deal).
The other syntax I could imagine doing - which we can do in convex too - is workflow.define.args().returns().handler().
So one version we could use in convex would be:

export const myquery = query({ args, returns }).handler(async () => {});

Another would be:

export const myquery = query.args().returns().handler(async() => {});

The latter could avoid overloads.
or even query.define({ args, returns }).handler(async() => {}); - if we want to have an object to be able to add more things, e.g. the custom function extra args pattern I like.

console.log("Starting confirmation workflow");
const length = await ctx.runWorkflow(
internal.nestedWorkflow.childWorkflow,
Expand All @@ -16,12 +19,11 @@ export const parentWorkflow = workflow.define({
foo: args.prompt,
});
console.log("Step result:", stepResult);
},
});
return stepResult;
});

export const childWorkflow = workflow.define({
args: { foo: v.string() },
returns: v.number(),
handler: async (_ctx, args) => {
console.log("Starting nested workflow");
return args.foo.length;
Expand Down
69 changes: 54 additions & 15 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import {
type RegisteredMutation,
type ReturnValueForOptionalValidator,
} from "convex/server";
import type {
Infer,
ObjectType,
PropertyValidators,
Validator,
} from "convex/values";
import type { ObjectType, PropertyValidators, Validator } from "convex/values";
import type { Step } from "../component/schema.js";
import type {
EventId,
Expand Down Expand Up @@ -118,15 +113,59 @@ export class WorkflowManager {
fn: "You should not call this directly, call workflow.start instead";
args: ObjectType<ArgsValidator>;
},
ReturnsValidator extends Validator<unknown, "required", string>
? Infer<ReturnsValidator>
: void
> {
return workflowMutation(
this.component,
workflow,
this.options?.workpoolOptions,
);
ReturnValueForOptionalValidator<ReturnsValidator>
>;
define<
ArgsValidator extends PropertyValidators,
ReturnsValidator extends Validator<unknown, "required", string> | void,
>(
workflow: Omit<
WorkflowDefinition<ArgsValidator, ReturnsValidator>,
"handler"
>,
): {
handler: (
handler: (
step: WorkflowCtx,
args: ObjectType<ArgsValidator>,
) => Promise<ReturnValueForOptionalValidator<ReturnsValidator>>,
) => RegisteredMutation<
"internal",
{
fn: "You should not call this directly, call workflow.start instead";
args: ObjectType<ArgsValidator>;
},
ReturnValueForOptionalValidator<ReturnsValidator>
>;
};
define<
ArgsValidator extends PropertyValidators,
ReturnsValidator extends Validator<unknown, "required", string> | void,
>(
workflow:
| Omit<WorkflowDefinition<ArgsValidator, ReturnsValidator>, "handler">
| WorkflowDefinition<ArgsValidator, ReturnsValidator>,
): unknown {
if ("handler" in workflow) {
return workflowMutation(
this.component,
workflow,
this.options?.workpoolOptions,
);
}
return {
handler: (
handler: (
step: WorkflowCtx,
args: ObjectType<ArgsValidator>,
) => Promise<ReturnValueForOptionalValidator<ReturnsValidator>>,
) =>
workflowMutation(
this.component,
{ ...workflow, handler },
this.options?.workpoolOptions,
),
};
}

/**
Expand Down