[PoC] feat: generate TanStack Query Options from Hono Client functions #3619
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It seems from my research that Hono misses that last tiny bit of functionality for a seamless integration of Hono RPC with TanStack Query.
Problem:
The "problem" now is that we need to write queryKeys and queryFunctions for all routes, as is done here. It seems cumbersome and redundant to add React Query helper functions for all routes when they are all available on the Hono client.
According to this comment this is missing to a few people and a selling point for tRPC over Hono RPC.
Proposed solution:
I tried to generate Tanstack queryKey, queryFn and queryOptions by reusing as much of the autocompletion that Hono client provides.
In your frontend code you can do the following:
and this will seamlessly generate the query key and function pair that you need when using React Query:
Further improvement:
Add a parameter to pass extra queryOptions to
getQueryOptions
.I think it means we need to import types from React Query, which is probably not wanted in this repo.
Caveat:
I'm aware that my approach is a bit basic, that I'm not a typescript ninja, and that I probably put the code in the wrong place. Also, I only tested it using React.
I'm very open to your feedback and to the code being improved.
Thanks
Please let me know what you think, I think it would be a great addition for the many, many Tanstack Query users.
The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code