lightsoutAlpha

query-options-per-query

Where a query is declared, and where its key lives.

Deterministic checkAgent checkAdvises by default

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

srcfeaturesissuesscreensservertypesroutes
src/features/issues/screens/IssuesScreen.tsx
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

srcfeaturesissuesqueriesscreensservertypesroutes
src/features/issues/queries/issuesQueryOptions.ts
// 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.
  • Advise"advisory"Reports it and hands it to the refactor agent. Never stops a run.
    Default
  • 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.

lightsout.config.json
{
"standards-rule-settings": {
"query-options-per-query": "advisory"
}
}

Packs that select it