Description
Feature description
I noticed that the Task type has a generic already, so Im not 100% sure it's not user error and I'm not typing things correctly. But I want to allow Task
functions in Graphile Worker to return any promise type, not just PromiseOrDirect<void | unknown[]>
. Right now the Task
signature is:
export type PromiseOrDirect<T> = Promise<T> | T;
export type Task<TName extends keyof GraphileWorker.Tasks | (string & {}) = string & {}> =
(
payload: TName extends keyof GraphileWorker.Tasks ? GraphileWorker.Tasks[TName] : unknown,
helpers: JobHelpers
) => PromiseOrDirect<void | PromiseOrDirect<unknown>[]>;
which rejects any return value other than void
or unknown[]
. Since the library treats a resolved promise as success regardless of its return value, the typings should allow arbitrary promise return types (e.g. Promise<number>
).
Motivating example
I have a task function that processes a post and patches my database, then naturally returns Objection’s .patch()
result (a number
):
export async function processItem(
{ itemId }: Payload,
helpers: JobHelpers
): Promise<void> {
const item = await Item.query().findById(itemId);
if (item.status === "pending") {
await item.$query().patch({ status: "skipped" });
return;
}
if (item.status === "archived") {
await item.$query().patch({ status: "ignored" });
return;
}
...10 more checks
}
This yields:
TS2418: Type of computed property's value is '({postId}: MyPayload, helpers?: JobHelpers | undefined) => Promise<number | undefined>'
which is not assignable to type 'Task<any>'
Type 'Promise<number | undefined>' is not assignable to type 'PromiseOrDirect<void | unknown[]>'
Yet Graphile Worker itself ignores the resolved value and only cares about thrown errors. Allowing Promise<T>
would remove friction and better reflect how users actually use async tasks.
Breaking changes
None for runtime behavior. Typing changes will be backward-compatible for existing tasks that return void
or unknown[]
, but consumers who currently rely on the narrower return type might need to adjust. Bumping to a new minor or major version may be appropriate.
Supporting development
I [tick all that apply]:
- am interested in building this feature myself
- am interested in collaborating on building this feature
- am willing to help testing this feature before it's released
- am willing to write a test-driven test suite for this feature (before it exists)
- am a [Graphile sponsor](https://www.graphile.org/sponsor/) ❤️
- have an active [support or consultancy contract](https://www.graphile.org/support/) with Graphile