Replies: 1 comment
-
I've transferred this to the new datasource-rest repository. Our docs aren't great on this point and we do plan to actually improve this soon, but the RESTDataSource model is based around two layers of caching: "per-GraphQL-request" caching which doesn't care about In a sense, the former cache serves a similar purpose to (part of the functionality of) the |
Beta Was this translation helpful? Give feedback.
-
So I've been looking at different ways to implement caching for Apollo express server, and I noticed some documentation for RestDataSource that mentioned it automatically caches GET requests in the memoizedResults field as a performance optimization. However, upon further inspection, it doesn't look like this field is actually doing anything at all! memoizedResults is a class level variable, meaning that every new instance of a RestDataSource will make a completely new memoizedResults field. This is a problem, because best practices state that a new datasource should be created with every request, which is why we make factory methods for each datasource on server initialization. The point of caching is to ensure that multiple identical requests don't trigger multiple expensive http call operations, but if the cache in charge of determining whether or not a request is a duplicate has a lifespan of precisely 1 request, then it's not really serving its purpose as a cache, right? I almost filed a bug report, but according to apollographql/apollo-server#3935 this feature is working as intended, so I'm obviously missing something, right? How is the code and logic surrounding memoizedResults not just completely non-functional?
As a caching solution, the memoized cache in RestDataSource is wholly outclassed by apollo-server-plugin-response-cache, which in addition to actually working, also allows me to configure fields like cache lifespan and visibility. Both of these are pretty core apollo server libraries though, which wouldn't make sense if they both served the same purpose.
I made a small repository to serve as working demos for both the server plugin response cache and the RestDataSource memoization. You can find it here: https://github.com/sadehart/ApolloExpressPlayground
Thank you for taking the time to read this. I'm pretty convinced that I'm missing something crucial here, but for the life of me I can't figure it out.
-Scott
Beta Was this translation helpful? Give feedback.
All reactions