-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.64 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
const { ApolloServer, PubSub } = require("apollo-server-express");
const { prisma } = require("./prisma/generated/prisma-client");
const express = require("express");
const { createServer } = require("http");
const pubsub = new PubSub();
const SOMETHING_CHANGED_TOPIC = "something_changed";
const modules = require("./modules");
const app = express();
// GraphQL middleware
const server = new ApolloServer({
modules: [...modules],
context: req => {
return {
...req
};
},
subscriptions: {
path: "/websocket",
onConnect: (connectionParams, rawSocket, context) => {
console.log("onConnect");
return pubsub.publish(SOMETHING_CHANGED_TOPIC, {
something_changed: new Date().toString()
});
// return {
// hello: "world"
// };
},
onDisconnect: rawSocket => {
console.log("onDisconnect");
// console.log(rawSocket.upgradeReq)
}
}
});
app.use((req, res, next) => {
req.userId =
"123456789123456789123456789123456789123456789123456789123456789123456789";
next();
});
server.applyMiddleware({ app });
// We need to create a separate HTTP server to handle GraphQL subscriptions via websockets
const httpServer = createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen(4000, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
);
//publish events every second
// var memory = process.memoryUsage()
// setInterval(() => {
// let memory = process.memoryUsage()
// // console.log(memory)
// return pubsub.publish(SOMETHING_CHANGED_TOPIC, {
// something_changed: new Date().toString()
// })
// }, 1000)