-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
47 lines (37 loc) · 1.2 KB
/
main.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
"use strict";
import fastify from "fastify";
import cors from "@fastify/cors";
import mercurius from "mercurius";
import "dotenv/config";
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
import { loadSchema } from "@graphql-tools/load";
import { isEnvValid, CorrectEnv } from "./types/validators";
import { connectDb } from "./data/store";
import resolvers from "./resolvers/resolvers";
import { readTokenIntoContext } from "./auth/authentication";
isEnvValid(process.env);
const env = process.env as CorrectEnv;
const app = fastify();
async function loadServer(): Promise<void> {
await connectDb();
const schema = await loadSchema("./schema/*.gql", {
cwd: __dirname,
assumeValid: true,
loaders: [new GraphQLFileLoader()],
});
if (env.CORS_ENABLED === "true") {
console.log(`CORS enabled from origin ${env.CORS_ENABLED_ORIGIN}`);
app.register(cors, { origin: env.CORS_ENABLED_ORIGIN });
}
app.register(mercurius, {
schema,
resolvers,
graphiql: true,
context: readTokenIntoContext,
});
await app.listen({
port: parseInt(env.API_PORT),
host: env.API_HOST,
});
}
loadServer().then(() => console.log(`listening @ port ${env.API_PORT}`));