-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
feat(react-query): add typesafe query disabling via skipToken #6999
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
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 efb6b6d:
|
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.
two things to keep an eye on:
- it shouldn't be possible to pass a
skipToken
to suspense versions of the hooks, likeuseSuspenseQuery
, because those always setenabled: true
. We should likely forbid this on type level, and at runtime, only setenabled: false
if we have a skipToken and enabled isn't already true - we should watch out how this influences places where we try to infer the type of what the
queryFn
returns, because withskipToken
, it might not be a function.queryOptions
is one of those places, I think we should add a type-test for that.
☁️ Nx Cloud ReportCI is running/has finished running commands for commit efb6b6d. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 2 targetsSent with 💌 from NxCloud. |
Is it only for react-query package or should this be implemented in other implementations of query as well? Once we accept queryFn as skipToken it will work like that everywhere Update: I implemented this on type level for useSuspenseQuery, I will implement type-test, why should we implement this for runtime? To help out people who are doing raw JS? |
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.
regarding queryOptions
- does this still work?
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Math.random() > 0.5 ? skipToken : Promise.resolve(5),
})
const data = queryClient.getQueryData(options.queryKey)
here, data
should be of type number | undefined
- inferred from the queryFn
. I just want to make sure that the skipToken
existence doesn't destroy that.
Oh Great 👍 |
… is set to false instead
… is set to false instead
…sx files are not executed anyway
the |
@TkDodo I found potential problem when fixing the The queryKeys I used were already declared in another test. Maybe we should do cleanup after every test to ensure that the queryKeys are not shared? I don't think there is a use in sharing them |
yeah that's what the |
…spect skipToken test
I can't find the documentation for skipToken. |
https://tanstack.com/query/latest/docs/framework/react/guides/disabling-queries |
@Jaaneek thanks! any idea as to why |
I found following code works better for me: const { data } = useQuery({
queryKey: ['todos', filter],
// ⬇️ avoid network fetch
queryFn: async () => filter ? await fetchTodos(filter) : [],
}) We don't have to worry about unintentional calling |
This pull request is heavily inspired by RTK query skipToken.
https://redux-toolkit.js.org/rtk-query/usage/conditional-fetching#overview
We are introducing skipToken which allows typesafe disabling of queries.