2016-06-12 22:41:46 -07:00
|
|
|
import * as express from 'express';
|
2016-07-28 20:19:39 -07:00
|
|
|
import * as url from 'url';
|
2017-01-13 13:49:25 +02:00
|
|
|
import { GraphQLOptions, HttpQueryError, runHttpQuery } from 'graphql-server-core';
|
2016-10-18 09:36:07 +03:00
|
|
|
import * as GraphiQL from 'graphql-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
|
|
|
|
//
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-22 23:56:14 -07: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
|
2016-07-29 12:37:02 -07: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
|
|
|
|
2017-01-13 13:49:25 +02:00
|
|
|
return (req: express.Request, res: express.Response): 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.write(gqlResponse);
|
|
|
|
res.end();
|
|
|
|
}, (error: HttpQueryError) => {
|
2017-01-18 10:49:13 +02:00
|
|
|
if ( 'HttpQueryError' !== error.name ) {
|
2017-01-13 13:49:25 +02:00
|
|
|
throw error;
|
2016-07-05 14:19:14 -07:00
|
|
|
}
|
2016-06-28 21:11:57 -04:00
|
|
|
|
2017-01-13 13:49:25 +02:00
|
|
|
if ( error.headers ) {
|
|
|
|
Object.keys(error.headers).forEach((header) => {
|
|
|
|
res.setHeader(header, error.headers[header]);
|
|
|
|
});
|
2016-06-28 00:15:11 -04:00
|
|
|
}
|
2016-06-27 18:06:15 -04:00
|
|
|
|
2017-01-13 13:49:25 +02:00
|
|
|
res.statusCode = error.statusCode;
|
|
|
|
res.write(error.message);
|
2016-07-28 20:19:39 -07:00
|
|
|
res.end();
|
2017-01-13 13:49:25 +02:00
|
|
|
});
|
2016-06-15 20:35:48 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-29 15:33:24 -04:00
|
|
|
/* 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
|
|
|
|
*/
|
2016-06-29 15:42:32 -04:00
|
|
|
|
2016-07-06 11:45:20 -07:00
|
|
|
export function graphiqlExpress(options: GraphiQL.GraphiQLData) {
|
2017-01-13 13:49:25 +02:00
|
|
|
return (req: express.Request, res: express.Response) => {
|
2016-07-28 20:19:39 -07:00
|
|
|
const q = req.url && url.parse(req.url, true).query || {};
|
2016-06-27 16:14:49 -04:00
|
|
|
const query = q.query || '';
|
|
|
|
const operationName = q.operationName || '';
|
2016-06-26 15:59:15 -04:00
|
|
|
|
2016-06-18 10:19:51 -07:00
|
|
|
const graphiQLString = GraphiQL.renderGraphiQL({
|
2016-06-29 13:57:21 -04:00
|
|
|
endpointURL: options.endpointURL,
|
2017-02-28 20:05:45 +02:00
|
|
|
subscriptionsEndpoint: options.subscriptionsEndpoint,
|
2016-06-26 15:59:15 -04:00
|
|
|
query: query || options.query,
|
2016-12-26 15:39:44 -06:00
|
|
|
variables: q.variables && JSON.parse(q.variables) || options.variables,
|
2016-06-26 15:59:15 -04:00
|
|
|
operationName: operationName || options.operationName,
|
2016-09-12 18:07:35 -04:00
|
|
|
passHeader: options.passHeader,
|
2016-06-14 12:03:53 -07:00
|
|
|
});
|
2016-07-28 20:19:39 -07:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.write(graphiQLString);
|
|
|
|
res.end();
|
2016-06-14 12:03:53 -07:00
|
|
|
};
|
2016-06-12 22:41:46 -07:00
|
|
|
}
|