-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ import { | |
| InfiniteQueryObserverResult, | ||
| MutateOptions, | ||
| MutationStatus, | ||
| QueryFunction, | ||
| QueryKey, | ||
| QueryObserverOptions, | ||
| QueryObserverResult, | ||
| } from '../core/types' | ||
|
|
@@ -18,8 +20,16 @@ export interface UseBaseQueryOptions< | |
| export interface UseQueryOptions< | ||
| TQueryFnData = unknown, | ||
| TError = unknown, | ||
| TData = TQueryFnData | ||
| > extends UseBaseQueryOptions<TQueryFnData, TError, TData> {} | ||
| TData = TQueryFnData, | ||
| TQueryKey extends QueryKey = QueryKey | ||
| > | ||
| extends Omit< | ||
| UseBaseQueryOptions<TQueryFnData, TError, TData>, | ||
| 'queryFn' | 'queryKey' | ||
| > { | ||
| queryKey?: TQueryKey | ||
| queryFn?: QueryFunction<TQueryFnData, TQueryKey> | ||
| } | ||
|
|
||
| export interface UseInfiniteQueryOptions< | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you maybe also add the support for infinite queries as well?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?). |
||
| TQueryFnData = unknown, | ||
|
|
||
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
UseBaseQueryOptionsdirectly? As far as I can see, this is the only interface that extendsUseBaseQueryOptions....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:9that uses that uses the UseBaseQueryOptions interface, I mainly did it this way to keep the impact minimalThere 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:9is only used foruseQuery- which you have already adapted, anduseInfiniteQuery. 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.
Uh oh!
There was an error while loading. Please reload this page.
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
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
QueryFunctionorUseQueryOptions).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.
@boschni This pr includes no API changes, only a minor change to an interface to resolve a minor TypeScript inconvenience. I am not saying the interfaces for ouseInfiniteQuery can't be changed in order for inference to work. I'm saying it can't be done as @TkDodo describes
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 useuseQuery, but others are also usingqueryClient.fetchQueryoruseInfiniteQuery. 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 👍