-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
89 lines (73 loc) · 1.93 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict'
const Hapi = require('hapi')
const { graphqlHapi, graphiqlHapi } = require('apollo-server-hapi')
const hapiPlayground = require('graphql-playground-middleware-hapi').default
const mongoose = require('mongoose')
const { makeExecutableSchema } = require('graphql-tools');
const { getUserId } = require('./utils')
const tabs = require('./tabs.js')
const HOST = 'server'
const PORT = 3030
const User = require('./models/user')
const Project = require('./models/project')
const Task = require('./models/task')
const myGraphQLSchema = require('./graphql/schema')
const createResolvers = require('./graphql/resolvers')
const executableSchema = makeExecutableSchema({
typeDefs: [myGraphQLSchema],
resolvers: createResolvers({ User, Project, Task }),
introspection: true,
});
const api = {
plugin: graphqlHapi,
options: {
path: '/graphql',
graphqlOptions: request => ({
schema: executableSchema,
context: request
}),
route: {
cors: true,
},
},
}
const graphiql = {
plugin: hapiPlayground,
options: {
path: '/graphiql',
tabs,
endpoint: '/graphql'
},
}
const endpoints = [api, graphiql]
const init = async () => {
const server = Hapi.server({
host: HOST,
port: PORT,
})
try {
await server.register(endpoints)
await server.start()
} catch (err) {
console.log(`Error while starting server: ${err.message}`)
}
console.log(`Server running at: ${server.info.uri}`)
}
process.on('unhandledRejection', (err) => {
console.log(err)
process.exit(1)
})
const connectToMongo = async () => {
try {
await mongoose.connect('mongodb://mongo:27017/kickit_db1', (err) => {
if (err) {
setTimeout(connectToMongo, 5000)
} else {
init()
}
})
} catch (err) {
console.error('Error while connecting to mongo DB - retrying in 5 seconds...')
}
}
connectToMongo()