-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathexpress.ts
More file actions
170 lines (165 loc) · 4.88 KB
/
Copy pathexpress.ts
File metadata and controls
170 lines (165 loc) · 4.88 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import type { Request, Response, Handler } from 'express';
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.
*
* The `res` property is the Express response object for the current request.
* It can be used to manipulate the HTTP response from the `context` option
* or from GraphQL resolvers, for example to set response headers or cookies.
*
* @category Server/express
*/
export interface RequestContext {
res: Response;
}
/**
* 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 `Response` 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 express from 'express'; // yarn add express
* import { parseRequestParams } from 'graphql-http/lib/use/express';
*
* const app = express();
* app.all('/graphql', async (req, res) => {
* 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);
* }
* });
*
* app.listen({ port: 4000 });
* console.log('Listening to port 4000');
* ```
*
* @category Server/express
*/
export async function parseRequestParams(
req: Request,
res: Response,
): 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 express adapter.
*
* @category Server/express
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<Request, RequestContext, Context>;
/**
* Create a GraphQL over HTTP spec compliant request handler for
* the express framework.
*
* The Express response is available to GraphQL resolvers through
* `req.context.res`. This can be used to manipulate the HTTP response,
* for example to set response headers or cookies.
*
* ```js
* import express from 'express'; // yarn add express
* import { createHandler } from 'graphql-http/lib/use/express';
* import { schema } from './my-graphql-schema';
*
* const app = express();
*
* app.all(
* '/graphql',
* createHandler({
* schema,
* context(req) {
* return {
* res: req.context.res,
* };
* },
* }),
* );
*
* app.listen({ port: 4000 });
* ```
*
* A resolver can then manipulate the response:
*
* ```js
* resolve(_, __, { res }) {
* res.setHeader('set-cookie', 'session=; Max-Age=0; Path=/');
* return true;
* }
* ```
*
* @category Server/express
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<Context>,
): Handler {
const handle = createRawHandler(options);
return async function requestListener(req, res) {
try {
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: Request,
res: Response,
): RawRequest<Request, RequestContext> {
return {
url: req.url,
method: req.method,
headers: req.headers,
body: () => {
if (req.body) {
// in case express has a body parser
return req.body;
}
return 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 },
};
}