-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (55 loc) · 1.43 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
import express from 'express'
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
import bodyParser from 'body-parser'
import compression from 'compression'
import StundentLoader from './graphql/loaders/student'
import SchoolLoader from './graphql/loaders/school'
import cors from 'cors'
import schema from './graphql/schema'
import { Engine } from 'apollo-engine'
const mongoose = require('./config/mongoose')
const db = mongoose()
const GRAPHQL_PORT = 4000
const ENGINE_API_KEY = '' // KEY
const engine = new Engine({
engineConfig: {
apiKey: ENGINE_API_KEY
},
graphqlPort: GRAPHQL_PORT
})
engine.start()
const graphQLServer = express()
const loaders = {
student: StundentLoader,
school: SchoolLoader
}
const helperMiddleware = [
bodyParser.json(),
bodyParser.text({ type: 'application/graphql' }),
(req, res, next) => {
if (req.is('application/graphql')) {
req.body = { query: req.body }
}
next()
}
]
graphQLServer.use(engine.expressMiddleware())
graphQLServer.use('*', cors())
graphQLServer.use(compression())
graphQLServer.use('/graphql',
...helperMiddleware,
graphqlExpress({
schema,
graphiql: true,
tracing: false,
context: {
loaders
}
})
)
graphQLServer.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }))
graphQLServer.listen(GRAPHQL_PORT, () =>
console.log(
`GraphiQL is now running on http://localhost:${GRAPHQL_PORT}/graphiql`
)
)