2016-06-12 22:41:46 -07:00
|
|
|
import * as express from 'express';
|
|
|
|
import * as graphql from 'graphql';
|
2016-07-28 20:19:39 -07:00
|
|
|
import * as url from 'url';
|
2016-10-22 23:56:14 -07:00
|
|
|
import { GraphQLOptions, runQuery } 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
|
|
|
|
2016-06-27 22:19:57 -04:00
|
|
|
return async (req: express.Request, res: express.Response, next) => {
|
2016-10-22 23:56:14 -07:00
|
|
|
let optionsObject: GraphQLOptions;
|
2016-06-24 16:57:52 -04:00
|
|
|
if (isOptionsFunction(options)) {
|
2016-07-05 14:19:14 -07:00
|
|
|
try {
|
2016-07-29 10:20:29 -07:00
|
|
|
optionsObject = await options(req, res);
|
2016-07-05 14:19:14 -07:00
|
|
|
} catch (e) {
|
2016-07-28 20:19:39 -07:00
|
|
|
res.statusCode = 500;
|
|
|
|
res.write(`Invalid options provided to ApolloServer: ${e.message}`);
|
|
|
|
res.end();
|
2016-07-05 14:19:14 -07:00
|
|
|
}
|
2016-06-24 16:57:52 -04:00
|
|
|
} else {
|
|
|
|
optionsObject = options;
|
|
|
|
}
|
|
|
|
|
2016-06-28 21:11:57 -04:00
|
|
|
const formatErrorFn = optionsObject.formatError || graphql.formatError;
|
2016-11-14 23:46:09 +02:00
|
|
|
let requestPayload;
|
2016-06-28 21:11:57 -04:00
|
|
|
|
2016-10-20 21:16:53 +03:00
|
|
|
switch ( req.method ) {
|
|
|
|
case 'POST':
|
|
|
|
if ( !req.body ) {
|
|
|
|
res.statusCode = 500;
|
|
|
|
res.write('POST body missing. Did you forget "app.use(bodyParser.json())"?');
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-14 23:46:09 +02:00
|
|
|
requestPayload = req.body;
|
2016-10-20 21:16:53 +03:00
|
|
|
break;
|
|
|
|
case 'GET':
|
2016-12-16 15:46:57 +02:00
|
|
|
if ( !req.query || (Object.keys(req.query).length === 0) ) {
|
|
|
|
res.statusCode = 400;
|
2016-10-20 21:16:53 +03:00
|
|
|
res.write('GET query missing.');
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
2016-06-27 18:06:15 -04:00
|
|
|
|
2016-11-14 23:46:09 +02:00
|
|
|
requestPayload = req.query;
|
2016-10-20 21:16:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
res.setHeader('Allow', 'GET, POST');
|
|
|
|
res.statusCode = 405;
|
|
|
|
res.write('Apollo Server supports only GET/POST requests.');
|
|
|
|
res.end();
|
|
|
|
return;
|
2016-06-24 16:57:52 -04:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:15:11 -04:00
|
|
|
let isBatch = true;
|
2016-06-27 21:58:22 -04:00
|
|
|
// TODO: do something different here if the body is an array.
|
|
|
|
// Throw an error if body isn't either array or object.
|
2016-11-14 23:46:09 +02:00
|
|
|
if (!Array.isArray(requestPayload)) {
|
2016-06-28 00:15:11 -04:00
|
|
|
isBatch = false;
|
2016-11-14 23:46:09 +02:00
|
|
|
requestPayload = [requestPayload];
|
2016-06-26 21:06:37 -04:00
|
|
|
}
|
2016-06-24 16:57:52 -04:00
|
|
|
|
2017-01-04 18:10:30 -08:00
|
|
|
let responses: Array<graphql.ExecutionResult> = [];
|
2016-11-14 23:46:09 +02:00
|
|
|
for (let requestParams of requestPayload) {
|
2016-06-28 21:11:57 -04:00
|
|
|
try {
|
|
|
|
const query = requestParams.query;
|
|
|
|
const operationName = requestParams.operationName;
|
|
|
|
let variables = requestParams.variables;
|
|
|
|
|
|
|
|
if (typeof variables === 'string') {
|
2016-09-09 20:30:27 -05:00
|
|
|
try {
|
|
|
|
variables = JSON.parse(variables);
|
|
|
|
} catch (error) {
|
|
|
|
res.statusCode = 400;
|
|
|
|
res.write('Variables are invalid JSON.');
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
2016-06-28 21:11:57 -04:00
|
|
|
}
|
|
|
|
|
2016-10-20 21:52:42 -07:00
|
|
|
// Shallow clone context for queries in batches. This allows
|
|
|
|
// users to distinguish multiple queries in the batch and to
|
|
|
|
// modify the context object without interfering with each other.
|
|
|
|
let context = optionsObject.context;
|
|
|
|
if (isBatch) {
|
|
|
|
context = Object.assign({}, context || {});
|
|
|
|
}
|
|
|
|
|
2016-06-28 21:11:57 -04:00
|
|
|
let params = {
|
|
|
|
schema: optionsObject.schema,
|
|
|
|
query: query,
|
|
|
|
variables: variables,
|
2016-10-20 21:52:42 -07:00
|
|
|
context: context,
|
2016-06-28 21:11:57 -04:00
|
|
|
rootValue: optionsObject.rootValue,
|
|
|
|
operationName: operationName,
|
|
|
|
logFunction: optionsObject.logFunction,
|
|
|
|
validationRules: optionsObject.validationRules,
|
|
|
|
formatError: formatErrorFn,
|
|
|
|
formatResponse: optionsObject.formatResponse,
|
2016-09-12 15:02:41 -07:00
|
|
|
debug: optionsObject.debug,
|
2016-06-28 21:11:57 -04:00
|
|
|
};
|
|
|
|
|
2016-06-29 15:42:32 -04:00
|
|
|
if (optionsObject.formatParams) {
|
|
|
|
params = optionsObject.formatParams(params);
|
2016-06-28 21:11:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
responses.push(await runQuery(params));
|
|
|
|
} catch (e) {
|
|
|
|
responses.push({ errors: [formatErrorFn(e)] });
|
2016-06-28 00:15:11 -04:00
|
|
|
}
|
2016-06-27 18:06:15 -04:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:19:39 -07:00
|
|
|
res.setHeader('Content-Type', 'application/json');
|
2016-06-28 00:15:11 -04:00
|
|
|
if (isBatch) {
|
2016-07-28 20:19:39 -07:00
|
|
|
res.write(JSON.stringify(responses));
|
|
|
|
res.end();
|
2016-06-28 00:15:11 -04:00
|
|
|
} else {
|
|
|
|
const gqlResponse = responses[0];
|
2016-06-27 18:06:15 -04:00
|
|
|
if (gqlResponse.errors && typeof gqlResponse.data === 'undefined') {
|
2016-07-28 20:19:39 -07:00
|
|
|
res.statusCode = 400;
|
2016-06-24 16:57:52 -04:00
|
|
|
}
|
2016-07-28 20:19:39 -07:00
|
|
|
res.write(JSON.stringify(gqlResponse));
|
|
|
|
res.end();
|
2016-06-28 00:15:11 -04:00
|
|
|
}
|
|
|
|
|
2016-06-15 20:35:48 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-22 23:56:14 -07:00
|
|
|
function isOptionsFunction(arg: GraphQLOptions | ExpressGraphQLOptionsFunction): arg is ExpressGraphQLOptionsFunction {
|
2016-06-24 16:57:52 -04:00
|
|
|
return typeof arg === 'function';
|
2016-06-17 15:45:35 -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) {
|
2016-06-15 20:35:48 -07:00
|
|
|
return (req: express.Request, res: express.Response, next) => {
|
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 variables = q.variables || '{}';
|
|
|
|
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,
|
2016-06-26 15:59:15 -04:00
|
|
|
query: query || options.query,
|
|
|
|
variables: JSON.parse(variables) || options.variables,
|
|
|
|
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
|
|
|
}
|