-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Feature/extended filters get query data #4190
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
Feature/extended filters get query data #4190
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 1594868:
|
getQueryData<TData = unknown>( | ||
queryKey: QueryKey, | ||
filters?: QueryFilters, | ||
): TData | undefined | ||
getQueryData<TData = unknown, Filters extends QueryFilters = QueryFilters>( |
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.
the generic trick is nice, but it sadly won't work if we provide a generic to TData:
const query = {
queryKey,
queryFn
}
queryClient.getQueryData<string>(query)
this will still fail because TypeScript has no partial inference for generics, so because we provide the first generic, the second one will default to QueryFilters
, and then TS complains that we cannot pass the queryFn
...
and in order to work with getQueryData
in a meaningful way in TypeScript, it is necessary to supply the TData
generic.
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.
Indeed.
So another option would be the solution of my first commit ?
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.
reverted to first commit for review and edited the PR message.
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.
One cool thing about using the extended filters, is that if your factory returns has a queryFn
the return type is now inferred :
const query = {
queryKey,
queryFn // return a string
}
const data = queryClient.getQueryData(query);
// ^? typeof data : string
const data = queryClient.getQueryData<string>(query); // stil works
const data = queryClient.getQueryData<number>(query); // typescript error number and queryFn missmatch
what about |
@@ -106,11 +107,18 @@ export class QueryClient { | |||
return this.mutationCache.findAll({ ...filters, fetching: true }).length | |||
} | |||
|
|||
getQueryData<TData = unknown>( | |||
filters: ExtendedQueryFilters<TData, any, TData, 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.
filters: ExtendedQueryFilters<TData, any, TData, QueryKey>, | |
filters: ExtendedQueryFilters<TData, unknown, TData, QueryKey>, |
could TError
be unknown
here?
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 so. I'll try it
getQueryData<TData = unknown>( | ||
queryKey: QueryKey, | ||
filters?: QueryFilters, | ||
): TData | undefined | ||
getQueryData<TData = unknown>( | ||
arg1: QueryKey | ExtendedQueryFilters<TData, any, TData, 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.
same question here:
arg1: QueryKey | ExtendedQueryFilters<TData, any, TData, QueryKey>, | |
arg1: QueryKey | ExtendedQueryFilters<TData, unknown, TData, QueryKey>, |
Codecov ReportBase: 96.36% // Head: 96.23% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #4190 +/- ##
==========================================
- Coverage 96.36% 96.23% -0.13%
==========================================
Files 45 62 +17
Lines 2281 2734 +453
Branches 640 789 +149
==========================================
+ Hits 2198 2631 +433
- Misses 80 101 +21
+ Partials 3 2 -1 Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Yes, if you agree, i'll also add it |
yep, awesome. I am trying to think if there is a situation where this will bite us, like when we introduce new query filters, they cannot overlap in namings with a query option that we already have, or vice-versa? like, we have a query filter called For typesafety, we would only need So:
would then become invalid because it contains |
You are correct on all points. So maybe, it would be safer to even go further. getQueryData(queryKey: QueryKey, filters?: Filters);
// and
getQueryData(queryOptions: QueryOptions, filters?: Filters); What do you think? |
Yes I think that would work 👍 |
For information, i'm strugling with where to handle this new overload. |
i'm sorry dom, but i can't make this work smoothly. |
no worries, thank you for trying ❤️ |
Hello @TkDodo @tannerlinsley ,
This PR is following this discussion :
https://twitter.com/ecyrbedev/status/1571561333992079363
Consider the following pattern :

The idea is now that people are using the factory pattern to create keys and
useQueryOptions
in general, it might be helpful to be consistent for every function and allow to pass directly the result of the factory to all functions.People might want to have everywhere the same ability to pass the factory result like they do for :
This PR add this possibility to :
Might also need to do it on (i can improve this PR upon agreement, after fixing details) :
The solution i took is to be backward compatible with current
getQueryData
, but also allow to pass only one parameter that would be the filter + ignore the additionnal parameters.For this i added an
ExtendedQueryFilters
: