Skip to content

Introduce self-describing hooks#91

Merged
ruudk merged 1 commit into
mainfrom
ruudk/new-hooks
Jun 9, 2026
Merged

Introduce self-describing hooks#91
ruudk merged 1 commit into
mainfrom
ruudk/new-hooks

Conversation

@ruudk

@ruudk ruudk commented Jun 9, 2026

Copy link
Copy Markdown
Owner

@hook(input: ["creator.id"]) forced every call site to know and select the exact fields a hook reads internally. Updating a hook's needs meant touching every query that used it — the call site was coupled to the hook's implementation.

A hook now declares the data it needs as a named GraphQL fragment via #[Hook(name: ..., requires: ...)]. The call site shrinks to a bare @hook(name: ...). The generator builds a typed data class from the requires fragment (emitted into Generated/Hook/), validates that the @hook field sits on a type the fragment's on Type condition allows — an object type, or an interface, in which case any implementer — and injects the required selection into the operation sent to the server. The hook receives the typed object.

Fields a hook needs but the caller did not select are merged into the raw payload only, never surfacing as properties on the caller's generated classes, so the typed API stays free of hook internals. Batched hooks (batched: true) carry over: the HookLoader collects the typed data objects instead of scalar tuples.

This is a breaking change — the @hook(input:) argument is removed; DirectiveProcessor raises a migration error if it is still used. Hooks must move their field list into a requires fragment.

`@hook(input: ["creator.id"])` forced every call site to know and
select the exact fields a hook reads internally. Updating a hook's
needs meant touching every query that used it — the call site was
coupled to the hook's implementation.

A hook now declares the data it needs as a named GraphQL fragment via
`#[Hook(name: ..., requires: ...)]`. The call site shrinks to a bare
`@hook(name: ...)`. The generator builds a typed data class from the
`requires` fragment (emitted into `Generated/Hook/`), validates that
the `@hook` field sits on a type the fragment's `on Type` condition
allows — an object type, or an interface, in which case any
implementer — and injects the required selection into the operation
sent to the server. The hook receives the typed object.

Fields a hook needs but the caller did not select are merged into the
raw payload only, never surfacing as properties on the caller's
generated classes, so the typed API stays free of hook internals.
Batched hooks (`batched: true`) carry over: the `HookLoader` collects
the typed data objects instead of scalar tuples.

This is a breaking change — the `@hook(input:)` argument is removed;
`DirectiveProcessor` raises a migration error if it is still used.
Hooks must move their field list into a `requires` fragment.
@ruudk ruudk merged commit 010dd81 into main Jun 9, 2026
3 checks passed
@ruudk ruudk deleted the ruudk/new-hooks branch June 9, 2026 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant