query-options-per-query
Where a query is declared, and where its key lives.
The rule
Agents read this text as written when they write and review code.
Declare every query once, as a queryOptions() factory in a file of its own named with a QueryOptions suffix, and give it its key there. A route loader, a screen and a mutation's invalidation all read that one object. None writes a queryKey of its own, and no file collects the keys of several queries.
Leave the factory's return type to inference, as inferred-return-types says.
A key written once is found, invalidated and changed from that one place. A key spelled in two places drifts, and a shared list of keys is a file every feature has to edit.
Examples
The check flags the incorrect code and passes the correct code; the agent judges what the check cannot see. Each example is a small repo, because this rule looks across files. It opens on the file that matters; the other files are the repo around it.
Incorrect
import { useQuery } from '@tanstack/react-query';import { findIssues } from '../server/findIssues.ts';// Incorrect: the screen spells the query and its key itself, so the loader// below has to spell them again, and the two can drift.export const IssuesScreen = () => {const { data: issues = [] } = useQuery({ queryKey: ['issues'], queryFn: () => findIssues() });return (<ul>{issues.map((issue) => (<li key={issue.id}>{issue.title}</li>))}</ul>);};
Correct
// Correct: the query and its key are declared once, and every reader imports this.import { queryOptions } from '@tanstack/react-query';import { findIssues } from '../server/findIssues.ts';export const issuesQueryOptions = () =>queryOptions({queryKey: ['issues'],queryFn: () => findIssues(),});
Configure
- Block
"blocking"Stops a run when a file the run changed breaks the rule. - AdviseDefault
"advisory"Reports it and hands it to the refactor agent. Never stops a run. - Off
"off"Not checked. Use it when your own linter already enforces the rule.
Add this to your lightsout.config.json, then change the value.
{"standards-rule-settings": {"query-options-per-query": "advisory"}}