-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
111 lines (99 loc) · 2.67 KB
/
index.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
import * as config from './config';
import rawSchema from './schema';
import fastify from 'fastify';
import fastifyCors from '@fastify/cors';
import {
getGraphQLParameters,
processRequest,
renderGraphiQL,
sendResult,
shouldRenderGraphiQL,
} from 'graphql-helix';
import { envelop, useErrorHandler, useLogger, useSchema } from '@envelop/core';
import { useResponseCache } from '@envelop/response-cache';
import { getHeapSpaceStatistics, getHeapStatistics } from 'v8';
import process from 'node:process';
const plugins = [
useSchema(rawSchema),
useErrorHandler((errors) =>
errors.forEach((e) => console.log('GraphQL error:', e))
),
useResponseCache({
ttl: 60 * 1000 * 60 * 24 * 7,
// use global cache for all operations
session: () => null,
}),
];
if (config.ENABLE_TRACING) {
plugins.push(useLogger());
}
const getEnveloped = envelop({
plugins,
enableInternalTracing: config.ENABLE_TRACING,
});
const app = fastify({
logger: true,
return503OnClosing: false, // Not sure if we still get requests after SIGTERM/close() happens, but just to be sure
});
app.register(fastifyCors);
app.get('/health', async () => {
return 'OK';
});
app.get('/', async (_req, res) => {
res.redirect('/graphql');
});
app.route({
method: ['GET', 'POST'],
url: '/graphql',
handler: async (req, res) => {
const { parse, validate, contextFactory, execute, schema } = getEnveloped({
req,
});
const request = {
body: req.body,
headers: req.headers,
method: req.method,
query: req.query,
};
if (shouldRenderGraphiQL(request)) {
void res.type('text/html');
return renderGraphiQL({});
}
res.hijack();
const { operationName, query, variables } = getGraphQLParameters(request);
const result = await processRequest({
operationName,
query,
variables,
request,
schema,
parse,
validate,
execute,
contextFactory,
});
void sendResult(result, res.raw);
},
});
app.listen({ port: config.PORT }, () => {
const newSpaceSize = getHeapSpaceStatistics().find(
(space) => space.space_name === 'new_space'
)?.space_size;
const heapSizeLimit = getHeapStatistics().heap_size_limit;
console.log(`🚀 Server started at http://localhost:${config.PORT}/graphql`);
console.log(
`Heap size limit: ${heapSizeLimit} New space size: ${newSpaceSize}`
);
console.log(`NODE_OPTIONS=${process.env.NODE_OPTIONS}`);
});
process.once('SIGTERM', () => {
console.log('SIGTERM signal received: closing HTTP server');
app.close().then(
() => {
console.log('HTTP server closed');
},
(err) => {
console.error('An error happened', err);
}
);
});