-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstartServerAndCreateNextHandler.ts
90 lines (74 loc) · 3.19 KB
/
startServerAndCreateNextHandler.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
import { getBody } from './lib/getBody';
import { getHeaders } from './lib/getHeaders';
import { isNextApiRequest } from './lib/isNextApiRequest';
import { ApolloServer, BaseContext, ContextFunction } from '@apollo/server';
import { NextApiRequest, NextApiResponse } from 'next';
import { NextRequest } from 'next/server';
import { Readable } from 'stream';
import { parse } from 'url';
type HandlerRequest = NextApiRequest | NextRequest | Request;
interface Options<Req extends HandlerRequest, Context extends BaseContext> {
context?: ContextFunction<[Req, Req extends NextApiRequest ? NextApiResponse : undefined], Context>;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defaultContext: ContextFunction<[], any> = async () => ({});
function startServerAndCreateNextHandler<
Req extends HandlerRequest = NextApiRequest,
Context extends BaseContext = object,
>(server: ApolloServer<Context>, options?: Options<Req, Context>) {
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
const contextFunction = options?.context || defaultContext;
async function handler<HandlerReq extends NextApiRequest>(req: HandlerReq, res: NextApiResponse): Promise<unknown>;
async function handler<HandlerReq extends NextRequest | Request>(req: HandlerReq, res?: undefined): Promise<Response>;
async function handler(req: HandlerRequest, res: NextApiResponse | undefined) {
const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({
context: () => contextFunction(req as Req, res as Req extends NextApiRequest ? NextApiResponse : undefined),
httpGraphQLRequest: {
body: await getBody(req),
headers: getHeaders(req),
method: req.method || 'POST',
search: req.url ? parse(req.url).search || '' : '',
},
});
if (isNextApiRequest(req)) {
if (!res) {
throw new Error('API Routes require you to pass both the req and res object.');
}
for (const [key, value] of httpGraphQLResponse.headers) {
res.setHeader(key, value);
}
res.statusCode = httpGraphQLResponse.status || 200;
if (httpGraphQLResponse.body.kind === 'complete') {
res.send(httpGraphQLResponse.body.string);
res.end();
} else {
res.send(Readable.from(httpGraphQLResponse.body.asyncIterator));
}
return;
}
const headers: Record<string, string> = {};
for (const [key, value] of httpGraphQLResponse.headers) {
headers[key] = value;
}
// eslint-disable-next-line consistent-return
return new Response(
httpGraphQLResponse.body.kind === 'complete'
? httpGraphQLResponse.body.string
: new ReadableStream({
async pull(controller) {
if (httpGraphQLResponse.body.kind === 'chunked') {
const { value, done } = await httpGraphQLResponse.body.asyncIterator.next();
if (done) {
controller.close();
} else {
controller.enqueue(value);
}
}
},
}),
{ headers, status: httpGraphQLResponse.status || 200 },
);
}
return handler;
}
export { startServerAndCreateNextHandler };