apollo-server/packages/apollo-server-express/src/expressApollo.ts
Evans Hauser 65d7b100e4
CDN cache-control headers (#1138)
* core: return response object from runHttpQuery

* core: change gqlResponse to graphqlResponse and add custom RequestInit type

* core: add cache-control headers based on the calcualted maxAge

* core: add extensions check during cache-control header creation

* core: create headers when cacheControl is not enabled otherwise pass through extensions

* express: initial tests of CDN cach-contol headers

* core: fixed tests with applyMiddleware and pass cacheControl config

* core: cache hint fixes, ignore when no maxAge, and check for rootKeys

* core: check for hints of length 0

* core: node 10 fails file upload test for some stream reason

* docs: add cdn caching section to features

* add space after // in comments

* fix feedback: proxy alignment and response creation

Adds cache-control toggles for http header calculation and stripping out
the cache control extensions from the respose.

Brings the default calculation of headers in line with the proxy.

* fix links in comments

* fix tests with null dereference

* update cdn docs and migration guide to include latest cdn configuration

* add not for engine migration to set engine to false

* add engine set to false in migration guide

* express: fixed tests

* address feedback to use omit and documentation

* docs: cdn caching is alternative to full response caching

* add back epipe check in upload tests
2018-06-21 13:29:14 -07:00

75 lines
1.8 KiB
TypeScript

import * as express from 'express';
import {
GraphQLOptions,
HttpQueryError,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';
export interface ExpressGraphQLOptionsFunction {
(req?: express.Request, res?: express.Response):
| GraphQLOptions
| Promise<GraphQLOptions>;
}
// Design principles:
// - there is just one way allowed: POST request with JSON body. Nothing else.
// - simple, fast and secure
//
export interface ExpressHandler {
(req: express.Request, res: express.Response, next): void;
}
export function graphqlExpress(
options: GraphQLOptions | ExpressGraphQLOptionsFunction,
): ExpressHandler {
if (!options) {
throw new Error('Apollo Server requires options.');
}
if (arguments.length > 1) {
// TODO: test this
throw new Error(
`Apollo Server expects exactly one argument, got ${arguments.length}`,
);
}
const graphqlHandler = (
req: express.Request,
res: express.Response,
next,
): void => {
runHttpQuery([req, res], {
method: req.method,
options: options,
query: req.method === 'POST' ? req.body : req.query,
request: convertNodeHttpToRequest(req),
}).then(
({ graphqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.write(graphqlResponse);
res.end();
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
return next(error);
}
if (error.headers) {
Object.keys(error.headers).forEach(header => {
res.setHeader(header, error.headers[header]);
});
}
res.statusCode = error.statusCode;
res.write(error.message);
res.end();
},
);
};
return graphqlHandler;
}