|
1 | 1 | import type {
|
2 | 2 | ApolloServer,
|
| 3 | + ApolloServerPlugin, |
3 | 4 | BaseContext,
|
4 | 5 | ContextFunction,
|
5 | 6 | HTTPGraphQLRequest,
|
6 | 7 | } from "@apollo/server";
|
7 | 8 | import type { WithRequired } from "@apollo/utils.withrequired";
|
8 | 9 | import type {
|
9 |
| - FastifyPluginCallback, |
| 10 | + FastifyInstance, |
10 | 11 | FastifyReply,
|
11 | 12 | FastifyRequest,
|
| 13 | + RouteHandlerMethod, |
12 | 14 | } from "fastify";
|
13 |
| -import type { PluginMetadata } from "fastify-plugin"; |
14 |
| -import fp from "fastify-plugin"; |
15 |
| - |
16 |
| -const pluginMetadata: PluginMetadata = { |
17 |
| - fastify: "4.x", |
18 |
| - name: "apollo-server-integration-fastify", |
19 |
| -}; |
| 15 | +import { parse as urlParse } from "url"; |
20 | 16 |
|
21 | 17 | export interface FastifyContextFunctionArgument {
|
22 | 18 | request: FastifyRequest;
|
23 | 19 | reply: FastifyReply;
|
24 | 20 | }
|
25 | 21 |
|
26 |
| -export interface LambdaHandlerOptions<TContext extends BaseContext> { |
27 |
| - context?: ContextFunction<[FastifyContextFunctionArgument], TContext>; |
28 |
| -} |
29 |
| - |
30 |
| -export function fastifyPlugin( |
31 |
| - server: ApolloServer<BaseContext>, |
32 |
| - options?: LambdaHandlerOptions<BaseContext>, |
33 |
| -): FastifyPluginCallback; |
34 |
| -export function fastifyPlugin<TContext extends BaseContext>( |
35 |
| - server: ApolloServer<TContext>, |
36 |
| - options: WithRequired<LambdaHandlerOptions<TContext>, "context">, |
37 |
| -): FastifyPluginCallback; |
38 |
| -export function fastifyPlugin( |
39 |
| - server: ApolloServer<BaseContext>, |
40 |
| - options?: LambdaHandlerOptions<BaseContext>, |
41 |
| -) { |
42 |
| - return fp(fastifyHandler(server, options), pluginMetadata); |
| 22 | +export interface FastifyHandlerOptions<TContext extends BaseContext> { |
| 23 | + context?: |
| 24 | + | ContextFunction<[FastifyContextFunctionArgument], TContext> |
| 25 | + | undefined; |
43 | 26 | }
|
44 | 27 |
|
45 |
| -function fastifyHandler( |
| 28 | +export function fastifyHandler( |
46 | 29 | server: ApolloServer<BaseContext>,
|
47 |
| - options?: LambdaHandlerOptions<BaseContext>, |
48 |
| -): FastifyPluginCallback; |
49 |
| -function fastifyHandler<TContext extends BaseContext>( |
| 30 | + options?: FastifyHandlerOptions<BaseContext>, |
| 31 | +): RouteHandlerMethod; |
| 32 | +export function fastifyHandler<TContext extends BaseContext>( |
50 | 33 | server: ApolloServer<TContext>,
|
51 |
| - options: WithRequired<LambdaHandlerOptions<TContext>, "context">, |
52 |
| -): FastifyPluginCallback; |
53 |
| -function fastifyHandler<TContext extends BaseContext>( |
| 34 | + options: WithRequired<FastifyHandlerOptions<TContext>, "context">, |
| 35 | +): RouteHandlerMethod; |
| 36 | +export function fastifyHandler<TContext extends BaseContext>( |
54 | 37 | server: ApolloServer<TContext>,
|
55 |
| - options?: LambdaHandlerOptions<TContext>, |
56 |
| -): FastifyPluginCallback { |
57 |
| - return async (fastify) => { |
58 |
| - server.assertStarted("fastifyHandler()"); |
59 |
| - |
60 |
| - // This `any` is safe because the overload above shows that context can |
61 |
| - // only be left out if you're using BaseContext as your context, and {} is a |
62 |
| - // valid BaseContext. |
63 |
| - const defaultContext: ContextFunction< |
64 |
| - [FastifyContextFunctionArgument], |
65 |
| - any |
66 |
| - > = async () => ({}); |
| 38 | + options?: FastifyHandlerOptions<TContext>, |
| 39 | +): RouteHandlerMethod { |
| 40 | + server.assertStarted("fastifyHandler()"); |
67 | 41 |
|
68 |
| - const contextFunction: ContextFunction< |
69 |
| - [FastifyContextFunctionArgument], |
70 |
| - TContext |
71 |
| - > = options?.context ?? defaultContext; |
| 42 | + // This `any` is safe because the overload above shows that context can |
| 43 | + // only be left out if you're using BaseContext as your context, and {} is a |
| 44 | + // valid BaseContext. |
| 45 | + const defaultContext: ContextFunction< |
| 46 | + [FastifyContextFunctionArgument], |
| 47 | + any |
| 48 | + > = async () => ({}); |
72 | 49 |
|
73 |
| - fastify.removeContentTypeParser(["application/json"]); |
74 |
| - fastify.addContentTypeParser( |
75 |
| - "application/json", |
76 |
| - { parseAs: "string" }, |
77 |
| - async (_request, body, _done) => { |
78 |
| - try { |
79 |
| - return JSON.parse(body as string); |
80 |
| - } catch (err) { |
81 |
| - if ((body as string).trim() === "") { |
82 |
| - return {}; |
83 |
| - } |
84 |
| - (err as any).statusCode = 400; |
85 |
| - throw err; |
86 |
| - } |
87 |
| - }, |
88 |
| - ); |
89 |
| - |
90 |
| - // This is dumb but it gets the integration testsuite passing. We should |
91 |
| - // maybe consider relaxing some of the tests in order to accommodate server |
92 |
| - // frameworks that behave well or better than Apollo Server. |
93 |
| - fastify.addContentTypeParser("*", (_, __, done) => { |
94 |
| - done(null, null); |
95 |
| - }); |
| 50 | + const contextFunction: ContextFunction< |
| 51 | + [FastifyContextFunctionArgument], |
| 52 | + TContext |
| 53 | + > = options?.context ?? defaultContext; |
96 | 54 |
|
97 |
| - fastify.addHook("preHandler", async (request, reply) => { |
98 |
| - const headers = new Map<string, string>(); |
99 |
| - for (const [key, value] of Object.entries(request.headers)) { |
100 |
| - // TODO: how does fastify handle duplicate headers? |
101 |
| - if (value !== undefined) { |
102 |
| - headers.set(key, Array.isArray(value) ? value.join(", ") : value); |
103 |
| - } |
| 55 | + return async (request, reply) => { |
| 56 | + const headers = new Map<string, string>(); |
| 57 | + for (const [key, value] of Object.entries(request.headers)) { |
| 58 | + if (value !== undefined) { |
| 59 | + headers.set(key, Array.isArray(value) ? value.join(", ") : value); |
104 | 60 | }
|
| 61 | + } |
105 | 62 |
|
106 |
| - const httpGraphQLRequest: HTTPGraphQLRequest = { |
107 |
| - method: request.method, |
108 |
| - headers, |
109 |
| - search: |
110 |
| - typeof request.raw.url === "string" |
111 |
| - ? request.raw.url.substring(1) |
112 |
| - : "", |
113 |
| - body: request.body, |
114 |
| - }; |
| 63 | + const httpGraphQLRequest: HTTPGraphQLRequest = { |
| 64 | + method: request.method, |
| 65 | + headers, |
| 66 | + search: urlParse(request.url).search ?? "", |
| 67 | + body: request.body, |
| 68 | + }; |
115 | 69 |
|
116 |
| - const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({ |
117 |
| - httpGraphQLRequest, |
118 |
| - context: () => contextFunction({ request, reply }), |
119 |
| - }); |
| 70 | + const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({ |
| 71 | + httpGraphQLRequest, |
| 72 | + context: () => contextFunction({ request, reply }), |
| 73 | + }); |
120 | 74 |
|
121 |
| - if (httpGraphQLResponse.completeBody === null) { |
122 |
| - throw Error("Incremental delivery not implemented"); |
123 |
| - } |
| 75 | + if (httpGraphQLResponse.completeBody === null) { |
| 76 | + throw Error("Incremental delivery not implemented"); |
| 77 | + } |
124 | 78 |
|
125 |
| - reply.code(httpGraphQLResponse.statusCode ?? 200); |
126 |
| - reply.headers(Object.fromEntries(httpGraphQLResponse.headers)); |
127 |
| - reply.send(httpGraphQLResponse.completeBody); |
| 79 | + reply.code(httpGraphQLResponse.statusCode ?? 200); |
| 80 | + reply.headers(Object.fromEntries(httpGraphQLResponse.headers)); |
| 81 | + reply.send(httpGraphQLResponse.completeBody); |
128 | 82 |
|
129 |
| - return reply; |
130 |
| - }); |
| 83 | + return reply; |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +// Add this plugin to your ApolloServer to drain the server during shutdown. |
| 88 | +// This works best with Node 18.2.0 or newer; with that version, Fastify will |
| 89 | +// use the new server.closeIdleConnections() to close idle connections, and the |
| 90 | +// plugin will close any other connections 10 seconds later. (With older Node, |
| 91 | +// the drain phase will hang until all connections naturally close; you can also |
| 92 | +// call `fastify({forceCloseConnections: true})` to make all connections immediately |
| 93 | +// close without grace.) |
| 94 | +export function fastifyDrainPlugin<TContext extends BaseContext>( |
| 95 | + app: FastifyInstance, |
| 96 | +): ApolloServerPlugin<TContext> { |
| 97 | + return { |
| 98 | + async serverWillStart() { |
| 99 | + return { |
| 100 | + async drainServer() { |
| 101 | + let timeout; |
| 102 | + if ("closeAllConnections" in app.server) { |
| 103 | + timeout = setTimeout( |
| 104 | + () => (app.server as any).closeAllConnections(), |
| 105 | + 10_000, |
| 106 | + ); |
| 107 | + } |
| 108 | + await app.close(); |
| 109 | + if (timeout) { |
| 110 | + clearTimeout(timeout); |
| 111 | + } |
| 112 | + }, |
| 113 | + }; |
| 114 | + }, |
131 | 115 | };
|
132 | 116 | }
|
0 commit comments