Fragments and views
A fragment is a declared field slice whose return type is the mask. A view is a fragment that renders itself; match is the inline fragment per union variant.
A fragment is a declared field-slice over a parent type. Its return type is the mask.
export const ClientRowFragment = Fragment.of<typeof ClientNode>()((client) => ({
id: client.id,
name: client.name,
email: client.email,
}));
// spread onto anything producing that parent
app.viewer.clients(ClientRowFragment); // a collection — masks every row
someClient.pipe(fragment(ClientRowFragment)); // a single parentFragment.of<typeof X>() takes a type-only reference, so a client component can name a
fragment's data without dragging the graph into its bundle:
"use client";
import type { FragmentData } from "@athanor/alembic/fragment";
import type { ClientRowFragment } from "./query";
export function ClientRow({ client }: { client: FragmentData<typeof ClientRowFragment> }) { … }Two rules that save real debugging:
- Fragments are Effects. Rendering is
Effect.map, so a fragment can return a component:detail: Effect.map(o.symbol, (s) => <Chip symbol={s} />). - A fragment for the wrong parent doesn't compile. It leaves an undischarged
Parent<E>in the requirement channel — Effect reports it, so there is no bespoke guard.
Views: a fragment that renders itself
Relay's colocation, without a client store. Declare the fragment beside the component that reads it, as one value:
// components/avatar.tsx
export const Avatar = view(
Fragment.of<typeof UserActor>()((u) => ({ name: u.name, image: u.image })),
({
data,
size = 32,
}: {
data: { name: string; image: string | null };
size?: number;
}) => <img src={data.image ?? ""} alt={data.name} width={size} />,
);A view takes every position a fragment can — as a leaf under its parent, piped onto a source, spread over a collection — but where a fragment resolves to data, a view resolves to a component with that data bound:
const page = Athanor.execute(AccountQuery, {}); // no await
<Suspense>
<page.Avatar size={48} />
</Suspense>; // suspends on its own leaf, and only its ownViews compose. A parent view's fragment spreads child views, and its component receives them bound — fragment composition, with the "ref" being a component:
export const Panel = view(
Fragment.of<typeof Viewer>()((v) => ({
name: v.name,
Avatar: v.actor.pipe(Avatar),
})),
({ data }) => (
<section>
<data.Avatar size={48} />
<h2>{data.name}</h2>
</section>
),
);Over a collection, a view is one component rendering the keyed list — app.viewer.team(Avatar)
→ <page.Team />, keyed by each row's id. Over a connection, put the view on the connection
(that is where pageInfo and "load more" live) and the row view on e.node(…).
One boundary: a bound view is a function, so a server component can render it but it cannot be passed to a client component as a prop — hand the element across instead. A view whose component is a client component must therefore select data only.
Unions: match
An inline fragment per variant — ... on UserActor { ...UserChip } — exhaustive by construction,
each branch masked to its own slice:
export const ActorChip = match({
UserActor: UserChip, // views → the result is a view
ServiceActor: ServiceChip,
SystemActor: SystemChip,
AnonymousActor: SignInPrompt,
});
query((app) => ({ Actor: app.viewer.actor.pipe(ActorChip) })); // <page.Actor />With fragments on the branches instead of views, the result is the tagged union of their data, so a
downstream Match.tag still has the discriminant. Plain Effect.map(app.viewer.actor, Match…)
also works — the entity carries _tag — but hands each branch the whole entity, unmasked.
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.
The entity cache
One use cache function for the whole graph, keyed by typename and id, with a bucketed clock so a cached read is deterministic.