-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (37 loc) · 1.13 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
36
37
38
39
40
41
42
43
44
45
46
import { ApolloServer } from 'apollo-server'
import { ApolloServerPluginLandingPageGraphQLPlayground} from 'apollo-server-core'//playground
import typeDefs from './gqlSchema.js'
import mongoose from 'mongoose'
import { JWT_TOKEN, MONGO_URL } from './config.js'
import jwt from 'jsonwebtoken'
mongoose.connect(MONGO_URL,{
useNewUrlParser: true,
useUnifiedTopology:true,
})
mongoose.connection.on("connected", () => {
console.log("Connected to mongodb")
})
mongoose.connection.on("error",(err) => {
console.log("Error Connection", err);
})
//import models here
import './models/User.js'
import './models/Quotes.js'
import resolvers from './resolvers.js'
//This is Middleware
const context = ({req}) => {
const {authorization} = req.headers
if(authorization) {
const {userId} = jwt.verify(authorization, JWT_TOKEN);
return {userId}
}
}
const server = new ApolloServer({
typeDefs,
resolvers,
context,
plugins:[
ApolloServerPluginLandingPageGraphQLPlayground()
]
})
server.listen(8000).then(({url}) =>console.log(`Server Ready At ${url}`))