2016-06-12 22:41:46 -07:00
|
|
|
import * as express from 'express';
|
|
|
|
import * as graphql from 'graphql';
|
2016-06-14 12:03:53 -07:00
|
|
|
import { runQuery } from '../core/runQuery';
|
2016-06-12 22:41:46 -07:00
|
|
|
|
2016-06-18 10:19:51 -07:00
|
|
|
import * as GraphiQL from '../modules/renderGraphiQL';
|
2016-06-15 20:35:48 -07:00
|
|
|
|
|
|
|
// TODO: will these be the same or different for other integrations?
|
|
|
|
export interface ExpressApolloOptions {
|
2016-06-12 22:41:46 -07:00
|
|
|
schema: graphql.GraphQLSchema;
|
2016-06-15 20:35:48 -07:00
|
|
|
formatError?: Function;
|
|
|
|
rootValue?: any;
|
|
|
|
context?: any;
|
|
|
|
logFunction?: Function;
|
2016-06-28 00:15:11 -04:00
|
|
|
formatRequest?: Function;
|
2016-06-27 21:58:22 -04:00
|
|
|
validationRules?: Array<Function>; // validation rules are functions
|
2016-06-27 19:06:09 -04:00
|
|
|
formatResponse?: Function;
|
2016-06-12 22:41:46 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 16:57:52 -04:00
|
|
|
export interface ExpressApolloOptionsFunction {
|
2016-06-27 22:19:57 -04:00
|
|
|
(req?: express.Request): ExpressApolloOptions | Promise<ExpressApolloOptions>;
|
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-06-24 17:16:33 -04:00
|
|
|
export function graphqlHTTP(options: ExpressApolloOptions | ExpressApolloOptionsFunction): 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
|
|
|
|
throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length + 1}`);
|
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-06-24 16:57:52 -04:00
|
|
|
let optionsObject: ExpressApolloOptions;
|
|
|
|
if (isOptionsFunction(options)) {
|
2016-06-27 22:19:57 -04:00
|
|
|
optionsObject = await options(req);
|
2016-06-24 16:57:52 -04:00
|
|
|
} else {
|
|
|
|
optionsObject = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method !== 'POST') {
|
|
|
|
res.setHeader('Allow', 'POST');
|
2016-06-27 22:19:57 -04:00
|
|
|
res.status(405);
|
|
|
|
res.send('Apollo Server supports only POST requests.');
|
|
|
|
return;
|
2016-06-27 18:06:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.body) {
|
2016-06-27 22:19:57 -04:00
|
|
|
res.status(500);
|
|
|
|
res.send('POST body missing. Did you forget "app.use(bodyParser.json())"?');
|
|
|
|
return;
|
2016-06-24 16:57:52 -04:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:15:11 -04:00
|
|
|
let b = req.body;
|
|
|
|
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-06-28 00:15:11 -04:00
|
|
|
if (!Array.isArray(b)) {
|
|
|
|
isBatch = false;
|
|
|
|
b = [b];
|
2016-06-26 21:06:37 -04:00
|
|
|
}
|
2016-06-24 16:57:52 -04:00
|
|
|
|
2016-06-28 00:15:11 -04:00
|
|
|
let responses: Array<graphql.GraphQLResult> = [];
|
|
|
|
for (let requestParams of b) {
|
|
|
|
const query = requestParams.query;
|
|
|
|
const operationName = requestParams.operationName;
|
|
|
|
let variables = requestParams.variables;
|
|
|
|
|
|
|
|
if (typeof variables === 'string') {
|
|
|
|
// TODO: catch errors
|
|
|
|
variables = JSON.parse(variables);
|
|
|
|
}
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
schema: optionsObject.schema,
|
|
|
|
query: query,
|
|
|
|
variables: variables,
|
|
|
|
rootValue: optionsObject.rootValue,
|
|
|
|
operationName: operationName,
|
|
|
|
logFunction: optionsObject.logFunction,
|
|
|
|
validationRules: optionsObject.validationRules,
|
|
|
|
formatError: optionsObject.formatError,
|
|
|
|
formatResponse: optionsObject.formatResponse,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (optionsObject.formatRequest) {
|
|
|
|
params = optionsObject.formatRequest(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!params.query) {
|
|
|
|
// TODO: shift this into runQuery
|
|
|
|
res.status(400);
|
|
|
|
res.send('Must provide query string.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
responses.push(await runQuery(params));
|
2016-06-27 18:06:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:15:11 -04:00
|
|
|
res.set('Content-Type', 'application/json');
|
|
|
|
if (isBatch) {
|
|
|
|
res.send(JSON.stringify(responses));
|
|
|
|
} else {
|
|
|
|
const gqlResponse = responses[0];
|
2016-06-27 18:06:15 -04:00
|
|
|
if (gqlResponse.errors && typeof gqlResponse.data === 'undefined') {
|
2016-06-24 16:57:52 -04:00
|
|
|
res.status(400);
|
|
|
|
}
|
2016-06-27 21:58:22 -04:00
|
|
|
res.send(JSON.stringify(gqlResponse));
|
2016-06-28 00:15:11 -04:00
|
|
|
}
|
|
|
|
|
2016-06-15 20:35:48 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-24 16:57:52 -04:00
|
|
|
function isOptionsFunction(arg: ExpressApolloOptions | ExpressApolloOptionsFunction): arg is ExpressApolloOptionsFunction {
|
|
|
|
return typeof arg === 'function';
|
2016-06-17 15:45:35 -07:00
|
|
|
}
|
|
|
|
|
2016-06-15 20:35:48 -07:00
|
|
|
// this returns the html for the GraphiQL interactive query UI
|
2016-06-18 10:19:51 -07:00
|
|
|
export function renderGraphiQL(options: GraphiQL.GraphiQLData) {
|
2016-06-15 20:35:48 -07:00
|
|
|
return (req: express.Request, res: express.Response, next) => {
|
2016-06-26 15:59:15 -04:00
|
|
|
|
2016-06-27 18:06:15 -04:00
|
|
|
const q = req.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-26 15:59:15 -04:00
|
|
|
location: options.location,
|
|
|
|
query: query || options.query,
|
|
|
|
variables: JSON.parse(variables) || options.variables,
|
|
|
|
operationName: operationName || options.operationName,
|
2016-06-14 12:03:53 -07:00
|
|
|
});
|
2016-06-15 20:35:48 -07:00
|
|
|
res.set('Content-Type', 'text/html');
|
|
|
|
res.send(graphiQLString);
|
2016-06-14 12:03:53 -07:00
|
|
|
};
|
2016-06-12 22:41:46 -07:00
|
|
|
}
|