-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (42 loc) · 1.33 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
import { ApolloServer } from 'apollo-server-express'
import {
ApolloServerPluginDrainHttpServer,
ApolloServerPluginLandingPageGraphQLPlayground,
} from 'apollo-server-core'
import http from 'http'
import dotenv from 'dotenv'
import resolvers from './graphql/resolver.js'
import app from './app.js'
import connectToMongo from './mongo.js'
import typeDefs from './graphql/schema.js'
import applyAuthMiddleware from './middlewares/auth.js'
import { getAndVerifyJWT } from './jwt.js'
dotenv.config()
async function startServer() {
await connectToMongo()
const httpServer = http.createServer(app)
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }),
ApolloServerPluginLandingPageGraphQLPlayground(),
],
context: ({ req }) => {
const payload = getAndVerifyJWT(req)
let user = payload?.user
//console.log(user)
return {
user,
req,
}
},
})
await server.start()
server.applyMiddleware({ app })
await new Promise((resolve) =>
httpServer.listen({ port: process.env.PORT || 8080 }, resolve)
)
console.log(`🚀 Server ready at http://localhost:8080${server.graphqlPath}`)
}
startServer()