-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
51 lines (47 loc) · 1.44 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
require('babel-polyfill');
require('babel-register');
var webpack = require('webpack')
var config = require('./webpack.config')
var express = require('express');
var app = new express();
var port = 3000
var graphql = require('graphql').graphql
var Schema = require('./src/schema');
var bodyParser = require('body-parser');
const compiler = webpack(config);
const delay = () => new Promise(resolve => setTimeout(resolve, 1000));
app.use(bodyParser.json());
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
app.use('/static', express.static('static'));
app.use('/graphql', function(req, res) {
// const {query, variables} = req.body;
const query = req.body.query;
const variables = req.body.variables;
const authToken = req.user || {};
const context = {authToken};
graphql(Schema, query, null, context, variables)
.then(result => {
if (result.errors) {
console.log(query)
console.log('DEBUG GraphQL Error:', result.errors);
}
return result;
})
.then(result => {
delay().then(() => res.send(result))
})
});
app.get('*', function(req, res) {
res.sendFile(__dirname + '/index.html')
});
app.listen(port, function(error) {
if (error) {
console.error(error)
} else {
console.info("==> Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port)
}
})