import * as express from 'express'; import * as url from 'url'; import { GraphQLOptions, HttpQueryError, runHttpQuery } from 'graphql-server-core'; import * as GraphiQL from 'graphql-server-module-graphiql'; export interface ExpressGraphQLOptionsFunction { (req?: express.Request, res?: express.Response): GraphQLOptions | Promise; } // 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 { if (!options) { throw new Error('Apollo Server requires options.'); } if (arguments.length > 1) { // TODO: test this throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`); } 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) => { if ( 'HttpQueryError' !== error.name ) { throw error; } if ( error.headers ) { Object.keys(error.headers).forEach((header) => { res.setHeader(header, error.headers[header]); }); } res.statusCode = error.statusCode; res.write(error.message); res.end(); }); }; } /* 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) { return (req: express.Request, res: express.Response) => { const q = req.url && url.parse(req.url, true).query || {}; const query = q.query || ''; const operationName = q.operationName || ''; const graphiQLString = GraphiQL.renderGraphiQL({ endpointURL: options.endpointURL, query: query || options.query, variables: q.variables && JSON.parse(q.variables) || options.variables, operationName: operationName || options.operationName, passHeader: options.passHeader, }); res.setHeader('Content-Type', 'text/html'); res.write(graphiQLString); res.end(); }; }