Quick Tip: Understanding Apollo Rest Datasource Cache

CSP
2 min readFeb 5, 2022
Photo by Olav Ahrens Røtne on Unsplash

Typically we use RESTDataSource for fetching data from a REST API and exposing it via GraphQL within Apollo Server. One of the key benefits of this is that it provides in-memory caching.

Here is the code snippet taken from apollo-datasource-rest code:

if (request.method === 'GET') {    let promise = this.memoizedResults.get(cacheKey);    if (promise) return promise;    promise = performRequest();    this.memoizedResults.set(cacheKey, promise);    return promise;} else ...

Looking at it, it appears that all the GET calls are cached which seems like a problem if you need control on what to cache and not. So how come we don't see stale (or event wrong data) data when calling queries multiple times for the same endpoint.

You don’t see this issue and in fact, are benefiting from this caching because of this:

const server = new ApolloServer({
...
dataSources: () => ({
myapi: new MyApi() // new instance
}),
...
});

Every time a query is executed the dataSources callback provides a new instance of the service for that execution phase so if multiple GET API calls are being made to the same endoint it will be served from the cache, which makes sense.

Imagine, instead of the above if you would have mistakenly done this:

const myApiInstance= new MyApi();const server = new ApolloServer({
...
dataSources: () => ({
myapi: myApiInstance //Problem!!!
}),
...
});

You will end up caching the data for all GET requests for all queries which fetch data from the same endpoint.

Hope this quick post helps!

--

--

CSP

Striving to become my better self. I write on well researched thought provoking topics.