-
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
Infinite network requests with 'cache-and-network' fetch policy #9450
Comments
I independently hit the same problem and created a sandbox with repro steps. It uses the default fetch policy and is based on two queries to the same type that is cached in the denormalized way (no id, no key fields). https://codesandbox.io/s/github/ertrzyiks/apollo-overfetching-example/tree/main/ By default it just duplicates the first query but it ends up in the infinite loop if you update
to
I narrowed it down to different handling of the server response in the apollo core QueryInfo. I didn't dive deeper, but I found a related comment in another issue: #6760 (comment) Anyway, if the second request for the same query returns different data than the first one (simulated with Math.random()) we end up in the infinite loop of requests. If the responses match, it ends on just one additional query being made. |
I believe I'm hitting a similar problem. I have two different queries with overlapping data. One using "network-only" and the other using "cache-and-network" fetch policies. Similarly as described in both issues above, the queries return different results if queried multiple times. The results differ because they contains lists and the list sorting on the backend in not stable. |
We've had this problem for over two years now. It might get resolved some day but until then I'm advising everyone I work with to use alternatives to apollo-client. How we've been working around this is simply to stop returning errors from the server. |
We heard from a customer in Mid Sept that this was also a problem they were having. here is what they said: The query actually resolves but then restarts. Every time the query resolves a second or 2 later it starts again. However, I seem to have 2 cases triggering this repetition. The first only does this when the object being returned is a large set of data. The fetchPolicy is set to "cache-first" but has no problems with smaller sets of returned data. The second is a case where 2 hooks (for example useGetDataQuerySubsetOne and useGetDataQuerySubsetTwo) are calling the same graph query (for example query getDataQuery) but each returns different bits of information. When these utilize fetchPolicy: "network-only" they work just fine but when I change it to fetchPolicy: "cache-first" they start entering the fetch resolve (notice this triggers with a successful resolution rather than it being an issue with caching an error) > repeat loop back to back, and subsequently repeatedly triggering child queries that depend of data from each of them. We also do not yet have a typePolicy defined for either of these queries, if that helps. If my understanding is correct I should be able to reproduce it, I'll attempt to submit something asap, but I wanted to provide a further verbose explanation just in case. I am checking which version Client they are on an asking them to upgrade to 3.7, which is our latest. |
@jpvajda When I forked this codesandbox in #9450 (comment) and upgraded to Apollo Client |
Sharing some findings. This issue seemed reminiscent of a bug that @alessbell is working on fixing here: #10143. I thought it might be interesting to see if the problem described in this Issue would still be present in the latest revision on that branch. If I'm looking at this correctly, that branch appears to resolve this bug. You can check it out yourself in this repo I made based on @LinusU's original comment: https://github.com/bignimbus/use-query-infinite-loop. Installation instructions are a bit complicated, see the README. Screencapture below: Screen.Recording.2022-10-13.at.1.05.50.AM.mov |
The original issue here was resolved as of @tomas-c @stijn-gravity please try versions >= @ertrzyiks I have not yet isolated the issue you are seeing, forking your codesandbox and testing with the latest Thanks all! |
This took us quite a while to track down 😅
It's a combination of:
useQuery
with same query in multiple places (don't think it has to be same query, but with field overlap)fetchPolicy: 'cache-and-network'
lastOnlineAt
fieldSee below for a complete test case!
Intended outcome:
I did not intend for infinite network requests to be sent to my server.
Actual outcome:
The client sends a new request as soon as the last query has finished to receive a response from the server.
How to reproduce the issue:
mkdir use-query-infinite-loop
cd use-query-infinite-loop
npx create-react-app@latest .
index.js
to contain:npm start
Screen.Recording.2022-02-24.at.16.39.05.mov
This is the code used to run the server at
https://o5xgyw.sse.codesandbox.io
:Versions
The text was updated successfully, but these errors were encountered: