forked from giraffQL/giraffQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (53 loc) · 1.82 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
const express = require('express');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const app = express();
const path = require('path')
const bodyParser = require('body-parser');
const graphQLHTTP = require('express-graphql');
const { GraphQLSchema, buildSchema, printSchema } = require('graphql');
const { memoryBlobStore } = require('./src/components/graphql/memoryBlobStore');
const { mockSchema } = require('./src/components/graphql/mockSchema');
const compiler = webpack(webpackConfig);
const blobStore = new memoryBlobStore();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.all('/schemas/:schemaId/graphql', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if ('OPTIONS' == req.method) {
res.send(200);
} else {
blobStore.fetch(req.params.schemaId).then((blob) => {
if (blob) {
const schema = mockSchema(blob);
graphQLHTTP({schema: schema, graphiql: true})(req, res);
} else {
res.send(404);
}
});
}
});
app.post('/schemas', (req, res) => {
const blob = req.body.schema;
const schema = buildSchema(blob);
const normalizedSchema = printSchema(schema);
blobStore.add(normalizedSchema).then((digest) => {
res.redirect(`/schemas/${digest}/graphql`);
});
});
app.use(webpackDevMiddleware(compiler, {
hot: true,
filename: 'bundle.js',
publicPath: '/',
stats: {
colors: true,
},
historyApiFallback: true,
}));
const server = app.listen(process.env.PORT || 8000, function() {
const port = server.address().port;
console.log(`Is it over ${port}?!?!`);
});