question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disable cache in ApolloServer when using RESTDataSource

See original GitHub issue

I am using RESTDataSource to fetch data from REST api’s but data is being cached.

How to disable cache since data keeps changing in thequery resolver.

Below is the node.js code.

   const app = express();
    
    const server = new ApolloServer({
      schema: schema,
      context: ({ req }) => ({
        token: req.headers.authorization,
      }),
      dataSources: () => apiDataSources,
    });
    
    server.applyMiddleware({ app });
    
    app.listen({ port: 4000 }, () => {
      console.log("Server is at http://localhost:4000/graphql");
    });

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:33 (4 by maintainers)

github_iconTop GitHub Comments

17reactions
MakhBethcommented, May 9, 2019

In my case, the issue was not the cache of the RESTDataSource class but a wrong initialization of the dataSources inside the new ApolloServer call:

export async function getApolloServer() {
  const schema = await buildSchema()
  const dataSources = await getDataSources()
  const apolloServer = new ApolloServer({
    schema,
    dataSources:  () => dataSources,
...

This was wrong because the dataSources variable was created once, when the ApolloServer started, so the cache was common to all the requests and not only per one request.

The solution for me was to initialize a new dataSources like written inside the doc

dataSources: () => {
    return {
      moviesAPI: new MoviesAPI(),
      personalizationAPI: new PersonalizationAPI(),
    };
  },

Hope this will help someone 😃

14reactions
estevaolucascommented, Nov 10, 2020

It’s important to remove the cache for error responses as well, otherwise you will experience unexpected results.

class NoCacheSource extends RESTDataSource {
  deleteCacheForRequest(request: Request) {
    this.memoizedResults.delete(this.cacheKeyFor(request));
  }

  didReceiveResponse(response: Response, request: Request) {
    this.deleteCacheForRequest(request);
    return super.didReceiveResponse(response, request);
  }

  didEncounterError(error: Error, request: Request) {
    this.deleteCacheForRequest(request);
    return super.didEncounterError(error, request);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-side caching - Apollo GraphQL Docs
When Apollo Server resolves an operation, it calculates the result's correct cache behavior based on the most restrictive settings among the result's fields ......
Read more >
How to invalidate cache in Apollo Server RESTDataSource
It seems like the best solution I can find, is to just leave the HTTP cache layer alone, and use a separate cache...
Read more >
Demystify apollo datasource cache | by Ron Liu - Medium
This article is trying to demystify this question. When we develop graphql server using apollo server with REST api as the backend, we...
Read more >
How Apollo REST Data Source Deduplicates and Caches API ...
How you can use the apollo-server-caching library to implement your own caching logic. Resolving data from a RESTful API. If you're thinking ...
Read more >
Frequently Asked Questions - Apollo GraphQL Docs
The RESTDataSource is a tool that integrates with apollo-server to ... On the client, caches can prevent multiple queries from being called when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found