-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
130 lines (108 loc) · 4 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
import { Meteor } from 'meteor/meteor';
const SUBSCRIPTION_NAME = '__graphql_subscription__';
const SUBSCRIPTION_METHOD_NAME = '__graphql_subscribe__';
const UNSUBSCRIPTION_METHOD_NAME = '__graphql_unsubscribe__';
export class SubscriptionServer {
/**
* @param {SubscriptionManager} subscriptionManager
* @param {Function} [onSubscribe]
*/
constructor({ subscriptionManager, onSubscribe }) {
if (!subscriptionManager) {
throw new Error('Must provide `subscriptionManager` to meteor subscription server constructor.');
}
this.subscriptionManager = subscriptionManager;
this.onSubscribe = onSubscribe;
const connections = this.connections = [];
const self = this;
Meteor.publish(SUBSCRIPTION_NAME, function () {
const subscription = this;
const connectionId = subscription.connection.id;
connections[connectionId] = { subscription, subscriptionIds : {} };
subscription.onStop(() => {
Object.keys(connections[connectionId].subscriptionIds).forEach(subscriptionId => {
self.unsubscribe({subscriptionId, connectionId});
});
delete connections[connectionId];
});
subscription.ready();
});
Meteor.methods({
[SUBSCRIPTION_METHOD_NAME]({query, variables, operationName, context, id}) {
self.subscribe(
{
query,
variables,
operationName,
context
}, {
subscriptionId: id,
connectionId: this.connection.id,
userId: this.userId
}
);
},
[UNSUBSCRIPTION_METHOD_NAME]({id}) {
self.unsubscribe({
subscriptionId: id,
connectionId: this.connection.id
});
}
});
}
/**
* @param {String} query
* @param {Object} [variables]
* @param {String} operationName
* @param {Object} [context]
* @param {Number} subscriptionId
* @param {String} connectionId
* @param {String} [userId]
*/
async subscribe({ query, variables, operationName, context = {}}, {subscriptionId, connectionId, userId}) {
let params = { query, variables, operationName, context };
if (userId) {
context.userId = userId;
context.user = await Meteor.users.findOne({ _id: userId });
}
if (this.onSubscribe) {
params = this.onSubscribe(Object.assign({}, params, { subscriptionId, connectionId }), params, this);
}
if (this.connections[connectionId].subscriptionIds[subscriptionId]) {
this.subscriptionManager.unsubscribe(this.connections[connectionId].subscriptionIds[subscriptionId]);
delete this.connections[connectionId].subscriptionIds[subscriptionId];
}
const subscription = this.connections[connectionId].subscription;
params.callback = (error, result) => {
if (!error) {
this.sendData({subscription, subscriptionId, data: result.data});
} else if (error.errors) {
this.sendData({subscription, subscriptionId, errors: error.errors });
} else {
this.sendData({subscription, subscriptionId, errors: [{message: error.message }]});
}
};
const graphqlSubId = await this.subscriptionManager.subscribe(params);
this.connections[connectionId].subscriptionIds[subscriptionId] = graphqlSubId;
}
/**
* @param {Number} subscriptionId
* @param {String} connectionId
*/
unsubscribe({ subscriptionId, connectionId }) {
if (typeof this.connections[connectionId].subscriptionIds[subscriptionId] !== 'undefined') {
this.subscriptionManager.unsubscribe(this.connections[connectionId].subscriptionIds[subscriptionId]);
delete this.connections[connectionId].subscriptionIds[subscriptionId];
}
}
/**
* @param {Object} subscription
* @param {Number} subscriptionId
* @param {Object} [data]
* @param {Array} [errors]
* @private
*/
sendData({subscription, subscriptionId, data = null, errors = null}) {
subscription._session.sendChanged(SUBSCRIPTION_NAME, subscriptionId.toString(), { data, errors });
}
}