-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.ts
111 lines (100 loc) · 2.94 KB
/
app.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 "dotenv/config";
import "reflect-metadata";
import AltairFastify from "altair-fastify-plugin";
import Fastify from "fastify";
import FastifyCookie from "fastify-cookie";
import mercurius from "mercurius";
import helmet from "fastify-helmet";
import FastifyNextJS from "fastify-nextjs";
import { renderVoyagerPage } from "graphql-voyager/middleware";
import ms from "ms";
import { resolve } from "path";
import { COOKIE_SECRET, PORT, SHOW_GRAPHQL_API } from "./api/constants";
import { buildContext } from "./api/core/buildContext";
import { schema } from "./api/core/schema";
import { logger } from "./api/services/logger";
import { IS_DEVELOPMENT, IS_NOT_TEST } from "./client/constants";
export const app = Fastify({
trustProxy: true,
pluginTimeout: ms("600 seconds"),
logger,
});
app.register(helmet, {
contentSecurityPolicy: {
directives: {
"default-src": "'self'",
"style-src":
"'self' https://cdn.jsdelivr.net https://fonts.googleapis.com 'unsafe-inline'",
"script-src": IS_DEVELOPMENT
? "'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net "
: /** 'self' - altair - voyager - cdn */ "'self' 'sha256-US93NNvzxqNwZq4vsTymtyO9e1JOnGB6vGy83vG+uuw=' 'sha256-YxOTuTvyNLHJOis3NYhQFhTdPRWuJIRWCv6cons2/b4=' https://cdn.jsdelivr.net",
"font-src":
"'self' https://fonts.gstatic.com https://cdn.jsdelivr.net data:",
"img-src": `'self' data:`,
"connect-src":
"'self' https://cdn.jsdelivr.net https://fonts.googleapis.com ",
"worker-src": "'self' blob:",
"object-src": "'self' data:",
},
},
});
app.register(FastifyCookie, {
secret: COOKIE_SECRET,
});
if (SHOW_GRAPHQL_API || IS_DEVELOPMENT) {
app.get("/api/voyager", (_req, res) => {
res.type("text/html").send(
renderVoyagerPage({
endpointUrl: "/api/graphql",
displayOptions: {
rootType: undefined,
skipRelay: false,
skipDeprecated: true,
sortByAlphabet: true,
showLeafFields: true,
hideRoot: false,
},
})
);
});
app.register(AltairFastify, {
endpointURL: "/api/graphql",
baseURL: "/api/altair/",
path: "/api/altair",
});
}
app.register(mercurius, {
path: "/api/graphql",
schema,
context: buildContext,
ide: false,
graphiql: false,
jit: 1,
queryDepth: 7,
});
if (IS_NOT_TEST) {
app
.register(FastifyNextJS, {
dir: resolve(process.cwd(), "./client"),
})
.after(() => {
app.next("/*");
});
}
if (IS_NOT_TEST) {
app.listen(PORT, "0.0.0.0").then(() => {
process.send?.("ready");
if (IS_DEVELOPMENT) {
const localPath = `http://localhost:${PORT}/`;
import("axios").then(({ default: { get } }) => {
get(localPath)
.then(() => {
import("open").then(({ default: open }) => {
open(localPath).catch(console.error);
});
})
.catch(console.error);
});
}
});
}