Skip to content

Allow Task functions in Graphile Worker to return **any** promise type #545

Open
@1mike12

Description

@1mike12

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]:


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions