ADR-0005: TanStack Query as a ratchet, not a rewrite
- Status: Accepted
- Date: 2026-08-10
- Deciders: Cardsite platform team (Phase 4 of the 2026-08-10 assessment)
Context
AGENTS.md mandates that all frontend server state go through TanStack Query
hooks, but the mandate describes ~9% of the app: 7 of 79 pages use
hooks/queries/, while 72 pages fetch imperatively with useEffect +
services/* and hand-rolled loading/error state (assessment finding 7,
.archives/removal-safe/.ops/projects/001/reports/ASSESSMENT.md). A rule that contradicts the
codebase teaches contributors to ignore rules; a big-bang migration of 72
working pages is churn without user-visible payoff.
Decision
TanStack Query is the target architecture, enforced as a ratchet:
- New pages and new fetches must use hooks in
apps/web/src/hooks/queries/with keys fromlib/queryKeys.ts(anticipatory keys already exist for most resources). No newuseEffect-fetch code. - Mutations anywhere — including in legacy pages — must invalidate the
relevant
queryKeysroot, so cached list views never go stale (established in Phase 2 for the QR builder and biolink editor). - Existing pages migrate opportunistically: when a change touches a page's data flow, move that page's reads onto hooks in the same PR. Pure-UI changes carry no migration obligation.
- The
useEffect+services/*pattern in untouched pages is accepted legacy, not a defect to file.
Consequences
The rule now matches enforcement reality and shrinks the legacy surface monotonically. Cache coherence is guaranteed globally (rule 2) even where reads are still imperative. The cost: two read patterns coexist indefinitely; reviewers must check which rule applies (did the PR touch the page's data flow?).
Alternatives considered
- Big-bang migration of all 72 pages — rejected: high-churn, no user-visible payoff, and the R2 audit showed bulk mechanical passes are where this repo's regressions come from.
- Demote the rule to "either pattern is fine" — rejected: permanently forks the architecture and gives up cache coherence for new work.
Source: docs/decisions/0005-server-state-ratchet.md