Query Key Kit
@amjadiqbal/query-key-kit
TanStack Query v5's queryOptions() keeps one key next to its fetcher, but nothing says which keys exist, how they nest or which partial key to pass when you invalidate. Teams hand-write key arrays, or use @lukemorales/query-key-factory, which has had no release since February 2024 (1.3.4) and drops every option except queryKey and queryFn.
createQueryKeys(scope, schema) turns a plain object of entries into a key tree. Each entry gives back an object you can pass straight to useQuery, fetchQuery or useInfiniteQuery: the full queryKey, branded with the data type the way queryOptions() brands it, the queryFn, and every other option you wrote (staleTime, placeholderData, initialData, initialPageParam and so on). Nested contextQueries extend a parent key under _ctx. Every node of the tree carries its partial key, so invalidateScope, refetchScope, cancelScope, removeScope, getScopeData and setScopeData take the node instead of a hand-written array, and the data helpers know the type of every query they match. It keeps the function names and key output of @lukemorales/query-key-factory, with a few differences listed in the migration section. Tested with React in a real app and from a fresh npm install, and with Vue Query through type tests. An independent package, not made or endorsed by TanStack or by the author of @lukemorales/query-key-factory.
- createQueryKeys builds a typed key tree from four entry kinds: null, a key tuple, an options object or a function returning either
- Entries produce queryOptions()-shaped objects with a DataTag-branded queryKey, so getQueryData, setQueryData and fetchQuery return your data type without a cast
- Every query option written on an entry is passed through unchanged, including staleTime, placeholderData, initialData and the infinite-query options
- contextQueries nest child queries under a parent key, reached through _ctx