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.

apollo-server-plugin-response-cache: cache hint not working

See original GitHub issue

My apollo graphql server setup uses the following:

    "apollo-datasource-rest": "^0.6.9",
    "apollo-server-express": "^2.9.12",
    "apollo-server-plugin-response-cache": "^0.3.8",

My schema:

type Query {
  types: [Type!]! @cacheControl(maxAge: 3600)
}

type Type {
  id: ID!
  label: String!
  active: Boolean!
}

My responseCachePlugin config:

responseCachePlugin({
    sessionId: (requestContext) => (requestContext.request.http.headers.get('sessionId') || null),
  })

Neither the static cache hint like the above nor the dynamic cache hint in the resolver work. i.e., they don’t cache the response from the datasource (REST API).

A bunch of other devs reported this issue on Spectrum. One of the Spectrum posts points to the problem:

The property requestContext.overallCachePolicy doesn't get updated by the cache hint, nither the static one in schema or the dynamic one ...
The value is always the cacheControl you configured when creating ApolloServer

github link to the line in the code where there is a potential problem: https://github.com/apollographql/apollo-server/blob/1acf62d903253f5669c0c4f7255c8a87b95b5e6c/packages/apollo-server-plugin-response-cache/src/ApolloServerPluginResponseCache.ts#L243

Spectrum post that details the above issue: https://spectrum.chat/apollo/apollo-server/apollo-server-plugin-response-cache-is-not-working-properly~e3390cd4-9e3f-4919-88b4-92508e235e38

Other Spectrum posts that talk about this problem:

  1. https://spectrum.chat/apollo/apollo-server/cachecontrol-does-not-work-unless-defaultmaxage-is-set~0e4dc3c5-7cdc-40be-b833-0b2d436d1288
  2. https://spectrum.chat/apollo/apollo-server/does-rediscache-work-in-dev-mode~18358268-8d11-4b13-a0c9-abddfb6623f3

cc to: @trevor-scheer

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:37
  • Comments:64 (17 by maintainers)

github_iconTop GitHub Comments

25reactions
leonardoanalistacommented, Dec 24, 2019

It’s disappointing the cache only works if you set a global caching settings:

  const server = new ApolloServer({
    cacheControl: {
       defaultMaxAge: 50,
    },
    plugins: [responseCachePlugin()],
    cache: new RedisCache({
      host: '127.0.0.1',
      namespace: 'apollo-cache:',
    }),

I am using merge-graphql-schemas with Redis and I am unable to get anything cached without the global settings.

I think it is a great idea to @cacheControl in the schema but it would be even better it it works first.

12reactions
leonardoanalistacommented, Jan 28, 2020

It is documented correctly but the result is not something usable. If you can’t set a overall cache, the cache doesn’t work at all. This is basically the issue. I wonder if someone from Apollo actively looks at the issues?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-side caching - Apollo GraphQL Docs
Our philosophy behind Apollo Server caching is that a response should only be considered cacheable if every part of that response opts in...
Read more >
API Reference: Cache control plugin - Apollo GraphQL Docs
This plugin enables your GraphQL server to specify a cache policy at the field level, either statically in your schema with the @cacheControl...
Read more >
Server-side caching - Apollo GraphQL Docs
You can cache Apollo Server query responses in stores like Redis, Memcached, or Apollo Server's in-memory cache. In-memory cache setup. To set up...
Read more >
responseCachePlugin() is not a function - Apollo Community
Hi, I am trying to learn GraphQL and how to use the server side caching but when I import responseCachePlugin and add it...
Read more >
Automatic persisted queries - Apollo GraphQL Docs
A persisted query is a query string that's cached on the server side, ... thus reducing request sizes dramatically (response sizes are unaffected)....
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