Replies: 1 comment
-
As for the technical realisation, either we could override React Query's type for |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
first off, thanks for the great package! I've been wanting to have something like this ever since I started using React Query over a year ago.
Open here for some personal backstory why I love this package
My main problem with React Query is that for queries there should be a single source of truth that links query key, query function, query parameter types and query return types together. But the
useQuery
hook makes you to define these relationships anew over and over again every time you call the hook, which wastes time, makes it easy to introduce an inconsistency, and makes it hard to refactor.So to solve this for the projects at work I had basically written my own "wrapper" around React Query which is very similar to what this package is doing, but since working with complex TypeScript type definitions can be quite daunting I didn't get as far as you guys. Which is why I was happy to finally discover this package! All this time I couldn't believe nobody else had the same problem with React Query as I did and created a package for it.
I'm not sure if you're familiar, but Redux Toolkit has a library similar to React Query called RTK Query, and I much prefer their structure: You first define your API using the
createApi
function, where you define all the keys, functions, and data types. This was my main inspiration, but RTK Query goes a bit "too far" by (1) being centered around URLs and endpoints (not flexible for other types of APIs) and (2) requiring Redux which isn't a dependency necessary for every project. So React Query + a helper library is my preferred solution.The feature I propose is to enable typing of the QueryClient's
setQueryData
function. Through thequeryFn
field defined on the queries in thecreateQueryKeyStore
function, we already give the type information of the data that this query will hold. Based on that we could type thesetQueryFunction
to accept only this type.The advantage is big: Currently, you can set the query data to any data you like, and then the type promise that
useQuery()
gives your components no longer holds true. It is up to the developer to keep track which query keys should contain what data types, and pass the right data every time. There are no type checks.Example:
And other functions such as
getQueryData
should also be type-safe.Use cases include Update from Mutation Responses and Optimistic Updates.
Beta Was this translation helpful? Give feedback.
All reactions