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

Drastically simplify useQuery default options processing #9665

Merged

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented May 3, 2022

When implementing the new useQuery(query, { defaultOptions }) functionality in #9563, I fell under the (thankfully mistaken) impression that I needed to handle all defaulting of WatchQueryOptions within the createWatchQueryOptions method of the InternalState class that useQuery uses internally.

This PR demonstrates we can avoid that computation entirely, and rely instead on existing defaulting mechanisms (in watchQuery) to do the same job. The trick is to use the defaultOptions from useQuery(query, { defaultOptions }) only when we first create the ObservableQuery (that is, only when we call client.watchQuery). That way, the defaultOptions are only relevant once, and do not continue to interfere with WatchQueryOptions (re)computation after that.

This simplification means we no longer need to consider the "latest" values of default options in createWatchQueryOptions, because we no longer consider default options at all in createWatchQueryOptions. More generally, at the point in time when we provide defaultOptions (once, when calling client.watchQuery), we have not yet created the ObservableQuery, so there are no other option values to worry about, latest or otherwise.

By contrast, my previous code went to unnecessary lengths to give precedence to this.observable.options whenever there was a default option of the same name. Unfortunately, that trick (or at least the way I implemented it) did not work for the fetchPolicy option in conjunction with skip, since skip: true sets this.observable.options.fetchPolicy = "standby", and we do not want to recycle that standby value (even though it is technically the "latest" value) when we switch back to skip: false. Instead, we want the original/initial fetchPolicy to be restored at that point. Fixing this problem fixes the reproduction in #9635.

Should fix the following issues:

Comment on lines +371 to +377
|| this.client.watchQuery(mergeOptions(
// Any options.defaultOptions passed to useQuery serve as default
// options because we use them only here, when first creating the
// ObservableQuery by calling client.watchQuery.
this.queryHookOptions.defaultOptions,
this.watchQueryOptions,
));
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the crux of the new strategy, allowing all the other simplifications.

Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

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

Awesome - thanks @benjamn!

@benjamn benjamn merged commit 15d00e8 into main May 3, 2022
@benjamn benjamn deleted the issue-9635-useQuery-stuck-in-standby-with-defaultOptions branch May 3, 2022 14:49
benjamn added a commit that referenced this pull request May 3, 2022
This test started out as a regression test for PR #9665, but it revealed
a few other problems that I will fix in the next commit, potentially
solving issue #9655 as well.
brainkim pushed a commit that referenced this pull request May 3, 2022
This test started out as a regression test for PR #9665, but it revealed
a few other problems that I will fix in the next commit, potentially
solving issue #9655 as well.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
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.

3.6.0 Some queries never executing with a conditional skip and overridden fetchPolicy
2 participants