-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (45 loc) · 1.07 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
import express from "express";
import bodyParser from "body-parser";
import { graphiqlExpress, graphqlExpress } from "graphql-server-express";
import { makeExecutableSchema } from "graphql-tools";
import cors from "cors";
import jwt from "jsonwebtoken";
import typeDefs from "./types";
import resolvers from "./resolvers";
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const SECRET = "aslkdjlkaj10830912039jlkoaiuwerasdjflkasd";
const app = express();
const addUser = async req => {
const token = req.headers.authorization;
try {
const { user } = await jwt.verify(token, SECRET);
req.user = user;
} catch (err) {
if (err.message === "jwt expired") console.log("got and old token.");
}
req.next();
};
app.use(cors("*"));
app.use(addUser);
app.use(
"/graphiql",
graphiqlExpress({
endpointURL: "/api"
})
);
app.use(
"/api",
bodyParser.json(),
graphqlExpress(req => ({
schema,
context: {
SECRET,
user: req.user
}
}))
);
app.listen(PORT);