-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (73 loc) · 1.96 KB
/
server.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
72
73
74
75
76
77
78
79
80
81
82
83
84
const dotenv = require("dotenv");
const express = require("express");
const { ApolloServer } = require("apollo-server-express");
const mongoose = require("mongoose");
const cors = require("cors");
const schema = require("./src/schema");
const bodyparser = require("body-parser");
const morgan = require("morgan");
const logger = require("./config/winston");
const requireLogin = require("./middleware/requireLogin");
const http = require("http");
dotenv.config({
path: ".env",
});
const uri = process.env.MONGO_URI;
const app = express();
const options = {
keepAlive: 1,
useNewUrlParser: true,
useUnifiedTopology: true,
};
mongoose
.connect(uri, options)
.then(() => {
console.log("Database connected");
})
.catch((err) => {
console.log(err);
});
const server = new ApolloServer({
schema,
context: requireLogin,
subscriptions: {
path: "/subscriptions",
onConnect: (connectionParams, webSocket, context) => {
console.log("Connected!");
},
onDisconnect: (webSocket, context) => {
console.log("Disconnected!");
},
},
logger,
tracing: true,
plugins: [
{
requestDidStart() {
return {
didEncounterErrors(requestContext) {
requestContext.logger.error(requestContext.errors);
},
};
},
},
],
introspection: true,
playground: true,
});
app.use(cors());
app.use(bodyparser.json());
app.use(morgan("combined", { stream: logger.stream }));
//Applying middlewares like body parser and cors
server.applyMiddleware({
app,
path: "/",
});
const PORT = process.env.PORT || 5000;
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen(PORT, () => {
logger.info(
`Apollo server listening at http://localhost:5000${server.graphqlPath}`
);
});