Skip to content

Commit

Permalink
Import apollo-cache-control-js (#1294)
Browse files Browse the repository at this point in the history
* Initial commit

* Add .npmignore to avoid ignoring lib when publishing

* 0.0.2

* Update graphql-extensions dependency to 0.0.2

* 0.0.3

* Reorganize code and add tests

* 0.0.4

* Add tests and reorganize code

* 0.0.5

* Update dependency to graphql-extensions 0.0.4

* 0.0.6

* Update graphql-extensions dependency and downgrade TS target

* 0.0.7

* Update README

* Update README

* Update README

* Update README

* Add import instructions for TypeScript to README (#7)

* Add cache hints when the directive is defined on interfaces (#10)

* Increase version range for `graphql` peerDependency. (#12)

* 0.0.8

* Update dependencies

* 0.0.9

* [apollo-bot] Update the Issue/PR Templates with auto label (#13)

* [apollo-bot] Update the Templates with docs label (#15)

* Add cache hints to info.cacheControl (#16)

This exposes the `cacheControl` hints on the schema, to the resolvers at runtime.

* Update `graphql` peer dependency range to allow 0.13.x.

* dev: Update TypeScript to latest version, v2.7.2.

* dev: Update jest & dependencies to latest versions.

* dev: Update type definitions for `graphql`, `node` and `jest`.

* dev: Update `graphql` to latest version, v0.13.2.

* 0.0.10

* Add defaultMaxAge option

This is an easy way to quickly say "I want everything in my schema to be cached
for 5 seconds". You can override it with specific maxAges on specific fields or
types.

An upcoming release of apollo-server-* will allow you to specify options to
cacheControl.

* Add CircleCI config

* Add partial CHANGELOG

* 0.1.0

* Honor overwrite default maxAge with maxAge=0 (Fixes #22) (#23)

* 0.1.1

* Allow default caching to apply to interfaces

* remove unused files

* update versions and use a-s style config

* run prettier

* fix tests and typing
  • Loading branch information
evans authored Jul 3, 2018
1 parent 8496387 commit 19c8adf
Show file tree
Hide file tree
Showing 10 changed files with 6,741 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/apollo-cache-control-js/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__test__
17 changes: 17 additions & 0 deletions packages/apollo-cache-control-js/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

### v0.1.1

* Fix `defaultMaxAge` feature (introduced in 0.1.0) so that `maxAge: 0` overrides the default, as previously documented.

### v0.1.0

* **New feature**: New `defaultMaxAge` constructor option. (`apollo-server-*` will be updated to allow you to pass constructor options to the extension.)


### v0.0.10

* Update peer dependencies to support `graphql@0.13`.
* Expose `context.cacheControl.cacheHint` to resolvers.

(Older versions exist but have no CHANGELOG entries.)
115 changes: 115 additions & 0 deletions packages/apollo-cache-control-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Apollo Cache Control (for Node.js)

This package is used to collect and expose cache control data in the [Apollo Cache Control](https://github.com/apollographql/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](https://www.apollographql.com/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:

```javascript
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`](https://github.com/graphql/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.

```graphql
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.

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

If you're using TypeScript, you need the following:
```javascript
import 'apollo-cache-control';
```

If set up correctly, for this query:

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

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

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

### Setting a default maxAge

The power of cache hints comes from being able to set them precisely to different values on different types and fields based on your understanding of your implementation's semantics. But when getting started with Apollo Cache Control, you might just want to apply the same `maxAge` to most of your resolvers. You can specify a default max age when you set up `cacheControl` in your server. This max age will be applied to all resolvers which don't explicitly set `maxAge` via schema hints (including schema hints on the type that they return) or the programmatic API. You can override this for a particular resolver or type by setting `@cacheControl(maxAge: 0)`. For example, for Express:

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

0 comments on commit 19c8adf

Please sign in to comment.