noctcore-query/trpc-invalidate-with-path-key
noctcore-query/trpc-invalidate-with-path-key
- Package
- query v0.2.0
- Preset
- recommended
- Severity
- error
- Autofix
--fix- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
Invalidate a tRPC procedure with pathKey(), not an input-less queryKey(), so infinite queries are reached too.
With tRPC’s TanStack Query integration, trpc.post.list.queryKey() without an input is
[path, { type: 'query' }]. An infinite query of the same procedure is keyed
[path, { input, type: 'infinite' }], and TanStack’s partial matching compares the type field
too, so this invalidation reaches every plain list query and no infinite one:
await queryClient.invalidateQueries({ queryKey: trpc.post.list.queryKey() });It works today, while the list is a plain useQuery. The day the list moves to
useInfiniteQuery, the call goes dead: the mutation succeeds, the row on screen never changes,
and unit tests that mock the client keep passing. pathKey() is [path], which matches every
entry of the procedure, plain and infinite:
await queryClient.invalidateQueries({ queryKey: trpc.post.list.pathKey() });For a plain query the two keys match the same entries, so the swap changes nothing until it matters.
What it flags
Section titled “What it flags”An input-less <root>...queryKey() as the queryKey of a filter, or an input-less
<root>...queryFilter() as the whole filter, passed to invalidateQueries, refetchQueries,
cancelQueries, removeQueries or resetQueries on any receiver (an imported sharedQueryClient
counts):
await Promise.all([ queryClient.invalidateQueries({ queryKey: trpc.invoice.list.queryKey() }), queryClient.refetchQueries(trpc.invoice.counts.queryFilter()),]);await Promise.all([ queryClient.invalidateQueries({ queryKey: trpc.invoice.list.pathKey() }), queryClient.refetchQueries(trpc.invoice.counts.pathFilter()),]);Autofix
Section titled “Autofix”--fix renames queryKey to pathKey and queryFilter to pathFilter. Nothing else in the
call changes.
What it does not flag
Section titled “What it does not flag”- A key with an input,
queryKey({ id }),queryKey({})included: it addresses exact entries on purpose. - A filter that also sets
exact:exact: trueagainst[path]would match nothing, so the swap would not be equivalent. setQueryDataandgetQueryData, which address one entry.- A key built in a variable first, or a proxy reached through
this.or a call: the check is lexical.
queryClient.setQueryData(trpc.post.list.queryKey(), next);await queryClient.invalidateQueries({ queryKey: trpc.post.byId.queryKey({ id }) });await queryClient.invalidateQueries({ queryKey: trpc.post.list.queryKey(), exact: true });Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
methods |
string[] |
['invalidateQueries', 'refetchQueries', 'cancelQueries', 'removeQueries', 'resetQueries'] |
QueryClient methods whose filter is checked. |
rootIdentifiers |
string[] |
['trpc'] |
Identifiers that hold the tRPC options proxy. |
'noctcore-query/trpc-invalidate-with-path-key': ['error', { rootIdentifiers: ['trpc', 'api'] }]When not to use it
Section titled “When not to use it”If you use tRPC’s classic React integration (utils.post.list.invalidate()), there is no
queryKey() call to check and the rule stays silent. Leave it on anyway if a migration to the
TanStack integration is planned.