Skip to content

Commit

Permalink
All: allow cacheControl: {defaultMaxAge: 5}. (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser authored Mar 28, 2018
1 parent f59accf commit 7d1165f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All of the packages in the `apollo-server` repo are released with the same version numbers, so a new version of a particular package might not represent an actual change to that package. We generally try to mark changes that affect only one web server integration package with that package name, and don't specify package names for changes that affect most of the packages or which affect shared core packages.

### v1.3.4

* Upgrade to `[email protected]` and allow you to specify options to it (such as the new `defaultMaxAge`) by passing `cacheControl: {defaultMaxAge: 5}` instead of `cacheControl: true`.

### v1.3.3

* Updated peer dependencies to support `[email protected]`.
Expand Down
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,34 @@
},
"scripts": {
"compile": "lerna exec -- npm run compile",
"lint":
"prettier-check --ignore-path .gitignore \"{docs/{,source/**},.,packages/**,test}/{*.js,*.ts,*.md}\"",
"lint-fix":
"prettier --write --ignore-path .gitignore \"{docs/{,source/**},.,packages/**,test}/{*.js,*.ts,*.md}\"",
"lint": "prettier-check --ignore-path .gitignore \"{docs/{,source/**},.,packages/**,test}/{*.js,*.ts,*.md}\"",
"lint-fix": "prettier --write --ignore-path .gitignore \"{docs/{,source/**},.,packages/**,test}/{*.js,*.ts,*.md}\"",
"prebootstrap": "npm install",
"postinstall": "lerna bootstrap",
"pretest": "npm run compile",
"test": "npm run testonly --",
"posttest": "npm run lint",
"testonly":
"mocha --reporter spec --full-trace --timeout 5000 ./test/tests.js",
"coverage":
"istanbul cover -x \"*.test.js\" _mocha -- --timeout 5000 --full-trace --reporter dot ./test/tests.js",
"testonly": "mocha --reporter spec --full-trace --timeout 5000 ./test/tests.js",
"coverage": "istanbul cover -x \"*.test.js\" _mocha -- --timeout 5000 --full-trace --reporter dot ./test/tests.js",
"pretravis": "npm run compile",
"travis":
"istanbul cover -x \"*.test.js\" _mocha -- --timeout 5000 --full-trace ./test/tests.js",
"postcoverage":
"remap-istanbul --input coverage/coverage.raw.json --type lcovonly --output coverage/lcov.info",
"travis": "istanbul cover -x \"*.test.js\" _mocha -- --timeout 5000 --full-trace ./test/tests.js",
"postcoverage": "remap-istanbul --input coverage/coverage.raw.json --type lcovonly --output coverage/lcov.info",
"release": "lerna publish",
"precommit": "lint-staged"
},
"lint-staged": {
"*.ts": ["prettier --write", "git add"],
"*.js": ["prettier --write", "git add"],
"*.md": ["prettier --write", "git add"]
"*.ts": [
"prettier --write",
"git add"
],
"*.js": [
"prettier --write",
"git add"
],
"*.md": [
"prettier --write",
"git add"
]
},
"devDependencies": {
"@types/chai": "4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"definition": "dist/index.d.ts"
},
"dependencies": {
"apollo-cache-control": "^0.0.x",
"apollo-cache-control": "^0.1.0",
"apollo-tracing": "^0.1.0",
"graphql-extensions": "^0.0.x"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from 'graphql';
import { LogFunction } from './runQuery';
import { GraphQLExtension } from 'graphql-extensions';
import { CacheControlExtensionOptions } from 'apollo-cache-control';

/*
* GraphQLServerOptions
Expand Down Expand Up @@ -33,7 +34,7 @@ export interface GraphQLServerOptions {
fieldResolver?: GraphQLFieldResolver<any, any>;
debug?: boolean;
tracing?: boolean;
cacheControl?: boolean;
cacheControl?: boolean | CacheControlExtensionOptions;
}

export default GraphQLServerOptions;
Expand Down
11 changes: 8 additions & 3 deletions packages/apollo-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
GraphQLExtensionStack,
} from 'graphql-extensions';
import { TracingExtension } from 'apollo-tracing';
import { CacheControlExtension } from 'apollo-cache-control';
import {
CacheControlExtension,
CacheControlExtensionOptions,
} from 'apollo-cache-control';

export interface GraphQLResponse {
data?: object;
Expand Down Expand Up @@ -68,7 +71,7 @@ export interface QueryOptions {
formatResponse?: Function;
debug?: boolean;
tracing?: boolean;
cacheControl?: boolean;
cacheControl?: boolean | CacheControlExtensionOptions;
}

export function runQuery(options: QueryOptions): Promise<GraphQLResponse> {
Expand Down Expand Up @@ -115,8 +118,10 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
if (options.tracing) {
extensions.push(TracingExtension);
}
if (options.cacheControl) {
if (options.cacheControl === true) {
extensions.push(CacheControlExtension);
} else if (options.cacheControl) {
extensions.push(new CacheControlExtension(options.cacheControl));
}
const extensionStack =
extensions.length > 0 && new GraphQLExtensionStack(extensions);
Expand Down
78 changes: 66 additions & 12 deletions packages/apollo-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ import { GraphQLOptions } from 'apollo-server-core';
import * as GraphiQL from 'apollo-server-module-graphiql';
import { OperationStore } from 'apollo-server-module-operation-store';

const personType = new GraphQLObjectType({
name: 'PersonType',
fields: {
firstName: {
type: GraphQLString,
},
lastName: {
type: GraphQLString,
},
},
});

const queryType = new GraphQLObjectType({
name: 'QueryType',
fields: {
Expand All @@ -29,6 +41,12 @@ const queryType = new GraphQLObjectType({
return 'it works';
},
},
testPerson: {
type: personType,
resolve() {
return { firstName: 'Jane', lastName: 'Doe' };
},
},
testStringWithDelay: {
type: GraphQLString,
args: {
Expand Down Expand Up @@ -72,18 +90,6 @@ const queryType = new GraphQLObjectType({
},
});

const personType = new GraphQLObjectType({
name: 'PersonType',
fields: {
firstName: {
type: GraphQLString,
},
lastName: {
type: GraphQLString,
},
},
});

const mutationType = new GraphQLObjectType({
name: 'MutationType',
fields: {
Expand Down Expand Up @@ -349,6 +355,54 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
});
});

it('can handle a basic request with cacheControl', async () => {
app = await createApp({
graphqlOptions: { schema, cacheControl: true },
});
const expected = {
testPerson: { firstName: 'Jane' },
};
const req = request(app)
.post('/graphql')
.send({
query: 'query test{ testPerson { firstName } }',
});
return req.then(res => {
expect(res.status).to.equal(200);
expect(res.body.data).to.deep.equal(expected);
return expect(res.body.extensions).to.deep.equal({
cacheControl: {
version: 1,
hints: [{ maxAge: 0, path: ['testPerson'] }],
},
});
});
});

it('can handle a basic request with cacheControl and defaultMaxAge', async () => {
app = await createApp({
graphqlOptions: { schema, cacheControl: { defaultMaxAge: 5 } },
});
const expected = {
testPerson: { firstName: 'Jane' },
};
const req = request(app)
.post('/graphql')
.send({
query: 'query test{ testPerson { firstName } }',
});
return req.then(res => {
expect(res.status).to.equal(200);
expect(res.body.data).to.deep.equal(expected);
return expect(res.body.extensions).to.deep.equal({
cacheControl: {
version: 1,
hints: [{ maxAge: 5, path: ['testPerson'] }],
},
});
});
});

it('can handle a request with variables', async () => {
app = await createApp();
const expected = {
Expand Down

0 comments on commit 7d1165f

Please sign in to comment.