apollo-server icon indicating copy to clipboard operation
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

Open shreyas1496 opened this issue 4 years ago • 4 comments

  • 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 }, });
  • 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 Screenshot from 2021-05-25 11-13-49

Actual logs in console Screenshot from 2021-05-25 11-13-43

shreyas1496 avatar May 25 '21 06:05 shreyas1496

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.

glasser avatar May 26 '21 18:05 glasser

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. :^)

HilaryDev avatar May 30 '21 08:05 HilaryDev

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 :(.

rdgb avatar Aug 25 '21 10:08 rdgb

Hi there, I just posted a detailed description of the problem. Hope you find it useful until/if the issue gets fixed

Jaxolotl avatar Jun 22 '22 21:06 Jaxolotl