-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.mjs
87 lines (78 loc) · 2.78 KB
/
index.mjs
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
85
86
87
import http from "http";
import express from "express";
import expressJWT from "express-jwt";
import jwt from "jsonwebtoken";
import { ApolloServer } from "apollo-server-express";
import { applyMiddleware } from "graphql-middleware";
import { makeExecutableSchema } from "graphql-tools";
import mongoose from "mongoose";
import typeDefs from "./graphql/schema.js";
import resolvers from "./graphql/resolvers.js";
import { User } from "./models/user.js";
import { permissions } from "./permissions/index.js";
import pubsub from "./pubsub.js";
const server = new ApolloServer({
schema: applyMiddleware(makeExecutableSchema({ typeDefs, resolvers }), permissions),
context: async ({ req, connection }) => {
if (connection) {
return { user: connection.context.user, pubsub };
}
const user = req?.user ? await User.findById(req.user.sub).populate("beacons") : null;
return { user, pubsub };
},
stopGracePeriodMillis: Infinity,
stopOnTerminationSignals: false,
subscriptions: {
path: "/graphql",
keepAlive: 9000,
onConnect: async connectionParams => {
console.log("Client connected");
const authorization = connectionParams["Authorization"];
if (authorization) {
try {
const decoded = jwt.verify(authorization.replace("Bearer ", ""), process.env.JWT_SECRET);
console.log("decoded: ", decoded);
const user = await User.findById(decoded.sub).populate("beacons");
return { user };
} catch (err) {
console.log(err);
throw new Error("Invalid token!");
}
}
throw new Error("Missing auth token!");
},
onDisconnect: () => {
console.log("Client disconnected");
},
},
});
const app = express();
app.use(
expressJWT({
secret: process.env.JWT_SECRET,
algorithms: ["HS256"],
credentialsRequired: false,
})
);
app.get("/", (req, res) => res.send("Hello World! This is a GraphQL API. Check out /graphql"));
app.get("/j/:shortcode", async (_req, res) => {
console.log(`shortcode route hit`);
res.send("this should open in the app eventually");
});
server.applyMiddleware({ app });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
mongoose
.connect(process.env.DB)
.then(() =>
httpServer.listen(
{ port: 4000 },
console.log(
`Server ready at http://localhost:4000${server.graphqlPath}\n` +
`Subscriptions endpoint at ws://localhost:4000${server.subscriptionsPath}`
)
)
)
.catch(error => {
throw error;
});