/
ApolloServer.ts
159 lines (141 loc) 路 4.84 KB
/
ApolloServer.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
153
154
155
156
157
158
159
import {
ApolloServerBase,
Config,
convertNodeHttpToRequest,
GraphQLOptions,
isHttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import accepts from '@fastify/accepts';
import fastifyCors from '@fastify/cors';
export interface ServerRegistration {
path?: string;
cors?: Record<string, unknown> | boolean;
onHealthCheck?: (req: FastifyRequest) => Promise<any>;
disableHealthCheck?: boolean;
}
export interface FastifyContext {
request: FastifyRequest;
reply: FastifyReply;
}
export type ApolloServerFastifyConfig = Config<FastifyContext>;
export class ApolloServer<
ContextFunctionParams = FastifyContext,
> extends ApolloServerBase<ContextFunctionParams> {
async createGraphQLServerOptions(
request: FastifyRequest,
reply: FastifyReply,
): Promise<GraphQLOptions> {
const contextParams: FastifyContext = { request, reply };
return this.graphQLServerOptions(contextParams);
}
public createHandler({
path,
cors,
disableHealthCheck,
onHealthCheck,
}: ServerRegistration = {}) {
this.graphqlPath = path || '/graphql';
this.assertStarted('createHandler');
const landingPage = this.getLandingPage();
return async (app: FastifyInstance) => {
if (!disableHealthCheck) {
app.get('/.well-known/apollo/server-health', async (request, reply) => {
// Response follows https://tools.ietf.org/html/draft-inadarei-api-health-check-01
reply.type('application/health+json');
if (onHealthCheck) {
try {
await onHealthCheck(request);
return reply.send('{"status":"pass"}');
} catch (e) {
return reply.status(503).send('{"status":"fail"}');
}
} else {
return reply.send('{"status":"pass"}');
}
});
}
app.register(
async (instance) => {
instance.register(accepts);
if (cors === true) {
instance.register(fastifyCors);
} else if (cors !== false) {
instance.register(fastifyCors, cors);
}
instance.setNotFoundHandler((_request, reply) => {
reply.code(405);
reply.header('allow', 'GET, POST');
reply.send();
});
const preHandler = landingPage
? async (request: FastifyRequest, reply: FastifyReply) => {
if (request.raw.method === 'GET') {
const accept = request.accepts();
const types = accept.types() as string[];
const prefersHtml =
types.find(
(x: string) =>
x === 'text/html' || x === 'application/json',
) === 'text/html';
if (prefersHtml) {
reply.type('text/html');
return reply.send(landingPage.html);
}
}
return undefined;
}
: undefined;
instance.route({
method: ['GET', 'POST'],
url: '/',
preHandler,
handler: async (request, reply) => {
try {
const { graphqlResponse, responseInit } = await runHttpQuery(
[],
{
method: request.raw.method as string,
options: () =>
this.createGraphQLServerOptions(request, reply),
query: (request.raw.method === 'POST'
? request.body
: request.query) as any,
request: convertNodeHttpToRequest(request.raw),
},
this.csrfPreventionRequestHeaders,
);
if (responseInit.headers) {
for (const [name, value] of Object.entries<string>(
responseInit.headers,
)) {
reply.header(name, value);
}
}
reply.status(responseInit.status || 200);
reply.serializer((payload: string) => payload);
return reply.send(graphqlResponse);
} catch (error) {
if (!isHttpQueryError(error)) {
throw error;
}
if (error.headers) {
for (const [header, value] of Object.entries(error.headers)) {
reply.header(header, value);
}
}
reply.code(error.statusCode);
reply.serializer((payload: string) => payload);
return reply.send(error.message);
}
},
});
},
{
prefix: this.graphqlPath,
},
);
};
}
}