apollo-server
apollo-server copied to clipboard
Rest datasource caches data even when the cache control header is no-cache and also when explictly configured as no-cache or no-store
- Package name and version
"apollo-datasource-rest": "^0.13.0",
"apollo-server": "^2.24.1",
"graphql": "^15.5.0"
-
Expected behavior
- Expected it to not cache the api response when the response has header
cache-control: no-cache - Expected it to not cache the api response when explicitly configured to do so. Example
this.get(`https://randomuser.me/api/`, undefined, { cache: "no-cache", });this.get(`https://randomuser.me/api/`, undefined, { cache: "no-store", });this.get(`https://randomuser.me/api/`, undefined, { cache: "no-cache", cacheOptions: { ttl: 0 }, });
- Expected it to not cache the api response when the response has header
-
Actual behavior - The api response is cached in either of the cases. - I have demonstrated the caching behavior with the time it takes to fulfill the subsequent request in the exaple repo given below - The actual time may depend on the connection speed and location. But the overall pattern remains the same - Please also refer to the screenshots
-
Repo for reproduction Refer to the instructions given in the readme https://github.com/shreyas1496/apollo-cache-issue
Expected logs in console

Actual logs in console

Thanks for the issue. Just as a note: as described at https://github.com/apollographql/apollo-server/pull/5070#issuecomment-847980268 we are not fully focused on apollo-datasource-rest issues at the moment, but hope to return to it soon.
I'm aware it's not the main focus right now but for whatever it's worth, I believe the problem might be due to the fetch method of the RESTDataSource class. The method does not look at the header (to double check the "no-cache" configuration) before performing the fetch.
See the fetch method's declaration, beginning at line 204. Perhaps the solution lies in adding that double-check before this.cacheKeyFor() is invoked (in line 249) or to incorporate the check in the if conditional at the end of the method (at line 268).
I could be totally wrong though. :^)
This has also impacted my project, despite disabling the cache explicitly I had a bug mysteriously arise that appears to be due to cached data :(.
Hi there, I just posted a detailed description of the problem. Hope you find it useful until/if the issue gets fixed