Skip to content

About

Typed query-key hierarchy and invalidation helpers for TanStack Query v5, built on queryOptions(). A maintained successor to @lukemorales/query-key-factory.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

8 Commits

Folders and files

Repository files navigation

query-key-kit: typed query keys and invalidation helpers for TanStack Query v5

query-key-kit: typed query keys for TanStack Query v5

A typed, framework-agnostic query-key factory for TanStack Query v5, tested at run time with query-core's QueryClient and in a React Query app, and type-tested with React Query and Vue Query. Organise your query keys in scopes, get real queryOptions() objects back with DataTag-branded keys, and invalidate or update whole branches of the cache with typed helpers. It keeps the API of @lukemorales/query-key-factory: for most code, switching is an import change, and the migration notes list what behaves differently.

npm License

import { createQueryKeys, invalidateScope } from '@amjadiqbal/query-key-kit';

export const todos = createQueryKeys('todos', {
  all: null,
  detail: (id: number) => ({
    queryKey: [id],
    queryFn: () => api.getTodo(id),
    staleTime: 30_000,
    contextQueries: {
      comments: { queryKey: null, queryFn: () => api.getComments(id) },
    },
  }),
  list: (filters: TodoFilters) => ({
    queryKey: [{ filters }],
    queryFn: () => api.getTodos(filters),
  }),
});

useQuery(todos.detail(1));                          // key ['todos', 'detail', 1], data: Todo | undefined
useQuery(todos.detail(1)._ctx.comments);            // key ['todos', 'detail', 1, 'comments']
queryClient.getQueryData(todos.detail(1).queryKey); // Todo | undefined, no cast

invalidateScope(queryClient, todos.list);      // every todos.list(...) query
invalidateScope(queryClient, todos.detail(1)); // detail 1 and its comments
invalidateScope(queryClient, todos);           // everything under 'todos'

Why

TanStack Query v5's queryOptions() already keeps a key and its fetcher together. What it does not give you is a key hierarchy: one place that says which keys exist, how they nest, and which partial key to pass when you invalidate. Teams write that by hand, or use @lukemorales/query-key-factory, which gets about 1.4 million downloads a month but has had no release since February 2024 and still has open v5 typing bugs.

query-key-kit is a maintained, v5-first take on the same idea:

  • Returns real query options. Every entry produces what queryOptions() produces: a DataTag-branded queryKey (so getQueryData, setQueryData and fetchQuery are typed) and a queryFn typed against the full key.
  • Any query option on an entry. staleTime, gcTime, placeholderData, initialData, select, meta, enabled, initialPageParam, getNextPageParam... are passed through unchanged.
  • Infinite queries. pageParam is available in queryFn, and an entry with initialPageParam works with useInfiniteQuery and fetchInfiniteQuery.
  • Typed scope helpers. invalidateScope, refetchScope, cancelScope, removeScope, getScopeData and setScopeData take any query node of the tree and pick the right partial key. getScopeData and setScopeData know the data type of every query they match.
  • Mutations. createMutationKeys keeps mutationFn's own type, so useMutation infers variables and result.
  • Framework-agnostic. It depends only on @tanstack/query-core types, so the same keys work with any adapter. Tested with @tanstack/react-query and @tanstack/vue-query; Solid, Svelte and Angular Query share those core types but have no tests of their own yet.
  • Small. No runtime dependencies, ESM only, "sideEffects": false. The 0.1.3 build is one 6.7 kB JavaScript file (2.1 kB gzipped).

Install

npm install @amjadiqbal/query-key-kit

You also need TanStack Query v5 for your framework (for example @tanstack/react-query), which brings @tanstack/query-core. TypeScript 5.0 or newer: the built .d.ts is checked with TypeScript 5.0.4 against @tanstack/query-core 5.0.0 (newer query-core releases need TypeScript 5.4 or later for their own types), and the library is built and tested with TypeScript 7.0.

Declaring keys

createQueryKeys(scope, schema) takes a scope name and an object of entries. Each entry is one of:

Entry Example Produces
null all: null { queryKey: ['todos', 'all'] }
key tuple count: ['total'] { _def: ['todos', 'count'], queryKey: ['todos', 'count', 'total'] }
options object stats: { queryKey: null, queryFn, staleTime } query options with key ['todos', 'stats']
function detail: (id: number) => ({ queryKey: [id], queryFn }) a function returning query options, with _def: ['todos', 'detail'] on the function

An options object (returned directly or from a function) may contain:

  • queryKey: extra key parts appended to the entry's path, or null for none.
  • queryFn: the fetcher. It receives the full key in ctx.queryKey, plus ctx.pageParam for infinite queries. skipToken works.
  • contextQueries: a nested schema whose keys extend this entry's key. They appear under _ctx.
  • Any other query option, passed through as is.

Names starting with _ are reserved for _def and _ctx, and are rejected at compile time and at run time.

A single store, or scopes per feature

// One file
export const queries = createQueryKeyStore({
  users: null,
  todos: {
    detail: (id: number) => ({ queryKey: [id], queryFn: () => api.getTodo(id) }),
  },
});

// Or one scope per feature, merged
export const queries = mergeQueryKeys(users, todos, todoMutations);
queries.todos.detail(1);

mergeQueryKeys combines scopes that share a name, typically a query scope and a mutation scope. If two of them define the same entry name, it throws rather than letting one replace the other.

Infinite queries

Give the entry initialPageParam and getNextPageParam. queryFn receives pageParam:

export const todos = createQueryKeys('todos', {
  feed: (filters: TodoFilters) => ({
    queryKey: [filters],
    queryFn: ({ pageParam }: { pageParam: number }) => api.getPage(filters, pageParam),
    initialPageParam: 0,
    getNextPageParam: (last: Page) => last.nextCursor,
  }),
});

useInfiniteQuery(todos.feed({ status: 'open' })); // data: InfiniteData<Page> | undefined

Callbacks inside an entry (getNextPageParam, select, placeholderData as a function) need their parameter annotated, because the entry is not tied to one query yet. Or add them at the call site, below.

Overriding options at the call site

Entries are plain objects, so spread them, directly or through the official helpers:

useQuery({ ...todos.detail(id), enabled: !!id, select: (todo) => todo.title });

const titleQuery = queryOptions({ ...todos.detail(id), select: (todo) => todo.title });
const feed = infiniteQueryOptions({ ...todos.feed(filters), maxPages: 3 });

Don't wrap the entry itself in queryOptions() inside createQueryKeys: those options are typed for the inner key, and TanStack's hooks then reject the prefixed key.

Invalidating and updating the cache

Every node of the tree knows its partial key. Pass the node, not a hand-written array:

import { cancelScope, getScopeData, invalidateScope, setScopeData } from '@amjadiqbal/query-key-kit';

await invalidateScope(queryClient, todos);                  // ['todos']
await invalidateScope(queryClient, todos.list);             // ['todos', 'list'], every filter
await invalidateScope(queryClient, todos.detail(1));        // ['todos', 'detail', 1] and its _ctx
await invalidateScope(queryClient, todos.detail(1), { exact: true }); // only that query

// Optimistic update of every cached list at once. `old` is Todo[] | undefined.
await cancelScope(queryClient, todos.list);
setScopeData(queryClient, todos.list, (old) => old?.map((t) => (t.id === id ? { ...t, done: true } : t)));

getScopeData(queryClient, todos.list); // Array<[QueryKey, Todo[] | undefined]>
Helper Calls
invalidateScope(client, node, filters?, options?) client.invalidateQueries
refetchScope(client, node, filters?, options?) client.refetchQueries
cancelScope(client, node, filters?, options?) client.cancelQueries
removeScope(client, node, filters?) client.removeQueries
getScopeData(client, node, filters?) client.getQueriesData, typed
setScopeData(client, node, updater, filters?) client.setQueriesData, typed
scopeKey(node) returns the partial key

The partial key is queryKey for an entry or a called entry, and _def for a scope or a function entry. filters takes every query filter except queryKey (exact, type, stale, predicate, ...). client is typed as query-core's QueryClient (only the method each helper calls), so adapter clients that extend it are accepted; run-tested with query-core's and React Query's, type-tested with Vue Query's. Mutation entries, function mutation entries and scopes created with createMutationKeys are rejected at compile time and throw at run time (scopeKey and every helper), because a mutation key used as a query filter would match unrelated queries. The data type of a node is the union of every query its key matches, including context queries; it is unknown when a matched entry has no queryFn.

You can still use the keys directly: queryClient.invalidateQueries({ queryKey: todos.detail._def }).

Mutations

export const todoMutations = createMutationKeys('todos', {
  create: { mutationKey: null, mutationFn: (input: NewTodo) => api.createTodo(input) },
  toggle: (id: number) => ({ mutationKey: [id], mutationFn: (done: boolean) => api.setDone(id, done) }),
});

const create = useMutation(todoMutations.create);
create.mutate({ title: 'Ship it' }); // variables typed from mutationFn

const toggle = useMutation({
  ...todoMutations.toggle(1),
  onSuccess: () => invalidateScope(queryClient, todos.detail(1)),
});

Types

Type Gives
inferQueryKeys<typeof todos> the key tree (queryKey, _def, _ctx) without functions or options
inferQueryKeyStore<typeof queries> the same for a whole store
inferQueryData<typeof todos.detail> the data of an entry (Todo)
inferScopeData<typeof todos.detail> the data of every query under a node
TypedUseQueryOptions<typeof todos.detail> observer options for custom hooks
type Keys = inferQueryKeyStore<typeof queries>;

const fetchList = async (ctx: QueryFunctionContext<Keys['todos']['list']['queryKey']>) => {
  const [, , { filters }] = ctx.queryKey; // typed
  return api.getTodos(filters);
};

Migrating from @lukemorales/query-key-factory

  1. Install and swap the import:

    npm uninstall @lukemorales/query-key-factory && npm install @amjadiqbal/query-key-kit
    grep -rl "@lukemorales/query-key-factory" src | xargs sed -i '' 's#@lukemorales/query-key-factory#@amjadiqbal/query-key-kit#g'

    (On Linux, use sed -i without the ''.)

  2. Run your type checker. createQueryKeys, createQueryKeyStore, mergeQueryKeys, createMutationKeys, inferQueryKeys, inferQueryKeyStore and TypedUseQueryOptions keep their names, and the keys they produce (queryKey, _def, _ctx) are the same. Then check the differences below, especially the first one: it changes runtime behaviour.

What changes:

  • Options are kept (runtime change). lukemorales silently dropped everything except queryKey and queryFn (its issues #83 and #85). Here staleTime, placeholderData, initialData and the rest reach useQuery. If you added options that never took effect, they now do, so review them before switching.
  • Keys are DataTag-branded, like queryOptions(). getQueryData returns your data type instead of unknown.
  • ctx.pageParam works and infinite entries work with useInfiniteQuery (#89, #73).
  • Mutation variables are typed from mutationFn (#55).
  • initialData gives defined data from useQuery (#76).
  • Entries keep your declaration order instead of being sorted. Keys are unchanged.
  • Objects have a normal prototype instead of Object.create(null).
  • inferQueryKeys returns only keys (queryKey, _def, _ctx), not other options.
  • TypedUseQueryOptions<Entry, Data> takes the same two type arguments, but is built on query-core's QueryObserverOptions instead of React's UseQueryOptions (the same options outside React-only ones such as suspense), and its error type is DefaultError instead of unknown.
  • mergeQueryKeys throws when two merged scopes define the same entry name; lukemorales let the later one win silently.

What's tested

  • Unit tests (Vitest) run every entry kind, context queries, stores, merging and mutations against a real QueryClient from @tanstack/query-core: fetching with the full key, infinite queries with page params, and every scope helper against a filled cache.
  • Type tests (expectTypeOf) cover useQuery, useSuspenseQuery, useInfiniteQuery, useQueries and useMutation from @tanstack/react-query, useQuery from @tanstack/vue-query, spreading entries into queryOptions() / infiniteQueryOptions(), skipToken, initialData, and the compatibility types.
  • Declaration emit is checked: a module that exports its keys compiles with declaration: true.
  • The library also type-checks against @tanstack/query-core 5.0.0, the lowest supported version.
  • The built dist/index.d.ts is type-checked with TypeScript 5.0.4 against @tanstack/query-core 5.0.0, with skipLibCheck off so the .d.ts itself is checked (npm run typecheck:ts5).
  • examples/react covers the main features: createQueryKeys (null, function and infinite entries, contextQueries, staleTime, placeholderData), createMutationKeys, mergeQueryKeys, inferQueryKeyStore, useQuery, useInfiniteQuery, useMutation, scopeKey, invalidateScope, cancelScope, setScopeData, getScopeData and typed getQueryData. npm run check:example installs the packed tarball into it and type-checks it. The rest (createQueryKeyStore, refetchScope, removeScope, TypedUseQueryOptions, the other infer* types, skipToken, initialData, useSuspenseQuery, useQueries) is covered by the unit and type tests only. Releases so far were also checked by hand in a browser with it, including a fresh install from npm.
  • Solid, Svelte and Angular Query rely on the shared query-core types and have no tests of their own yet.

Support

  • ๐Ÿ› Bug or feature request: open an issue
  • ๐Ÿ”’ Security issue: please report privately via GitHub's security tab

Need this built into your app, or something custom?

Get in touch.

Hire me on Upwork

More packages

Part of a family of open-source packages. See all of them.

start-seo-kit SEO for TanStack Start: meta tags, sitemaps, JSON-LD

Credits

The API follows @lukemorales/query-key-factory by Luke Morales (MIT), which this package replaces. The code here is a new implementation for TanStack Query v5.

License

MIT. See LICENSE.md.

About

Typed query-key hierarchy and invalidation helpers for TanStack Query v5, built on queryOptions(). A maintained successor to @lukemorales/query-key-factory.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages