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

Allow refetchQueries to refetch observerless queries (again) #9616

Closed

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Apr 21, 2022

This PR is a follow-up to PR #8825, which was first released in Apollo Client v3.4.14. As a number of commenters have reported in issue #5419, it appears we did not completely fix the problem, so this PR attempts to finish that work.

To reiterate the motivation behind #8825: an ObservableQuery redelivers the most recent result/error to new subscribers, so it is not completely pointless to refetch an ObservableQuery that (currently) has no subscribers.

However, any ObservableQuery in standby mode (whose options.fetchPolicy is standby) should still be excluded from "active" status as far as refetchQueries is concerned. This logic handles useLazyQuery queries that have not yet been executed, since they start out in standby mode until called.

This PR is a follow-up to PR #8825, which was first released in Apollo
Client v3.4.14. As a number of commenters have reported in issue #5419,
it appears we did not completely fix the problem, so this PR attempts to
finish that work.

Since ObservableQuery redelivers the most recent result/error to new
subscribers, it is not completely pointless to refetch an
ObservableQuery that (currently) has no subscribers.

However, putting the ObservableQuery in "standby" mode by setting the
obsQuery.options.fetchPolicy to "standby" should still exclude the
ObservableQuery from "active" status as far as refetchQueries is
concerned. This logic handles useLazyQuery queries that have not yet
been executed, since they start out in "standby" mode until called.
@benjamn benjamn self-assigned this Apr 21, 2022
@benjamn benjamn added this to the Release 3.6 milestone Apr 21, 2022
@benjamn benjamn linked an issue Apr 21, 2022 that may be closed by this pull request
@benjamn
Copy link
Member Author

benjamn commented Apr 21, 2022

In other words, I now believe I was wrong about #8825 (comment), and the active shorthand should only mean "not standby" with no additional requirement that the observables currently have observers/subscribers.

@benjamn
Copy link
Member Author

benjamn commented Apr 21, 2022

On third thought, with the exception of @Off2Race in #5419 (comment), I doubt most of the people reporting problems in #5419 are using the refetchQueries: "active" shorthand when performing mutations, so this PR might not be the answer for them.

Instead, the general theme in #5419 seems to be the unpredictable interaction between refetchQueries and recently-unmounted components/queries. Specifically, there seems to be a desire for refetching an unmounted query to have the effect of marking it for refetching whenever it gets mounted again.

The trouble with that idea, as I understand it, is that React gives us no way to recover the old ObservableQuery state for a re-mounted component, since we can't even tell whether two component renderings represent the same logical component (especially if the component moves around in the document between mountings).

I'll have to keep thinking about this, but I wanted to share my current thinking in case anyone has ideas.

@benjamn benjamn marked this pull request as draft April 21, 2022 19:54
@jpvajda jpvajda removed this from the v3.x.x patch releases milestone Aug 17, 2022
@jerelmiller
Copy link
Member

Hey @benjamn 👋

I know you wrote this PR a couple years ago, but after discussing with the rest of the team, we are going to hold off on making changes to the existing behavior for 3.x and revisit this API in its entirety for a future major version. As such, I'm going to close this. Thanks for your exploration and explanations here which helped us arrive at this conclusion!

@jerelmiller jerelmiller deleted the let-refetchQueries-refetch-observerless-queries branch January 22, 2024 17:22
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refetchQueries not working when using string array after mutation
4 participants