-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (64 loc) · 1.77 KB
/
index.js
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
const dotenv = require("dotenv");
const { ApolloServer } = require("apollo-server");
const schema = require("./src/schemas");
dotenv.config();
/**
* Ensure that the PORT variable is provided in the .env file
*/
if (!process.env.PORT) {
console.error("Please provide a PORT variable in the .env file.");
process.exit(1);
}
/**
* The port number for Apollo Server.
* @type {number}
*/
const port = parseInt(process.env.PORT, 10) || 4000;
/**
* The Apollo Server instance for serving GraphQL requests.
* @type {ApolloServer}
*/
const server = new ApolloServer({
schema,
});
/**
* Handle SIGINT for graceful shutdown.
*/
process.on("SIGINT", async () => {
try {
console.log("Received SIGINT. Closing Apollo Server...");
await server.stop();
console.log("Apollo Server closed.");
process.exit(0);
} catch (error) {
console.error("Error during graceful shutdown:", error);
process.exit(1);
}
});
/**
* Starts the Apollo Server and listens on the specified port.
* @function
* @param {number} port - The port number to listen on.
*/
const startApolloServer = (port) => {
server
.listen({ port })
.then(({ url }) => {
console.log(`Apollo Server started at ${url} 🚀`);
})
.catch((error) => {
console.error("Error starting Apollo Server:", error);
if (error.code === "EADDRINUSE") {
console.error(
`Port ${port} is already in use. Please choose another port.`
);
} else {
console.error(`Unknown error during Apollo Server startup.`);
}
process.exit(1);
});
};
/**
* Start Apollo Server on the specified port
*/
startApolloServer(port);