RFC: Runtime Type-Safe React Query Hook Generator #9897
Unanswered
dominiquekossi
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
RFC: Runtime Type-Safe React Query Hook Generator
Summary
This RFC proposes adding support for a runtime, type-safe React Query hook generator to the TanStack Query ecosystem. Unlike traditional code generation tools, this approach uses a typed configuration object to automatically generate fully-typed React Query hooks at runtime, eliminating boilerplate while maintaining zero runtime overhead beyond TanStack Query itself.
The proposal is based on a working open-source implementation published as the package
tanstack-api-generator, which has been battle-tested in production environments.Problem Statement
Building React Query hooks for large or evolving APIs requires repetitive, manual work:
There is currently no official or recommended way to:
Proposed Solution
Introduce an official or community-supported runtime React Query hook generator that:
Key differentiator: This is NOT a code generation tool. It's a runtime library that uses TypeScript's type system to provide full type safety without generating files.
A working implementation exists as the community package
tanstack-api-generator.Motivation
A runtime hook generator improves the developer experience:
Projects using TanStack Query at scale would benefit immediately from an official solution that standardizes patterns and reduces boilerplate.
Key Features
1. Zero Boilerplate
Define your API once, get fully-typed hooks automatically:
2. Automatic Query Keys
Query keys follow TanStack Query v5 hierarchical standards automatically:
3. Automatic Invalidation
Mutations automatically invalidate related queries based on HTTP semantics:
4. Full Type Safety
Complete TypeScript inference from configuration to hooks:
5. Runtime Validation
Optional Zod integration for runtime type safety:
6. Request/Response Interceptors
Add authentication, logging, or custom headers easily:
Detailed Proposal
Input
The library accepts a typed configuration object with:
/users/:id)Example configuration:
Output
For each endpoint, the library provides at runtime:
Example usage:
All hooks follow TanStack Query v5 best practices:
[group, endpoint, params]API Design Goals
Alternatives Considered
Manual creation of React Query hooks - Repetitive, error-prone, inconsistent patterns across teams
Build-time code generation (OpenAPI, GraphQL codegen) - Requires build step, generates files that clutter the codebase, harder to customize
Generic code generators not tailored to TanStack Query - Don't follow TanStack Query v5 best practices, no automatic invalidation
In-house tools created separately by each team - Duplicate effort, inconsistent implementations, no community support
tRPC - Excellent but requires backend to be TypeScript, not suitable for REST APIs or third-party APIs
The runtime approach offers the best balance: full type safety without code generation, instant feedback, minimal configuration, and follows TanStack Query best practices out of the box.
Open Questions
Prior Art / Prototype
Package:
tanstack-api-generatorDescription: Production-ready runtime library that generates typed React Query hooks, mutation hooks, query keys, and invalidation utilities from a typed configuration object. Features include:
Current Status:
This RFC proposes evolving this concept into an official or community-endorsed solution aligned with TanStack architecture and standards.
Conclusion
A runtime hook generator for TanStack Query would:
The runtime approach offers unique advantages over traditional code generation:
This RFC proposes formalizing such a tool and aligning the final design with TanStack maintainers' expectations for quality, extensibility, and best practices.
Request for Feedback
Feedback requested on:
@tanstack/query-generatoror similar?)Technical Implementation Details
The current implementation uses:
useQueryanduseMutationhooks as primitives[group, endpoint, params]Key technical achievements:
Beta Was this translation helpful? Give feedback.
All reactions