Query Key Factory

Typesafe query key management for TanStack Query with first-class autocompletion.

Library
npm
v1.3.4
1,673stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
43/100Fair
Development Activity0
Maintenance32
Community44
Maturity56
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
81/100Excellent
Architecture80
Code Quality84
Innovation78
Learning Curve82

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

  • createQueryKeys and createQueryKeyStore for declaring typed key hierarchies
  • mergeQueryKeys to compose feature-colocated key modules into one store
  • createMutationKeys for 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search