apollo-server/packages/apollo-server-express/src/expressApollo.ts

102 lines
2.9 KiB
TypeScript
Raw Normal View History

2016-06-12 22:41:46 -07:00
import * as express from 'express';
import * as url from 'url';
import {
GraphQLOptions,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import * as GraphiQL from 'apollo-server-module-graphiql';
2016-06-15 20:35:48 -07:00
2016-10-22 23:56:14 -07:00
export interface ExpressGraphQLOptionsFunction {
(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
//
export interface ExpressHandler {
(req: express.Request, res: express.Response, next): void;
}
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-12 22:41:46 -07:00
if (arguments.length > 1) {
2016-06-24 16:57:52 -04:00
// TODO: test this
throw new Error(
`Apollo Server expects exactly one argument, got ${arguments.length}`,
);
2016-06-12 22:41:46 -07:00
}
return (req: express.Request, res: express.Response, next): void => {
runHttpQuery([req, res], {
method: req.method,
options: options,
query: req.method === 'POST' ? req.body : req.query,
}).then(
gqlResponse => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', Buffer.byteLength(gqlResponse, 'utf8'));
res.write(gqlResponse);
res.end();
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
return next(error);
}
2016-06-28 21:11:57 -04:00
if (error.headers) {
Object.keys(error.headers).forEach(header => {
res.setHeader(header, error.headers[header]);
});
}
2016-06-27 18:06:15 -04:00
res.statusCode = error.statusCode;
res.write(error.message);
res.end();
},
);
2016-06-15 20:35:48 -07:00
};
}
export interface ExpressGraphiQLOptionsFunction {
(req?: express.Request):
| GraphiQL.GraphiQLData
| Promise<GraphiQL.GraphiQLData>;
}
/* This middleware returns the html for the GraphiQL interactive query UI
*
* GraphiQLData arguments
*
* - endpointURL: the relative or absolute URL for the endpoint which GraphiQL will make queries to
* - (optional) query: the GraphQL query to pre-fill in the GraphiQL UI
* - (optional) variables: a JS object of variables to pre-fill in the GraphiQL UI
* - (optional) operationName: the operationName to pre-fill in the GraphiQL UI
* - (optional) result: the result of the query to pre-fill in the GraphiQL UI
*/
export function graphiqlExpress(
options: GraphiQL.GraphiQLData | ExpressGraphiQLOptionsFunction,
) {
return (req: express.Request, res: express.Response, next) => {
const query = req.url && url.parse(req.url, true).query;
GraphiQL.resolveGraphiQLString(query, options, req).then(
graphiqlString => {
res.setHeader('Content-Type', 'text/html');
res.write(graphiqlString);
res.end();
},
error => next(error),
);
};
2016-06-12 22:41:46 -07:00
}