-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhttp.ts
152 lines (147 loc) · 4.51 KB
/
http.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { IncomingMessage, ServerResponse } from 'http';
import {
createHandler as createRawHandler,
HandlerOptions as RawHandlerOptions,
Request as RawRequest,
parseRequestParams as rawParseRequestParams,
OperationContext,
} from '../handler';
import { RequestParams } from '../common';
/**
* The context in the request for the handler.
*
* @category Server/http
*/
export interface RequestContext {
res: ServerResponse;
}
/**
* The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
*
* If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
* on the `ServerResponse` argument and return `null`.
*
* If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
* the function will throw an error and it is up to the user to handle and respond as they see fit.
*
* ```js
* import http from 'http';
* import { parseRequestParams } from 'graphql-http/lib/use/http';
*
* const server = http.createServer(async (req, res) => {
* if (req.url.startsWith('/graphql')) {
* try {
* const maybeParams = await parseRequestParams(req, res);
* if (!maybeParams) {
* // not a well-formatted GraphQL over HTTP request,
* // parser responded and there's nothing else to do
* return;
* }
*
* // well-formatted GraphQL over HTTP request,
* // with valid parameters
* res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
* } catch (err) {
* // well-formatted GraphQL over HTTP request,
* // but with invalid parameters
* res.writeHead(400).end(err.message);
* }
* } else {
* res.writeHead(404).end();
* }
* });
*
* server.listen(4000);
* console.log('Listening to port 4000');
* ```
*
* @category Server/http
*/
export async function parseRequestParams(
req: IncomingMessage,
res: ServerResponse,
): Promise<RequestParams | null> {
const rawReq = toRequest(req, res);
const paramsOrRes = await rawParseRequestParams(rawReq);
if (!('query' in paramsOrRes)) {
const [body, init] = paramsOrRes;
res.writeHead(init.status, init.statusText, init.headers).end(body);
return null;
}
return paramsOrRes;
}
/**
* Handler options when using the http adapter.
*
* @category Server/http
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<IncomingMessage, RequestContext, Context>;
/**
* Create a GraphQL over HTTP spec compliant request handler for
* the Node environment http module.
*
* ```js
* import http from 'http';
* import { createHandler } from 'graphql-http/lib/use/http';
* import { schema } from './my-graphql-schema';
*
* const server = http.createServer(createHandler({ schema }));
*
* server.listen(4000);
* console.log('Listening to port 4000');
* ```
*
* @category Server/http
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<Context>,
): (req: IncomingMessage, res: ServerResponse) => Promise<void> {
const handle = createRawHandler(options);
return async function requestListener(req, res) {
try {
if (!req.url) {
throw new Error('Missing request URL');
}
if (!req.method) {
throw new Error('Missing request method');
}
const [body, init] = await handle(toRequest(req, res));
res.writeHead(init.status, init.statusText, init.headers).end(body);
} catch (err) {
// The handler shouldnt throw errors.
// If you wish to handle them differently, consider implementing your own request handler.
console.error(
'Internal error occurred during request handling. ' +
'Please check your implementation.',
err,
);
res.writeHead(500).end();
}
};
}
function toRequest(
req: IncomingMessage,
res: ServerResponse,
): RawRequest<IncomingMessage, RequestContext> {
if (!req.url) {
throw new Error('Missing request URL');
}
if (!req.method) {
throw new Error('Missing request method');
}
return {
url: req.url,
method: req.method,
headers: req.headers,
body: () =>
new Promise<string>((resolve) => {
let body = '';
req.setEncoding('utf-8');
req.on('data', (chunk) => (body += chunk));
req.on('end', () => resolve(body));
}),
raw: req,
context: { res },
};
}