-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
83 lines (77 loc) · 2.05 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require("express");
const { postgraphile } = require("postgraphile");
const Config = require("good-config");
const jwt = require("jsonwebtoken");
const config = new Config({
path: "./config",
format: "yml", // Optional
provider: "FileSystem"
});
config
.load()
.then(() => {
const configData = config.getAll();
const app = express();
app.use(
postgraphile(configData.db, configData.graphile.schemas, {
pgDefaultRole: "user_login",
ignoreRBAC: false, // Reflect the GRANTs in the generated GraphQL schema
graphiql: true,
enhanceGraphiql: true,
showErrorStack: true,
extendedErrors: [
"severity",
"code",
"detail",
"hint",
"position",
"internalPosition",
"internalQuery",
"where",
"schema",
"table",
"column",
"dataType",
"constraint",
"file",
"line",
"routine"
],
jwtSecret: configData.auth.secret, // sharing the same secret we make our jwt with Postgraphile
jwtVerifyOptions: {
audience: null
}
})
);
// A fictional route that generates different jwt tokens for different roles so you can use for testing different fetch from graphhql
app.get("/login", (req, res, next) => {
res.json({
1: jwt.sign(
{ role: "user_login", user_id: 1 },
configData.auth.secret,
{}
),
2: jwt.sign(
{ role: "user_login", user_id: 2 },
configData.auth.secret,
{}
),
3: jwt.sign(
{ role: "user_login", user_id: 3 },
configData.auth.secret,
{}
),
admin: jwt.sign(
{ role: "user_admin", user_id: 0 },
configData.auth.secret,
{}
)
});
});
app.listen(configData.app.port);
console.log(`App is running on ${configData.app.port}`);
})
.catch(err => {
console.log(err);
process.exit(1);
});