vuestorefront / storefront-api

Storefront GraphQL API Gateway. Modular architecture. ElasticSearch included. Works great with Magento1, Magento2, Spree, OpenCart, Pimcore and custom backends
https://storefrontapi.com
MIT License
8 stars 1 forks source link

Add data cache on the resolver level #12

Open pkarw opened 4 years ago

pkarw commented 4 years ago

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

ovidiul commented 4 years ago

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.'
    );
  }