-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·36 lines (31 loc) · 1 KB
/
server.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
const { ApolloServer} = require('apollo-server')
const mongoose = require("mongoose")
require("dotenv").config();
const typeDefs = require("./typeDefs")
const resolvers = require("./resolvers")
const {findOrCreateUser} = require('./controllers/userController')
mongoose
.connect(process.env.MONGO_URI, {useNewUrlParser: true })
.then(() => console.log("DB connected!"))
.catch(err => console.error(err))
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({req}) => {
let authToken = null
let currentUser = null
try{
authToken = req.headers.authorization
if (authToken) {
currentUser = await findOrCreateUser(authToken)
}
}
catch (err){
console.error(`Unable to authenticate user with token ${authToken}`)
}
return { currentUser }
}
})
server.listen({ port: process.env.PORT || 4000}).then(({ url }) =>{
console.log(`Server listening on ${url}`)
})