vuestorefront/storefront-api

Add data cache on the resolver level

pkarw opened this issue · 1 comments

pkarw commented

What is the motivation for adding/enhancing this feature?

At the moment, the SSR caching is supposed to work with the REST catalog endpoint only

The same output cache - including tagging - should be added on resolvers level

What are the acceptance criteria

  • SSR cache added to graphQL resolvers for key entities: product, category

Apollo Graphql Server has a caching mechanism that can be implemented with a cache server like Redis https://www.apollographql.com/docs/apollo-server/performance/caching/ , would this be a possible solution for this issue, from my tests, it could be implemented with minimal changes across all queries.

Screenshot 2020-02-20 at 15 01 00

Screenshot 2020-02-20 at 15 47 01

The config with redis would look something like this, by modifying the ApolloServer setup in the src/index.ts file:

import responseCachePlugin from 'apollo-server-plugin-response-cache';
const { RedisCache } = require('apollo-server-cache-redis');

if (config.get('server.useOutputCache')) {
    useQueryCache = {
      cache: new RedisCache(`//${config.get('redis.host')}:${config.get('redis.port')}`),
      cacheControl: {
        defaultMaxAge: config.get('server.outputCacheDefaultTtl')
      },
      plugins: [responseCachePlugin()]
    }
  }

 if (aggregatedGraphqlConfig.hasGraphqlSupport) {
    const server = new ApolloServer({
      typeDefs: mergeTypes(aggregatedGraphqlConfig.schema, { all: true }),
      resolvers: aggregatedGraphqlConfig.resolvers,
      rootValue: global,
      playground: true,
      ...useQueryCache,
      context: integrationContext => integrationContext
    });
    server.applyMiddleware({ app, path: '/graphql' });
  } else {
    console.info(
      'No GraphQL Support enabled. Please provide at least one module supporting graphQL schema.'
    );
  }