forked from pagesource/twitter-graphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (97 loc) · 2.6 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import chokidar from 'chokidar';
import express from 'express';
import graphQLHTTP from 'express-graphql';
import path from 'path';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import { clean } from 'require-clean';
import { exec } from 'child_process';
import constants from './constants';
const APP_PORT = constants.devPort;
const GRAPHQL_PORT = constants.GraphQLPort;
let graphQLServer;
let appServer;
function startAppServer(callback) {
// Serve the Relay app
const compiler = webpack({
entry: path.resolve(__dirname, 'js', 'app.js'),
module: {
loaders: [
{
exclude: /node_modules/,
loader: 'babel',
test: /\.js$/,
},
{
exclude: /node_modules/,
loader: 'style!css',
test: /\.css$/,
}
]
},
output: { filename: '/app.js', path: '/', publicPath: '/js/' }
});
appServer = new WebpackDevServer(compiler, {
contentBase: '/public/',
proxy: { '/graphql': `http://localhost:${GRAPHQL_PORT}` },
publicPath: '/js/',
stats: { colors: true }
});
// Serve static resources
appServer.use('/', express.static(path.resolve(__dirname, 'public')));
appServer.listen(APP_PORT, () => {
console.log(`App is now running on http://localhost:${APP_PORT}`);
if (callback) {
callback();
}
});
}
function startGraphQLServer(callback) {
// Expose a GraphQL endpoint
clean('./gql-schema/schema');
const { Schema } = require('./gql-schema/schema');
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
}));
graphQLServer = graphQLApp.listen(GRAPHQL_PORT, () => {
console.log(
`GraphQL server is now running on http://localhost:${GRAPHQL_PORT}`
);
if (callback) {
callback();
}
});
}
function startServers(callback) {
// Shut down the servers
if (appServer) {
appServer.listeningApp.close();
}
if (graphQLServer) {
graphQLServer.close();
}
// Compile the schema
exec('npm run update-schema', (error, stdout) => {
console.log(stdout);
let doneTasks = 0;
function handleTaskDone() {
doneTasks++;
if (doneTasks === 2 && callback) {
callback();
}
}
startGraphQLServer(handleTaskDone);
startAppServer(handleTaskDone);
});
}
const watcher = chokidar.watch('./gql-schema/{database,schema}.js');
watcher.on('change', path => {
console.log(`\`${path}\` changed. Restarting.`);
startServers(() =>
console.log('Restart your browser to use the updated schema.')
);
});
startServers();