Apollo-server: cacheControl / apollo-server-cache-redis not triggered

Created on 8 Aug 2019  路  2Comments  路  Source: apollographql/apollo-server

"apollo-datasource-rest": "0.6.1",
"apollo-server-cache-redis": "1.1.1",
"apollo-server-express": "2.8.1"

I'm unable to get caching to work + cacheControl is not available in the resolvers. No keys are ever created in Redis either.

I can see that a SELECT and INFO are performed at start and a rare MGET operation for some requests.

Server Initialization

const { ApolloServer } = require('apollo-server-express')
const { RedisCache } = require('apollo-server-cache-redis')

new ApolloServer({
  typeDefs,
  resolvers,
  schemaDirectives,
  cacheControl: {
    defaultMaxAge: 60
  },
  cache: new RedisCache({ host: '...' }),
  context: ({ req, res }) => ({ req, res }),
  dataSources: () => ({
    exampleAPI: new ExampleAPI()
  })
})

Schema

type Query {
  search(
    firstName: String
    lastName: String
  ): [UserSearchResult] @cacheControl(maxAge: 120)
}

type UserSearchResult @cacheControl(maxAge: 240) {
  id: Int!
  email: String!
  firstName: String
  lastName: String
}

Resolver

async search (_source, args, { cacheControl, dataSources }) {
  // cacheControl.setCacheHint({ maxAge: 120, scope: 'PUBLIC' })
  return dataSources.exampleAPI.searchMember(args)
}

In the above code, cacheControl is always undefined.

No keys are ever created in Redis and the REST endpoint is always called on every request.

I closely followed the docs [1] [2] but I can't get anything to work. Is something broken or am I missing something obvious?

Most helpful comment

Isn't cacheControl on the fourth parameter of a resolver? Looks like you are trying to destructure it from the context.
https://www.apollographql.com/docs/apollo-server/features/caching/#adding-cache-hints-dynamically-in-your-resolvers

All 2 comments

Isn't cacheControl on the fourth parameter of a resolver? Looks like you are trying to destructure it from the context.
https://www.apollographql.com/docs/apollo-server/features/caching/#adding-cache-hints-dynamically-in-your-resolvers

See @rizowski's suggestion above. 馃槃

(Thank you, @rizowski!)

Was this page helpful?
0 / 5 - 0 ratings