-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (76 loc) · 2.31 KB
/
app.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
/* eslint-disable prefer-destructuring */
const mongoose = require("mongoose");
const jwt = require("jsonwebtoken");
const path = require("path");
const express = require("express");
const {ApolloServer} = require("apollo-server-express");
const {createServer} = require("http");
const schema = require("./GraphqlSchemas/ChatGraphqlSchema/index");
require("dotenv").config();
const _Url = process.env.DB_URL;
const PORT = process.env.PORT || 8000;
console.log("🚀 Server IS ready");
mongoose
.connect(_Url, {useUnifiedTopology: true, useNewUrlParser: true})
.then(() => console.log("connected to DB"))
.catch(err => console.log(err));
const app = express();
const ChatRoom = require("./Models/ChatRoomModel");
//
const getTokenPayload = token => {
const payload = jwt.verify(token, "secret");
return payload;
};
// const disConnectTheUser = async context => {
// const initialContext = await context.initPromise;
// const user = getTokenPayload(initialContext.token);
// const userID = mongoose.Types.ObjectId(user._id);
// await ChatRoom.findOneAndUpdate({members: userID}, {$pull: {members: userID}});
// };
// const connectTheUser = async context => {
// const user = getTokenPayload(context.token);
// const userID = mongoose.Types.ObjectId(user._id);
// await ChatRoom.findOneAndUpdate(
// {_id: context.roomID},
// {$push: {members: userID}}
// );
// };
const apolloServer = new ApolloServer({
schema,
subscriptions: {
path: "/subscriptions",
onConnect: connectionParams => {
// connectTheUser(connectionParams)
return connectionParams;
},
onDisconnect: (_, context) => {
// disConnectTheUser(context);
},
},
context: async ({req, connection}) => {
var token;
if (connection) {
token = connection.context.token;
} else {
token = req.headers.authorization.split(" ")[1];
}
try {
const user = getTokenPayload(token);
return {user};
} catch {
return {user: null};
}
},
});
if (process.env.NODE_ENV === "production" || false) {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (_, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
}
const server = createServer(app);
apolloServer.applyMiddleware({app});
apolloServer.installSubscriptionHandlers(server);
server.listen(PORT, () => {
console.log("🚀 Server IS ready");
});