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

Make ObservableQuery#getCurrentResult always call queryInfo.getDiff() #8422

Merged
merged 3 commits into from
Jun 28, 2021

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Jun 23, 2021

Appears to fix #7978, by always giving the cache a chance to provide an up-to-date result for the given options, rather than relying on observableQuery.lastResult, which may be stale.

Note that QueryInfo#getDiff knows how to handle fetchPolicy: "no-cache", so it's safe to call even in that case.

@benjamn benjamn self-assigned this Jun 23, 2021
@benjamn benjamn added this to the June 2021 milestone Jun 23, 2021
@benjamn benjamn requested review from brainkim and hwillson June 23, 2021 18:22
@benjamn benjamn changed the title Make ObservableQuery#getCurrentResult always call queryInfo.getDiff(). Make ObservableQuery#getCurrentResult always call queryInfo.getDiff() Jun 23, 2021
benjamn added a commit that referenced this pull request Jun 23, 2021
benjamn added 3 commits June 23, 2021 15:08
Appears to fix #7978.

Note that QueryInfo#getDiff knows how to handle fetchPolicy: "no-cache",
so it's safe to call it even in that case.
@benjamn benjamn force-pushed the issue-7978-ObservableQuery-getCurrentResult branch from 540a1a4 to f23cba6 Compare June 23, 2021 19:14
@dylanwulf
Copy link
Contributor

dylanwulf commented Jun 23, 2021

@benjamn Thanks! This does appear to resolve the previous data issue. I do have a question about the network-only fetch policy though. If I have a query that uses network-only, and I change the variables while the query is still mounted, are the results for the new variables supposed to be obtained from the cache? When I change the variables in my reproduction, it appears to (1) get results from the cache to display immediately, (2) fetch fresh data from the server, then (3) display the new data from the server. This behavior sounds more like cache-and-network to me, but maybe I'm misunderstanding something?

@dylanwulf
Copy link
Contributor

dylanwulf commented Jun 23, 2021

I'm also a little confused about how nextFetchPolicy is supposed to behave when changing variables. It seems to me like changing variables should reset back to fetchPolicy instead of using nextFetchPolicy. Our app has a paginated (infinitely scrolling) query with fetchPolicy: 'network-only', nextFetchPolicy: 'cache-first'. The nextFetchPolicy: 'cache-first' is necessary for pagination to work correctly, but every time any of the variables change we want to discard the cached results and fetch fresh data from the server. I couldn't find an easy way to accomplish that with the current API, but again I might just be misunderstanding something.

Anyways, thanks again! ❤️

EDIT: This behavior with nextFetchPolicy doesn't seem to be occurring in v3.3. It looks like something in the v3.4 branch made it so that changing the variables on a query will use nextFetchPolicy instead of fetchPolicy. I can create a separate issue/reproduction for that if you want?

@benjamn
Copy link
Member Author

benjamn commented Jun 24, 2021

@dylanwulf A separate issue/test/repro would be great, if possible!

Alternatively, you could try out different beta/rc releases to figure out where the change happened. The first beta was @apollo/client@3.4.0-beta.0, continuing until @apollo/client@3.4.0-beta.28, and then @apollo/client@3.4.0-rc.0 through @apollo/client@3.4.0-rc.14 (so far). I don't know if git bisect will be much use, but a manual binary search should take 6ish steps.

@dylanwulf
Copy link
Contributor

dylanwulf commented Jun 24, 2021

@benjamn Ok I submitted a new issue with reproduction in #8426. Looks like it first showed up in rc.5. I'm still wondering if you could answer my question about expected behavior of the network-only fetch policy in my first comment? That behavior is separate from the issue I found in v3.4 and I can't tell if it's intentional or not.

CHANGELOG.md Show resolved Hide resolved
Copy link
Contributor

@brainkim brainkim left a comment

Choose a reason for hiding this comment

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

LGTM. Some of the logic changes might be blocking what I was currently working on.

@benjamn benjamn merged commit 36fea0f into release-3.4 Jun 28, 2021
@benjamn benjamn deleted the issue-7978-ObservableQuery-getCurrentResult branch June 28, 2021 20:58
@hwillson hwillson removed this from the MM-2021-06 milestone Jul 29, 2021
benjamn added a commit that referenced this pull request Aug 12, 2021
This fixes #7978 in a better way than #8422, by actually checking
whether the `last.variables` used for `last.result` are still equal to
the current `options.variables`, before reusing `last.result` in
`ObservableQuery#getCurrentResult`.
benjamn added a commit that referenced this pull request Aug 13, 2021
This fixes #7978 in a better way than #8422, by actually checking
whether the `last.variables` used for `last.result` are still equal to
the current `options.variables`, before reusing `last.result` in
`ObservableQuery#getCurrentResult`.
benjamn added a commit that referenced this pull request Aug 16, 2021
This fixes #7978 in a better way than #8422, by actually checking
whether the `last.variables` used for `last.result` are still equal to
the current `options.variables`, before reusing `last.result` in
`ObservableQuery#getCurrentResult`.
@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.

Previous data returned when changing variables and fetchPolicy is network-only (Reproduction Provided)
4 participants