Skip to content

Conversation

@onursagir
Copy link

Allows for the queryKey in the queryFn to be inferred as discussed in #1462

For example
image

@vercel
Copy link

vercel bot commented Mar 12, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/tannerlinsley/react-query/A47eSD3e9r5qMiFQa9Kteyj8s5kg
✅ Preview: https://react-query-git-fork-onursagir-fix-infer-query-variab-b32bef.vercel.app

Comment on lines +26 to +29
extends Omit<
UseBaseQueryOptions<TQueryFnData, TError, TData>,
'queryFn' | 'queryKey'
> {
Copy link
Collaborator

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....

Copy link
Author

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

Copy link
Collaborator

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.

Copy link
Author

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.

Copy link
Author

@onursagir onursagir Mar 12, 2021

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).

Copy link
Collaborator

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?

Copy link
Collaborator

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

Copy link
Author

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

Copy link
Collaborator

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.

Copy link
Collaborator

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<
Copy link
Collaborator

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?

Copy link
Author

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?).

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 29, 2021

@onursagir I picked up from where this PR was closed and I think I got it working for useQuery and useInfiniteQuery a like: #2047

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants