-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathserver.js
50 lines (42 loc) · 1.02 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
import Hapi from 'hapi';
import {graphql} from 'graphql';
import {promisify} from 'bluebird';
import {HOST, PORT} from './config';
import SqlitePlugin from './SqlitePlugin';
import Schema from './Schema';
async function graphQLHandler(request, reply) {
const {query, variables = {}} = request.payload;
const result = await graphql(
Schema,
query,
{
db: request.db,
userId: '1'
},
variables
);
return reply(result);
}
export default async function runServer() {
try {
const server = new Hapi.Server();
// Make server methods promise friendly
for (const method of ['register', 'start']) {
server[method] = promisify(server[method], server);
}
server.connection({
host: HOST,
port: PORT
});
await server.register(SqlitePlugin);
server.route({
method: 'POST',
path: '/',
handler: graphQLHandler
});
await server.start();
console.log('Server started at ' + server.info.uri);
} catch(e) {
console.log(e);
}
}