forked from relay-tools/relay-subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
157 lines (130 loc) · 3.96 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* This file provided by Facebook is for non-commercial testing and evaluation
* purposes only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* eslint-disable no-console */
import express from 'express';
import graphQLHTTP from 'express-graphql';
import { graphql } from 'graphql';
import { graphqlSubscribe } from 'graphql-relay-subscription';
import path from 'path';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import { addNotifier } from './data/database';
import { schema } from './data/schema';
const APP_PORT = 3000;
const GRAPHQL_PORT = 8080;
// Expose a GraphQL endpoint
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({ schema, pretty: true, graphiql: true }));
const graphQLServer = graphQLApp.listen(GRAPHQL_PORT, () => {
console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}`
);
});
const io = require('socket.io')(graphQLServer, {
serveClient: false,
});
io.on('connection', socket => {
const topics = Object.create(null);
const unsubscribeMap = Object.create(null);
const removeNotifier = addNotifier(({ topic, data }) => {
const topicListeners = topics[topic];
if (!topicListeners) return;
topicListeners.forEach(({ id, query, variables }) => {
graphql(
schema,
query,
data,
null,
variables
).then((result) => {
socket.emit('subscription update', { id, ...result });
});
});
});
socket.on('subscribe', ({ id, query, variables }) => {
function unsubscribe(topic, subscription) {
const index = topics[topic].indexOf(subscription);
if (index === -1) return;
topics[topic].splice(index);
console.log(
'Removed subscription for topic %s. Total subscriptions for topic: %d',
topic,
topics[topic].length
);
}
function subscribe(topic) {
topics[topic] = topics[topic] || [];
const subscription = { id, query, variables };
topics[topic].push(subscription);
unsubscribeMap[id] = () => {
unsubscribe(topic, subscription);
};
console.log(
'New subscription for topic %s. Total subscriptions for topic: %d',
topic,
topics[topic].length
);
}
graphqlSubscribe({
schema,
query,
variables,
context: { subscribe },
}).then((result) => {
if (result.errors) {
console.error('Subscribe failed', result.errors);
}
});
});
socket.on('unsubscribe', (id) => {
const unsubscribe = unsubscribeMap[id];
if (!unsubscribe) return;
unsubscribe();
delete unsubscribeMap[id];
socket.emit('subscription closed', id);
});
socket.on('disconnect', () => {
console.log('Socket disconnect');
removeNotifier();
});
});
// Serve the Relay app.
const compiler = webpack({
entry: [
'babel-polyfill',
'./js/app.js',
],
output: {
path: '/',
filename: 'app.js',
},
module: {
rules: [
{ test: /\.js$/, exclude: /node_modules/, use: 'babel-loader' },
],
},
devtool: 'sourcemap',
});
const app = new WebpackDevServer(compiler, {
contentBase: '/public/',
proxy: {
'/graphql': `http://localhost:${GRAPHQL_PORT}`,
'/socket.io': `http://localhost:${GRAPHQL_PORT}`,
},
publicPath: '/js/',
stats: { colors: true },
});
// Serve static resources
app.use('/', express.static(path.join(__dirname, 'public')));
app.listen(APP_PORT, () => {
console.log(`App is now running on http://localhost:${APP_PORT}`);
});