Declare a node
A node is a record: identified, batched over one service, cached once per entity. Four things are load-bearing, and one row property makes tenant isolation possible.
A node is a record: identified, batched over one service, cached once per entity.
// server/athanor/nodes/vault.ts
import { Effect, Schema } from "effect";
import { VaultService } from "@athanor/custody/vault/service";
import { vaultTags } from "@athanor/custody/vault/tags";
import { Athanor } from "../alembic";
export const VaultSchema = Schema.TaggedStruct("Vault", {
id: Schema.String,
name: Schema.String,
location: Schema.NullOr(Schema.String),
provider: Schema.String,
});
export const VaultNode = Athanor.Node(VaultSchema, {
load: (ids) =>
VaultService.use((vaults) => vaults.byIds(ids)).pipe(
Effect.map((rows) => new Map(rows.map((vault) => [vault.id, vault]))),
),
}).pipe(Athanor.Auth.Public, Athanor.Cache("hours", { tag: vaultTags.byId }));Four things are load-bearing:
- The typename comes from the schema's tag, not the binding name. It is a persisted
identifier — it appears in global ids in URLs and in cache tags across deploys — so renaming
VaultNodemust stay a safe refactor. loadtakes keys only. It structurally cannot see the request, which is what makes a shared cache entry safe: the entry isglobalId → raw entity, viewer-independent.- Every node needs an access decision —
Auth.PublicorAuth.Authorize. It is a type-level requirement; an undecided node cannot be used. Cache(life, { tag })is the whole cache declaration. The tag builder should be the same string your domain already invalidates under, so hand-writtenupdateTagsites and the graph agree during a migration.
Viewer-scoped nodes are cacheable
This surprises people. The entry is the raw entity and the gate runs per read, on top of it:
export const ClientNode = Athanor.Node(ClientSchema, {
load: (ids) => ClientService.use((c) => c.clientsByIds(ids)).pipe(/* … */),
}).pipe(
Athanor.Auth.Authorize((ctx, entity) => {
const actor = ctx.viewer.actor;
return (
actor._tag === "User" &&
String(actor.organizationId) === entity.organizationId
);
}),
Athanor.Cache("minutes", { tag: clientTags.byId }),
);The row must carry whatever the gate decides on. load cannot see the viewer, so
organizationId has to be in the entity — otherwise tenant isolation has nowhere to live. That
is the single most common mistake when adding a scoped node.
A denied read and an absent one are deliberately indistinguishable (node(id) returns null for
both), so an always-available lookup is never an existence oracle.
The builder, then the graph
Two phases, because declarations reference each other in both directions: declare against a builder, build the graph once from the roots.
Objects, fields and the Query root
An object is fields with no identity and derives from its parent. Seven field kinds, field arguments, nullable edges and null propagation.