2018-06-20 16:21:50 -07:00
|
|
|
import * as express from 'express';
|
2018-01-09 00:08:01 +01:00
|
|
|
import {
|
|
|
|
GraphQLOptions,
|
|
|
|
HttpQueryError,
|
|
|
|
runHttpQuery,
|
2018-05-29 21:37:38 -07:00
|
|
|
convertNodeHttpToRequest,
|
2018-01-09 00:08:01 +01:00
|
|
|
} from 'apollo-server-core';
|
2016-06-15 20:35:48 -07:00
|
|
|
|
2016-10-22 23:56:14 -07:00
|
|
|
export interface ExpressGraphQLOptionsFunction {
|
2018-01-09 00:08:01 +01:00
|
|
|
(req?: express.Request, res?: express.Response):
|
|
|
|
| GraphQLOptions
|
|
|
|
| Promise<GraphQLOptions>;
|
2016-06-24 16:57:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Design principles:
|
|
|
|
// - there is just one way allowed: POST request with JSON body. Nothing else.
|
|
|
|
// - simple, fast and secure
|
|
|
|
//
|
2016-06-24 17:12:04 -04:00
|
|
|
|
2016-06-18 10:19:51 -07:00
|
|
|
export interface ExpressHandler {
|
|
|
|
(req: express.Request, res: express.Response, next): void;
|
|
|
|
}
|
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
export function graphqlExpress(
|
|
|
|
options: GraphQLOptions | ExpressGraphQLOptionsFunction,
|
|
|
|
): ExpressHandler {
|
2016-06-12 22:41:46 -07:00
|
|
|
if (!options) {
|
2016-06-24 16:57:52 -04:00
|
|
|
throw new Error('Apollo Server requires options.');
|
2016-06-12 22:41:46 -07:00
|
|
|
}
|
2016-06-14 12:03:53 -07:00
|
|
|
|
2016-06-12 22:41:46 -07:00
|
|
|
if (arguments.length > 1) {
|
2016-06-24 16:57:52 -04:00
|
|
|
// TODO: test this
|
2018-01-09 00:08:01 +01:00
|
|
|
throw new Error(
|
|
|
|
`Apollo Server expects exactly one argument, got ${arguments.length}`,
|
|
|
|
);
|
2016-06-12 22:41:46 -07:00
|
|
|
}
|
2016-06-14 12:03:53 -07:00
|
|
|
|
2018-04-04 22:56:07 +03:00
|
|
|
const graphqlHandler = (
|
|
|
|
req: express.Request,
|
|
|
|
res: express.Response,
|
|
|
|
next,
|
|
|
|
): void => {
|
2017-01-13 13:49:25 +02:00
|
|
|
runHttpQuery([req, res], {
|
|
|
|
method: req.method,
|
|
|
|
options: options,
|
|
|
|
query: req.method === 'POST' ? req.body : req.query,
|
2018-05-29 21:37:38 -07:00
|
|
|
request: convertNodeHttpToRequest(req),
|
2018-01-09 00:08:01 +01:00
|
|
|
}).then(
|
2018-06-21 13:29:14 -07:00
|
|
|
({ graphqlResponse, responseInit }) => {
|
|
|
|
Object.keys(responseInit.headers).forEach(key =>
|
|
|
|
res.setHeader(key, responseInit.headers[key]),
|
2018-04-11 11:17:55 +03:00
|
|
|
);
|
2018-06-21 13:29:14 -07:00
|
|
|
res.write(graphqlResponse);
|
2018-01-09 00:08:01 +01:00
|
|
|
res.end();
|
|
|
|
},
|
|
|
|
(error: HttpQueryError) => {
|
|
|
|
if ('HttpQueryError' !== error.name) {
|
|
|
|
return next(error);
|
|
|
|
}
|
2016-06-28 21:11:57 -04:00
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
if (error.headers) {
|
|
|
|
Object.keys(error.headers).forEach(header => {
|
|
|
|
res.setHeader(header, error.headers[header]);
|
|
|
|
});
|
|
|
|
}
|
2016-06-27 18:06:15 -04:00
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
res.statusCode = error.statusCode;
|
|
|
|
res.write(error.message);
|
|
|
|
res.end();
|
|
|
|
},
|
|
|
|
);
|
2016-06-15 20:35:48 -07:00
|
|
|
};
|
2018-04-04 22:56:07 +03:00
|
|
|
|
|
|
|
return graphqlHandler;
|
2016-06-15 20:35:48 -07:00
|
|
|
}
|