Query Key Factory
Typesafe query key management for TanStack Query with first-class autocompletion.
Repository Health
Technical Analysis
Query Key Factory is a small, tree-shakeable TypeScript library that gives TanStack Query users a structured, typesafe way to declare and organize their query keys. Instead of scattering hand-written key arrays across your codebase, you define a store of keys and query functions in one place and get full editor autocompletion for every key.
It works with the React, Vue, Solid, and Svelte adapters of TanStack Query, and produces standard query key arrays that plug directly into useQuery, queryClient.invalidateQueries, and the rest of the API — making cache invalidation predictable and refactor-safe.
What You Get
createQueryKeysandcreateQueryKeyStorefor declaring typed key hierarchiesmergeQueryKeysto compose feature-colocated key modules into one storecreateMutationKeysfor typed mutation key management- Standard query key arrays compatible with every TanStack Query adapter
- Full TypeScript inference and editor autocompletion for keys and query functions
Common Use Cases
- Centralizing all of an app’s TanStack Query keys in a single typed store
- Colocating query keys and query functions per feature and merging them
- Invalidating groups of related queries without remembering exact key shapes
- Refactoring query keys safely with compiler-checked references
Under The Hood
Architecture The library is a set of pure factory functions in src/ — create-query-keys, create-query-key-store, create-mutation-keys, and merge-query-keys — that transform a declarative key schema into standardized query key arrays. Each function walks the supplied object, prefixes keys with their scope, and preserves any attached queryFn/contextQueries so the output slots directly into TanStack Query’s useQuery and invalidation APIs. Tech Stack Authored in TypeScript with heavy use of conditional and mapped types (utility-types.ts, *.types.ts) for inference, built and bundled into ESM/CJS with types, and tested with Vitest. It has no runtime dependencies and lists TanStack Query only as a peer. Code Quality Each source module has a colocated .spec.ts file, and the codebase separates runtime logic from type-only helpers, reflecting a deliberate, well-typed design; the main gap is low recent maintenance activity. API Design The public surface is tiny and consistent — a handful of create* functions plus mergeQueryKeys — and the ergonomics come almost entirely from type inference, so most usage is discoverable through autocompletion with little boilerplate.