Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apollo-cache-control

Package Overview
Dependencies
Maintainers
1
Versions
118
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-cache-control

A GraphQL extension for cache control

  • 0.11.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is apollo-cache-control?

The apollo-cache-control npm package is used to provide fine-grained cache control for GraphQL responses in Apollo Server. It allows developers to specify cache hints directly in the schema or resolvers, which can then be used to control the caching behavior of responses.

What are apollo-cache-control's main functionalities?

Setting Cache Hints in Schema

This feature allows you to set cache hints directly in the GraphQL schema using the @cacheControl directive. In this example, the 'books' query has a cache hint with a maxAge of 60 seconds.

const { ApolloServer, gql } = require('apollo-server');
const { ApolloServerPluginCacheControl } = require('apollo-server-core');

const typeDefs = gql`
  type Query {
    books: [Book] @cacheControl(maxAge: 60)
  }

  type Book {
    title: String
    author: String
  }
`;

const resolvers = {
  Query: {
    books: () => [
      { title: 'The Awakening', author: 'Kate Chopin' },
      { title: 'City of Glass', author: 'Paul Auster' }
    ]
  }
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginCacheControl({ defaultMaxAge: 5 })]
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

Setting Cache Hints in Resolvers

This feature allows you to set cache hints programmatically within resolvers. In this example, the 'books' resolver sets a cache hint with a maxAge of 60 seconds using the context.cacheControl.setCacheHint method.

const { ApolloServer, gql } = require('apollo-server');
const { ApolloServerPluginCacheControl } = require('apollo-server-core');

const typeDefs = gql`
  type Query {
    books: [Book]
  }

  type Book {
    title: String
    author: String
  }
`;

const resolvers = {
  Query: {
    books: (parent, args, context) => {
      context.cacheControl.setCacheHint({ maxAge: 60 });
      return [
        { title: 'The Awakening', author: 'Kate Chopin' },
        { title: 'City of Glass', author: 'Paul Auster' }
      ];
    }
  }
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginCacheControl({ defaultMaxAge: 5 })]
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

Other packages similar to apollo-cache-control

FAQs

Package last updated on 27 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc