0.15.0 • Published 2 years ago

apollo-cache-control v0.15.0

Weekly downloads
1,262,613
License
MIT
Repository
github
Last release
2 years ago

Apollo Cache Control (for Node.js)

This package is used to collect and expose cache control data in the Apollo Cache Control format.

It relies on instrumenting a GraphQL schema to collect cache control hints, and exposes cache control data for an individual request under extensions as part of the GraphQL response.

This data can be consumed by Apollo Engine or any other tool to inform caching and visualize the cache policies that are in effect for a particular request.

Usage

Apollo Server

Apollo Server includes built-in support for Apollo Cache Control from version 1.2.0 onwards.

The only code change required is to add tracing: true and cacheControl: true to the options passed to the Apollo Server middleware function for your framework of choice. For example, for Express:

app.use('/graphql', bodyParser.json(), graphqlExpress({
  schema,
  context: {},
  tracing: true,
  cacheControl: true
}));

If you are using express-graphql, we recommend you switch to Apollo Server. Both express-graphql and Apollo Server are based on the graphql-js reference implementation, and switching should only require changing a few lines of code.

Add cache hints to your schema

Cache hints can be added to your schema using directives on your types and fields. When executing your query, these hints will be added to the response and interpreted by Engine to compute a cache policy for the response. Hints on fields override hints specified on the target type.

type Post @cacheControl(maxAge: 240) {
  id: Int!
  title: String
  author: Author
  votes: Int @cacheControl(maxAge: 30)
  readByCurrentUser: Boolean! @cacheControl(scope: PRIVATE)
}

If you need to add cache hints dynamically, you can use a programmatic API from within your resolvers.

const resolvers = {
  Query: {
    post: (_, { id }, _, { cacheControl }) => {
      cacheControl.setCacheHint({ maxAge: 60 });
      return find(posts, { id });
    }
  }
}

If you're using TypeScript, you need the following:

import 'apollo-cache-control';

If set up correctly, for this query:

query {
  post(id: 1) {
    title
    votes
    readByCurrentUser
  }
}

You should receive cache control data in the extensions field of your response:

"cacheControl": {
  "version": 1,
  "hints": [
    {
      "path": [
        "post"
      ],
      "maxAge": 240
    },
    {
      "path": [
        "post",
        "votes"
      ],
      "maxAge": 30
    },
    {
      "path": [
        "post",
        "readByCurrentUser"
      ],
      "scope": "PRIVATE"
    }
  ]
}
0.15.0

2 years ago

0.14.0

4 years ago

0.13.0

4 years ago

0.14.0-alpha.0

4 years ago

0.300.0-alpha.4

4 years ago

0.12.1-unified.0

4 years ago

0.13.0-alpha.2

4 years ago

0.13.0-alpha.1

4 years ago

0.13.0-alpha.0

4 years ago

0.12.0

4 years ago

0.12.0-alpha.0

4 years ago

0.11.6

4 years ago

0.11.5

4 years ago

0.11.4

4 years ago

0.11.4-alpha.0

4 years ago

0.11.3

4 years ago

0.11.2

4 years ago

0.11.2-rc.0

4 years ago

0.300.0-alpha.3

4 years ago

0.300.0-alpha.2

4 years ago

0.11.1

4 years ago

0.11.0

5 years ago

0.11.0-alpha.1

5 years ago

0.11.0-alpha.0

5 years ago

0.10.1-alpha.0

5 years ago

0.10.0

5 years ago

0.10.0-alpha.1

5 years ago

0.10.0-alpha.0

5 years ago

0.9.1

5 years ago

0.9.1-alpha.5

5 years ago

0.9.1-alpha.4

5 years ago

0.9.1-alpha.3

5 years ago

0.9.1-alpha.2

5 years ago

0.9.1-alpha.1

5 years ago

0.9.0

5 years ago

0.9.0-alpha.1

5 years ago

0.8.11

5 years ago

0.8.10

5 years ago

0.8.9

5 years ago

0.8.8

5 years ago

0.8.7

5 years ago

0.8.6

5 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.6-alpha.13

5 years ago

0.7.6-alpha.9

5 years ago

0.7.5

5 years ago

0.7.5-alpha.8

5 years ago

0.7.5-alpha.2

5 years ago

0.7.5-alpha.0

5 years ago

0.7.4

5 years ago

0.7.3-alpha.0

6 years ago

0.7.2

6 years ago

0.7.2-alpha.0

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.1

6 years ago

0.7.0-alpha.7

6 years ago

0.7.0-alpha.6

6 years ago

0.7.0-alpha.1

6 years ago

0.7.0-alpha.0

6 years ago

0.6.1-alpha.14

6 years ago

0.6.0

6 years ago

0.6.0-alpha.7

6 years ago

0.6.0-alpha.3

6 years ago

0.6.0-alpha.2

6 years ago

0.6.0-alpha.1

6 years ago

0.6.0-alpha.0

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.1-rc.0

6 years ago

0.5.0

6 years ago

0.5.0-alpha.3

6 years ago

0.4.1

6 years ago

0.5.0-alpha.2

6 years ago

0.5.0-alpha.1

6 years ago

0.5.0-alpha.0

6 years ago

0.4.0

6 years ago

0.3.4

6 years ago

0.4.0-alpha.0

6 years ago

0.3.4-alpha.0

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.3.0-alpha.3

6 years ago

0.3.0-alpha.2

6 years ago

0.3.0-alpha.1

6 years ago

0.3.0-alpha.0

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.2.0-rc.4

6 years ago

0.2.0-rc.3

6 years ago

0.2.0-rc.2

6 years ago

0.2.0-rc.1

6 years ago

0.2.0-rc.0

6 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.1

7 years ago