Skip to Content
Athanor

Mutations

An ordinary Effect function with a declared input and a payload, registered under its name, recording the writes it made so the cache tags derive from the graph.

An ordinary Effect function with a declared input and a payload.

// server/athanor/mutations/client.ts
export const updateClient = mutation({
  input: UpdateClientInput, // a Schema — decoded before `resolve` sees it
  resolve: (input) =>
    ClientService.use((clients) => clients.updateClient(input)).pipe(
      Effect.tap((client) => updated(ClientNode, client)), // ← the tag derives from here
      Effect.map((client) => ({
        status: "success" as const,
        clientId: client.id,
      })),
    ),
});
  • Its name is the key it is registered under in Mutation({ updateClient, … }) — the field name is the mutation name, as in GraphQL, so there is nothing to spell twice and it is what an adapter dispatches on.
  • The input is a schema, and it decodes at the boundary. A mutation lowered to a Server Action receives its argument from the client; a TypeScript interface enforces nothing at runtime.
  • A write names its node. updated(ClientNode, row) — never a tag string. A misspelled typename would resolve to nothing and emit no tag, silently, leaving a stale entry live.
  • created / deleted also bust collection tags. Looking at an entity tells you it changed; nothing in it tells you a list did. That one bit cannot be inferred.
  • mask(entity, fragment) records the write and returns the masked value in one call, for when you hand the changed entity back.
  • A mutation runs under a scope of its own, so it can read the graph — readNode(Node, id) after a write, gate and all, is the honest way to get the row a write names.
  • The policy lives in the write. "Only an owner or admin" is checked in resolve from Policy.Viewer; the form action that calls it only parses the form.

Making one a Server Action

"use server" is lexical, so it lives in a file you own — which also makes your endpoint surface something you can read:

// app/clients/_actions/action.ts
"use server";
import { action } from "@/server/athanor/action";
import { updateClient as updateClientMutation } from "@/server/athanor/mutations/client";

export const updateClient = action(updateClientMutation);

action() runs Athanor.mutate and applies updateTag — read-your-own-writes, which Next allows only inside a Server Action (E872), and which re-renders the current route in the same response, so no router.refresh() is needed. refresh() is the fallback when nothing entity-cached changed. For a cron, webhook or route handler use revalidate() instead, which applies revalidateTag(tag, "max") and runs anywhere.

On this page