-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fetchMore and fetchPolicy: cache-and-network does not work together #6916
Comments
@alex-golubtsov I believe you're describing a related issue to the one mentioned here -> #6907 Addition from myself: |
See also #6327 and #6833, especially this comment. Looks like the proper policy combination in 2021 for paging+cache is
|
Any reason why that isn’t the default setting? |
My guess would be that the system is too flexible for "one size fits all" defaults. |
Having the same issue in Apollo Client 3.3.13 I can't use |
Reproduction: https://codesandbox.io/s/apolloclient-beta-fetchmore-bug-forked-8mplu?file=/src/App.js Click multiple times on the load more button: Indeed apollo client does a "phantom" request after the fetch more one: |
Note: still an issue in Apollo Client 3.4.0-beta.19 |
Any update on people trying to fix this??? |
I tried again on the codesandbox with 3.4.0-rc.23. The issue is kinda still here on the network side: the second phantom/unnecessary request with |
Seems it's still not working on my real project with 3.4.0-rc.23. Still having two merges with the second one being the initial query before the |
Digging into the source code... The unwanted fetch seems to be caused by this line: apollo-client/src/core/ObservableQuery.ts Line 333 in 336337d
Which in turn calls this with the initial variables/options: apollo-client/src/core/Reobserver.ts Line 52 in 336337d
On 3.4 the lines are all in apollo-client/src/core/ObservableQuery.ts Line 383 in 7856c18
And the fetch: apollo-client/src/core/ObservableQuery.ts Line 658 in 7856c18
Not sure what a concast is though. |
If I remove the apollo-client/src/core/ObservableQuery.ts Line 372 in 7856c18
This apollo-client/src/core/QueryInfo.ts Line 205 in 7856c18
This then calls a apollo-client/src/core/QueryInfo.ts Line 233 in 7856c18
Then the result from |
There is probably an architectural issue if a |
Maybe related? #8541 |
I've just spent ~3.5h debugging my code, then Apollo, investigating what appears to be the same issue as described, in a lazy loading list view. Fetch more is correctly fetching and merging the next page of data, but a second request without the pagination token was then being sent and replacing the data in merge, causing the page content to reset and reset the scrollbar to the end, tiggering a refetch of the second page again, repeating infinitely @khitrenovich suggestion to set fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first', appears to suppress the second call and things now seem to work as expected. What's frustrating is this query is only used in one place, so I'm not sure what exactly is subscribed to be notified which could be triggering this behavior. |
Unfortunately this trick doesn't work with |
I can also confirm this behavior, but I can't use the workaround. I really need |
After thinking more about this, I agree with @Akryum the extra network fetch after Yes, you may be able to work around the problem with I'm still investigating the consequences of these changes, but I think/hope we can remove the commit 5c378d1d07a33ab4de0c14d2f6d68d48297ef9ec
Author: Ben Newman <ben@apollographql.com>
Date: Tue Aug 17 16:00:56 2021 -0400
Avoid full reobservation in QueryInfo listener when diff.complete.
diff --git a/src/core/QueryInfo.ts b/src/core/QueryInfo.ts
index ea23f374a..7f5b6ce5d 100644
--- a/src/core/QueryInfo.ts
+++ b/src/core/QueryInfo.ts
@@ -235,7 +235,8 @@ export class QueryInfo {
// full reobservation, since oq.reobserve might make a network
// request, and we don't want to trigger network requests for
// optimistic updates.
- if (this.getDiff().fromOptimisticTransaction) {
+ const diff = this.getDiff();
+ if (diff.complete || diff.fromOptimisticTransaction) {
oq["observe"]();
} else {
oq.reobserve(); The I hope to have more answers soon. Thanks to @Akryum for sharing your findings! |
So what's the verdict here? Is it reasonable to expect/say this is a valid solution and no harm comes from it - hence it'd make it onto the next release <3? (Wishful thinking - I know, but hey, what's there if there's no hope?) |
Hi @benjamn |
Just following this - setting the fetchPolicy as recommended does not seem to stop my app from doing a whole query when a new subscription item is received and therefore duplicating the entire cache. I'm using the incredibly ugly hack
All subscriptions return all values PLUS the new one, which is always more than 3 (the number we get on fetchMore). So if this is a fetchmore we merge, and otherwise we just write the incoming - since the subscription query on the front end is already merging in the new data correctly, the incoming in that case is what we need. |
what is the solution at the moment ? still have this issue with
|
@videni The whole world is pretty much waiting on this and other similar issues. Hopefully it will make it to 3.6 release. The last good version of Apollo Client was 2.6.10 and we are still on this version. EDIT: The last package versions that work correctly are:
|
Any update? At this moment I can't use fetch more or create an infinite scroll 😭. There are some workaround to make this fetchMore? |
@jairmedeiros
It probably requires a refactor though to handle the loading/error states (our code used to wait for/catch the |
Some good news, at long last: using @Akryum's reproduction and While this change seems like the right behavior for most use cases, there's a chance it will be disruptive for code that was previously relying on |
Was closed on the team board so I am closing it here. |
How about github.com//issues/7485 and #6760, will those be addressed as well? |
For any poor soul who couldn't figure this out like me. My problem was unique in that I had set custom
The problem for me specifically was that My fix was to just remove
|
I had a similar issue to @zakton5. I originally though it was a
That field could be null for some of my data while I was doing a Changing my merge to be:
caused the issue to stop happening. |
Intended outcome:
In case of
fetchMore
additional request should not be executed or should use last variables (passed tofetchMore
)Actual outcome:
The request executes with wrong variables
How to reproduce the issue:
That's what is happening:
<MyComponent />
is rendered=> request to the server is executed with
{ offset: 0 }
#button
clicked=> request to the server is executed with
{ offset: 10 }
=> request to the server is executed with
{ offset: 0 }
In other words the request, which fetches fresh data from the server uses variables passed to the hook, but to to
fetchMore
Versions
The text was updated successfully, but these errors were encountered: