-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (38 loc) · 1.16 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
const express = require('express')
const { ApolloServer } = require('apollo-server-express')
const cors = require('cors')
const dotEnv = require('dotenv')
const Dataloader = require('dataloader')
const resolvers = require('./resolvers')
const typeDefs = require('./typeDefs')
const { connection } = require('./database/util')
const { verifyUser } = require('./helper/context')
const loaders = require('./loaders')
dotEnv.config()
const app = express()
connection()
app.use(express.json())
app.use(cors())
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => {
await verifyUser(req)
return {
email: req.email,
loggedInUserId: req.loggedInUserId,
loaders: {
user: new Dataloader(keys => loaders.user.batchUsers(keys))
}
}
}
})
apolloServer.applyMiddleware({ app, path: '/graphql'})
const PORT = process.env.PORT || 3000
app.use('/', (req, res) => {
res.json({ status: 'OK' })
} )
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`)
console.log(`Grahql endpoint listening on ${apolloServer.graphqlPath}`)
})