-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
54 lines (46 loc) · 1.57 KB
/
server.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
import dotenv from "dotenv";
dotenv.config();
import { ApolloServer } from "@apollo/server";
import { ConnectMongoDb, getCachedDb } from "./mongodb";
import { typeDefs, resolvers } from "./graphql/schema";
import { Collection } from "mongodb";
import express from "express";
import http from "http";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import cors from "cors";
import bodyParser from "body-parser";
import { expressMiddleware } from "@apollo/server/express4";
const port = process.env.PORT || 5000; // Specify your desired port number here
const app = express();
const httpServer = http.createServer(app);
// Set up Apollo Server
const server = new ApolloServer({
typeDefs: typeDefs,
resolvers: resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
async function startServer() {
// Connect to MongoDB
ConnectMongoDb();
await server.start();
app.use(
"/api/graphql",
cors<cors.CorsRequest>(),
bodyParser.json({ limit: "50mb" }),
expressMiddleware(server, {
context: async () => {
const db = getCachedDb();
const usersCollection: Collection = db.collection("users");
const mlhFellowshipCollection: Collection =
db.collection("mlh-fellowship");
return { usersCollection, mlhFellowshipCollection };
},
})
);
// Modified server startup
await new Promise<void>((resolve) =>
httpServer.listen({ port: 4000 }, resolve)
);
console.log(`🚀 Server ready at on Port`, port);
}
startServer().catch((err) => console.error(err));