-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (53 loc) · 1.46 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
require('now-env')
require('dotenv').config()
const massive = require('massive')
// const monitor = require('pg-monitor')
const { ApolloServer, ApolloError } = require('apollo-server')
const jwt = require('jsonwebtoken')
const typeDefs = require('./schema')
const resolvers = require('./resolvers')
massive({
// poolsize: 20, // default is 10
host: process.env.PGHOST,
port: process.env.PGPORT,
database: process.env.PGDATABASE,
user: process.env.PGUSER,
password: process.env.PGPASSWORD
})
.then(db => {
// monitor.attach(db.driverConfig)
const server = new ApolloServer({
typeDefs,
resolvers,
formatError: error => {
console.log(error)
return error
},
formatResponse: response => {
console.log(response)
return response
},
context: ({ req }) => {
let bearer, token
if (req.headers.authorization) {
;[bearer, token] = req.headers.authorization.split(' ')
}
let user
if (bearer === 'Bearer' && token) {
try {
user = jwt.verify(token, process.env.JWS_SECRET)
} catch (err) {
if (req.body.query.includes('mutation')) {
throw new ApolloError('Unauthorized', 401)
}
}
}
return { db, user }
},
cors: true
})
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
})
.catch(console.error)