Skip to content

feat(query-db-collection): support automatic refetch on query invalidation #381

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kels-c
Copy link

@kels-c kels-c commented Aug 6, 2025

From #344:

Adds support for automatic refetching of collections when queryClient.invalidateQueries is called with a matching queryKey. This removes the need for manually coordinating utils.refetch() calls across related collections.

Tests are passing, but I’d appreciate a second look to confirm they cover the right scenarios — especially around observed vs. passive (unobserved) invalidation.

Copy link

changeset-bot bot commented Aug 6, 2025

⚠️ No Changeset found

Latest commit: bd4cdab

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@kels-c kels-c marked this pull request as ready for review August 6, 2025 00:54
@@ -834,7 +834,7 @@ describe(`QueryCollection`, () => {

// Wait for refetch to complete
await vi.waitFor(() => {
expect(queryFn).toHaveBeenCalledTimes(2)
expect(queryFn).toHaveBeenCalledTimes(3)
Copy link
Author

Choose a reason for hiding this comment

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

This test might need to be changed, open to feedback!

Copy link
Collaborator

Choose a reason for hiding this comment

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

huh wait... did we already have support for invalidating queries?? 😅 It looks like we just added another implementation... it seems Query was already handling this internally... if this is the case, then we have a lot less to do, just update the docs/jsdocs a bit.

Copy link
Author

Choose a reason for hiding this comment

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

Definitely appears so, and to be honest, that's what I've been confused about. Still not completely following the need for refetch in general, if under the hood we're reading from the cache from query? But I don't have full context as well. 😄

I'll go ahead and remove the changes.

If this behavior is handled by Query already, do we want to mention that directly in docs/query-collection.md? What did you have in mind?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah when building this originally and writing this issue it didn't occur to me that the query client would just keep calling the queryFn like normal.

Refetch is nice as not everyone is migrating from a full blown normal Query setup so they might just have a one-off collection that they want to be able to manually refetch. It depends I suppose on your mental model of your state. A web of data or standalone table-like collections.

There's a few places where we mention calling other collections to refetch (search for "refetch". So we should mention first that you can invalidate a tag or just directly tell those collections to refetch.

@KyleAMathews
Copy link
Collaborator

The build is failing — also let's update docs/jsdocs where we recommend calling refetch on collections to also mention invalidate as an option.

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.

2 participants