-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(useQuery): let context infer queryKey #1955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(useQuery): let context infer queryKey #1955
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tannerlinsley/react-query/A47eSD3e9r5qMiFQa9Kteyj8s5kg |
| extends Omit< | ||
| UseBaseQueryOptions<TQueryFnData, TError, TData>, | ||
| 'queryFn' | 'queryKey' | ||
| > { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not change this on UseBaseQueryOptions directly? As far as I can see, this is the only interface that extends UseBaseQueryOptions....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a function in src/react/useBaseQuery.ts:9 that uses that uses the UseBaseQueryOptions interface, I mainly did it this way to keep the impact minimal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this goes hand-in-hand with the request for infinite query support, because src/react/useBaseQuery.ts:9 is only used for useQuery - which you have already adapted, and useInfiniteQuery. With regards to the queryKey, they are identical, so if you add useInfiniteQuery support to this PR, you could also get rid of the Omit I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I see what you mean now. I will update it accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a check and I don't think I can make it work like that the interfaces are put together like this
interface UseQueryOptions extends UseBaseQueryOptions extends QueryObserverOptions {}
interface UseInfiniteQueryOptions extends InfiniteQueryObserverOptions extends QueryObserverOptions {}While it is true that useQuery and useInfiniteQuery do both use the useBaseQuery function, I don't think that matters when it comes to inferring the queryKey (which is either inferred through QueryFunction or UseQueryOptions).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@boschni what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think we should try to keep the API consistent and implement things like this library wide instead of in one hook
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone is opting in to use this feature, they most likely want to use it throughout their codebase, and not only when using useQuery. At this moment you might only use useQuery, but others are also using queryClient.fetchQuery or useInfiniteQuery. If we provide support for this pattern, users should be able to expect that they can use it anywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll try to take another stab at this if i find the time - i think we’re already on the right track here 👍
| queryFn?: QueryFunction<TQueryFnData, TQueryKey> | ||
| } | ||
|
|
||
| export interface UseInfiniteQueryOptions< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you maybe also add the support for infinite queries as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could in the future, but I think I would prefer to do that on a separate branch (and perhaps another one for useMutation?).
|
@onursagir I picked up from where this PR was closed and I think I got it working for useQuery and useInfiniteQuery a like: #2047 |
Allows for the queryKey in the queryFn to be inferred as discussed in #1462
For example
