-
Notifications
You must be signed in to change notification settings - Fork 11
/
relay.js
109 lines (98 loc) · 2.76 KB
/
relay.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
/* eslint-env browser */
import 'isomorphic-fetch';
import Relay from 'relay-runtime';
import socketio from 'socket.io-client';
// ===================================================
// Main
// ===================================================
let environment;
let inspector;
const createEnvironment = (
records,
fetchQuery = defaultFetchQuery,
subscribe = defaultSubscribe
) => {
const source = new Relay.RecordSource(records);
const store = new Relay.Store(source);
environment = new Relay.Environment({
network:
subscribe != null
? Relay.Network.create(fetchQuery, subscribe)
: Relay.Network.create(fetchQuery),
store,
});
// Debugging
inspector =
process.env.NODE_ENV !== 'production'
? new Relay.RecordSourceInspector(source)
: null;
return environment;
};
const getEnvironment = () => environment;
const getInspector = () => inspector;
// ===================================================
// Default network layer
// ===================================================
let socket;
// Map of operation name to observers ({ onNext, onError, onCompleted })
const subscriptionObservers = {};
if (!process.env.SERVER_SIDE_RENDERING) {
socket = socketio.connect('/mady');
socket.on('MESSAGE', msg => {
const { type } = msg;
if (type === 'NOTIF') {
rxNotif(msg);
}
});
}
const rxNotif = msg => {
// console.log('NOTIF', msg);
const { subscriptionId, payload } = msg;
const observers = subscriptionObservers[subscriptionId];
if (!observers) return;
observers.forEach(({ onNext }) => {
onNext(payload);
});
};
const defaultFetchQuery = async (operation, variables) => {
const response = await fetch('/mady-graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}, // Add authentication and other headers here
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
});
return response.json();
};
const defaultSubscribe = async (
operation,
variables,
_ /* cacheConfig -- will always receive null */,
observer
) => {
// console.log(operation)
if (!socket) {
observer.onError(new Error('No socket available for subscriptions'));
return;
}
const subscriptionId = operation.name;
if (!subscriptionObservers[subscriptionId]) {
subscriptionObservers[subscriptionId] = [];
socket.emit('MESSAGE', {
type: 'SUBSCRIBE',
subscriptionId,
query: operation.text,
variables,
});
}
const observers = subscriptionObservers[subscriptionId];
observers.push(observer);
};
// ===================================================
// Public
// ===================================================
export default createEnvironment;
export { getEnvironment, getInspector };