apollo-server/src/integrations/expressApollo.ts

120 lines
3.6 KiB
TypeScript
Raw Normal View History

2016-06-12 22:41:46 -07:00
import * as express from 'express';
import * as graphql from 'graphql';
import { runQuery } from '../core/runQuery';
2016-06-12 22:41:46 -07:00
import * as GraphiQL from '../modules/renderGraphiQL';
2016-06-15 20:35:48 -07:00
2016-06-24 16:57:52 -04:00
import httpError from 'http-errors';
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-24 16:57:52 -04:00
// pretty?: boolean;
2016-06-15 20:35:48 -07:00
// TODO: does this need to be able to take a promise as well, like express-graphql does?
// answer: yes, it does. Func(req) => options
2016-06-12 22:41:46 -07:00
}
2016-06-24 16:57:52 -04:00
export interface ExpressApolloOptionsFunction {
(req?: express.Request): ExpressApolloOptions;
}
// 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;
}
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-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-12 22:41:46 -07:00
return (req: express.Request, res: express.Response, next) => {
2016-06-24 16:57:52 -04:00
let optionsObject: ExpressApolloOptions;
if (isOptionsFunction(options)) {
optionsObject = options(req);
} else {
optionsObject = options;
}
if (req.method !== 'POST') {
res.setHeader('Allow', 'POST');
throw httpError(405, 'Apollo Server supports only POST requests for GraphQL.');
}
// TODO: some sanity checks here.
2016-06-27 16:14:49 -04:00
const b = req.body;
const query = b.query;
const operationName = b.operationName;
let variables = b.variables;
2016-06-26 21:06:37 -04:00
if (typeof variables === 'string') {
// TODO: catch errors
variables = JSON.parse(variables);
}
2016-06-24 16:57:52 -04:00
// either query or operationName must be present. Return 400 otherwise
// if only operationName is present, check if it's in store. Return 400 otherwise
// TODO: in store, fragments should only have to be written once, then used across queries.
return runQuery({
2016-06-24 16:57:52 -04:00
schema: optionsObject.schema,
query: query,
variables: variables,
rootValue: optionsObject.rootValue,
operationName: operationName,
}).then(gqlResponse => {
res.set('Content-Type', 'application/json');
2016-06-24 16:57:52 -04:00
if (gqlResponse.errors && !gqlResponse.data) {
res.status(400);
}
const response = {
data: gqlResponse.data,
};
if (gqlResponse.errors) {
response['errors'] = gqlResponse.errors.map(graphql.formatError);
}
res.send(JSON.stringify(response));
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-15 20:35:48 -07:00
// this returns the html for the GraphiQL interactive query UI
// TODO: it's still missing a way to tell it where the GraphQL endpoint is.
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 16:14:49 -04:00
const q = req.query;
const query = q.query || '';
const variables = q.variables || '{}';
const operationName = q.operationName || '';
2016-06-26 15:59:15 -04: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-15 20:35:48 -07:00
res.set('Content-Type', 'text/html');
res.send(graphiQLString);
};
2016-06-12 22:41:46 -07:00
}