-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
70 lines (46 loc) · 1.54 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require('express')
const { ApolloServer, gql, AddArgumentsAsVariables } = require('apollo-server-express')
//cross origin request handle
const cors = require('cors')
const dotenv = require('dotenv')
const {connection} = require('./database/util')
const {verifyUser} = require('./helper/context')
const resolvers = require('./Resolvers')
const typeDefs = require('./typeDefs')
const { keys } = require('./typeDefs')
dotenv.config()
//db connectivity
connection()
const app = express()
//body parser middleware
app.use(cors())
app.use(express.json());
const apolloServer = new ApolloServer({
typeDefs,//schema
resolvers,//how to get data for particular schema
context: async({req, connection})=> {//if we want to get something as a per request basis then we declare context func
const contextObj = {}
if (req){
await verifyUser(req)
contextObj.email= req.email,
contextObj.loggedInUserId = req.loggedInUserId
}
return contextObj
},
formatError: (error) => {
return {
message: error.message
}
}
})
apolloServer.applyMiddleware({app, path:'/graphql'})
//if the port 3001 is not found then 3000 will be the default port
const PORT= process.env.PORT || 3000
app.use('/' , (req,res,next) =>{
res.send({message: 'Hello'})
})
const httpServer = app.listen(PORT, () =>{
console.log(`Server listening on PORT: ${PORT}`)
console.log(`Graphql Endpoint: ${apolloServer.graphqlPath}`)
})
apolloServer.installSubscriptionHandlers(httpServer)