Skip to content

Graph

Graph: object

Defined in: Core/Graph.ts:25

Runs asynchronous tasks after their dependencies succeed.

Shared dependencies run once per execution. Independent nodes can run concurrently, and failures signal active tasks to stop.

make: <Id, E, Out, Input>(target, options?) => Graph<E, Out, Input> = makeGraph

Builds a reusable execution plan for a target node and its dependencies.

Checks for duplicate node IDs and cycles when called. Invalid graphs throw an error.

Id extends string

E

Out

Input

WithKind<"GraphNode"> & object & (unknown extends Input ? never : unknown)

MakeOptions

Graph<E, Out, Input>

const dashboardGraph = Graph.make(summaryNode);

node: {<Id, Input, E, Out>(id, run): Node<Id, E, Out, unknown extends Input ? void : Input>; <Id, Deps, Input, E, Out>(id, deps, run): Node<Id, E | NodeErrorOf<Deps[keyof Deps]>, Out, NodeInput<Deps, Input>>; } = makeNode

<Id, Input, E, Out>(id, run): Node<Id, E, Out, unknown extends Input ? void : Input>

Creates an individual node descriptor for inclusion in a DAG.

Dependencies are declared as an object dictionary of upstream nodes. The run callback receives the per-run input in ctx and dependency outputs in deps.

Id extends string

Input = unknown

E = never

Out = never

Id

NodeRun<undefined, Input, E, Out>

Node<Id, E, Out, unknown extends Input ? void : Input>

  • Graph.make to compile declared nodes into a validated execution graph.
  • Graph.run for executing compiled graphs.
const user = Graph.node("user", () => fetchUser("u_1"));

const permissions = Graph.node("permissions", { user }, ({ deps }) =>
  fetchPermissions(deps.user.roleId));

<Id, Deps, Input, E, Out>(id, deps, run): Node<Id, E | NodeErrorOf<Deps[keyof Deps]>, Out, NodeInput<Deps, Input>>

Creates an individual node descriptor for inclusion in a DAG.

Dependencies are declared as an object dictionary of upstream nodes. The run callback receives the per-run input in ctx and dependency outputs in deps.

Id extends string

Deps extends Record<string, AnyNode<never>>

Input = unknown

E = never

Out = never

Id

Deps

NodeRun<Deps, Input, E, Out>

Node<Id, E | NodeErrorOf<Deps[keyof Deps]>, Out, NodeInput<Deps, Input>>

  • Graph.make to compile declared nodes into a validated execution graph.
  • Graph.run for executing compiled graphs.
const user = Graph.node("user", () => fetchUser("u_1"));

const permissions = Graph.node("permissions", { user }, ({ deps }) =>
  fetchPermissions(deps.user.roleId));

nodeFactory: <Input>() => NodeFactory<Input>

Creates a node constructor with a shared input type.

Input

NodeFactory<Input>

const node = Graph.nodeFactory<ReportInput>();
const customer = node("customer", ({ ctx }) => findCustomer(ctx.input.id));

run: {(): <E, Out>(graph) => Deferred<Result<Aborted | E, Out>>; <Input>(input, options?): <E, Out>(graph) => Deferred<Result<Aborted | E, Out>>; } = runGraph

(): <E, Out>(graph) => Deferred<Result<Aborted | E, Out>>

Starts a compiled graph and returns a deferred result.

Runs independent nodes concurrently, subject to the configured limit, and runs each shared dependency once per graph execution. If a node returns an error, the graph settles with that error and signals active tasks to abort. Tasks must respond to the signal to stop their work. A caller’s abort signal settles the graph with Aborted even if active tasks ignore it.

<E, Out>(graph) => Deferred<Result<Aborted | E, Out>>

  • Graph.make to compile a target node into a validated graph.
  • Graph.node for defining upstream DAG nodes.

A graph with no input is run as Graph.run()(graph). Pass undefined as the input when such a graph also needs run options: Graph.run(undefined, { signal })(graph).

const outcome = await Graph.run(reportInput)(dashboardGraph);

<Input>(input, options?): <E, Out>(graph) => Deferred<Result<Aborted | E, Out>>

Starts a compiled graph and returns a deferred result.

Runs independent nodes concurrently, subject to the configured limit, and runs each shared dependency once per graph execution. If a node returns an error, the graph settles with that error and signals active tasks to abort. Tasks must respond to the signal to stop their work. A caller’s abort signal settles the graph with Aborted even if active tasks ignore it.

Input

Input

RunOptions

<E, Out>(graph) => Deferred<Result<Aborted | E, Out>>

  • Graph.make to compile a target node into a validated graph.
  • Graph.node for defining upstream DAG nodes.

A graph with no input is run as Graph.run()(graph). Pass undefined as the input when such a graph also needs run options: Graph.run(undefined, { signal })(graph).

const outcome = await Graph.run(reportInput)(dashboardGraph);