-
Notifications
You must be signed in to change notification settings - Fork 2k
/
expressApollo.ts
101 lines (90 loc) · 2.89 KB
/
expressApollo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as express from 'express';
import * as url from 'url';
import {
GraphQLOptions,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import * as GraphiQL from 'apollo-server-module-graphiql';
export interface ExpressGraphQLOptionsFunction {
(req?: express.Request, res?: express.Response):
| GraphQLOptions
| Promise<GraphQLOptions>;
}
// 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, next): 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.setHeader('Content-Length', Buffer.byteLength(gqlResponse, 'utf8'));
res.write(gqlResponse);
res.end();
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
return next(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();
},
);
};
}
export interface ExpressGraphiQLOptionsFunction {
(req?: express.Request):
| GraphiQL.GraphiQLData
| Promise<GraphiQL.GraphiQLData>;
}
/* 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 | ExpressGraphiQLOptionsFunction,
) {
return (req: express.Request, res: express.Response, next) => {
const query = req.url && url.parse(req.url, true).query;
GraphiQL.resolveGraphiQLString(query, options, req).then(
graphiqlString => {
res.setHeader('Content-Type', 'text/html');
res.write(graphiqlString);
res.end();
},
error => next(error),
);
};
}