Skip to content
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: usePrefetchQuery #7582

Merged
merged 12 commits into from
Jun 25, 2024
Merged

feat: usePrefetchQuery #7582

merged 12 commits into from
Jun 25, 2024

Conversation

TkDodo
Copy link
Collaborator

@TkDodo TkDodo commented Jun 17, 2024

missing:

  • docs
  • runtime tests
  • type tests

Copy link

vercel bot commented Jun 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
query ⬜️ Ignored (Inspect) Visit Preview Jun 25, 2024 11:55am

Copy link

nx-cloud bot commented Jun 17, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit dd7cc03. 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 1 target

Sent with 💌 from NxCloud.

Copy link

codesandbox-ci bot commented Jun 17, 2024

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 dd7cc03:

Sandbox Source
@tanstack/query-example-angular-basic Configuration
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

Copy link

codecov bot commented Jun 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.03%. Comparing base (6355244) to head (dd7cc03).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #7582       +/-   ##
===========================================
+ Coverage   43.88%   86.03%   +42.14%     
===========================================
  Files         184       24      -160     
  Lines        7029      315     -6714     
  Branches     1539       80     -1459     
===========================================
- Hits         3085      271     -2814     
+ Misses       3578       37     -3541     
+ Partials      366        7      -359     
Components Coverage Δ
@tanstack/angular-query-devtools-experimental ∅ <ø> (∅)
@tanstack/angular-query-experimental ∅ <ø> (∅)
@tanstack/eslint-plugin-query ∅ <ø> (∅)
@tanstack/query-async-storage-persister ∅ <ø> (∅)
@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)
@tanstack/query-codemods ∅ <ø> (∅)
@tanstack/query-core ∅ <ø> (∅)
@tanstack/query-devtools ∅ <ø> (∅)
@tanstack/query-persist-client-core ∅ <ø> (∅)
@tanstack/query-sync-storage-persister ∅ <ø> (∅)
@tanstack/react-query 92.93% <100.00%> (+0.21%) ⬆️
@tanstack/react-query-devtools 10.71% <ø> (ø)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client 100.00% <ø> (ø)
@tanstack/solid-query ∅ <ø> (∅)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client ∅ <ø> (∅)
@tanstack/svelte-query ∅ <ø> (∅)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client ∅ <ø> (∅)
@tanstack/vue-query ∅ <ø> (∅)
@tanstack/vue-query-devtools ∅ <ø> (∅)

brunolopesr and others added 3 commits June 19, 2024 08:14
…7586)

* chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery

* chore: update tests to assert the alternative spy is not called

* chore: add some new tests

* chore: remove it.only whoops

* chore: call mockClear after fetching

* chore: improve waterfall test by asserting fallback calls instead of loading node query

* chore: improve code repetition

* chore: add some generics to helper functions
* chore: add type tests and docs

* chore: update hooks to use FetchQueryOptions and FetchInfiniteQueryOptions

* chore: update tests

* chore: update docs

* chore: remove .md extension from link

* chore: add unknown default value to TQueryFnData

* Apply suggestions from code review

---------

Co-authored-by: Dominik Dorfmeister <[email protected]>
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jun 19, 2024
@TkDodo TkDodo marked this pull request as ready for review June 20, 2024 12:55
@TkDodo TkDodo requested a review from Ephem June 20, 2024 12:56
Copy link
Collaborator

@Ephem Ephem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few NITs on the tests, but I think the implementation is fine to ship as is. 🎉

docs/framework/react/guides/prefetching.md Outdated Show resolved Hide resolved
const rendered = renderWithClient(queryClient, <App />)

await waitFor(() => rendered.getByText('data: Prefetch is nice!'))
expect(queryOpts.queryFn).toHaveBeenCalledTimes(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this testing what it says? It might also be the case that usePrefetchQuery didn't fetch, but useSuspenseQuery did. Not sure if it necessarily needs fixing, but thought I should point it out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we should probably use a different queryFn for prefetching and the component so that we can assert which one ran. @brunolopesr can you add that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ephem @TkDodo

Added a better assertion for this in PR #7614

})

function App() {
usePrefetchQuery(queryOpts)
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 to replicate the bug we saw with endless loops, the usePrefetchQuery would have to be inside the Suspense boundary so it would rerender->refetch when unsuspended? I haven't tested so not entirely sure though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ephem , I'm not sure, but this endless loops isn't about the usage of ensureQueryData instead of prefetchQuery? But I will add a test covering the usage of the hook inside the Suspense Boundary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added new tests to ensure that endless loops don't occur at #7614

packages/react-query/src/__tests__/prefetch.test.tsx Outdated Show resolved Hide resolved
await waitFor(() =>
rendered.getByText('data: Prefetch does not create waterfalls!!'),
)
expect(Fallback).toHaveBeenCalledTimes(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what tests for waterfalls? Seems a bit brittle to me. Maybe we could inspect the queryClient right after the renderWithClient call to see that all three queries are fetching?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test to assert the query state just after rendering in #7614

packages/react-query/src/__tests__/prefetch.test.tsx Outdated Show resolved Hide resolved
@brunolopesr brunolopesr mentioned this pull request Jun 24, 2024
3 tasks
brunolopesr and others added 2 commits June 25, 2024 11:04
* chore: add new tests

* Apply suggestions from code review

---------

Co-authored-by: Dominik Dorfmeister <[email protected]>
@TkDodo TkDodo merged commit fbfe940 into main Jun 25, 2024
8 checks passed
@TkDodo TkDodo deleted the feature/use-prefetch-query branch June 25, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation package: react-query
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants