Repository navigation
Expand file tree
/
Copy pathabsintheSocketLink.js
More file actions
150 lines (131 loc) · 5.46 KB
/
Copy pathabsintheSocketLink.js
File metadata and controls
150 lines (131 loc) · 5.46 KB
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
import { ApolloLink, Observable } from '@apollo/client';
import { print } from 'graphql';
import { Socket as PhoenixSocket } from 'phoenix';
// Apollo link speaking Absinthe's Phoenix channel protocol, replacing the
// abandoned @absinthe/socket-apollo-link. That package was last published in
// 2022 and builds its link on apollo-link 1.x, which no longer survives
// Metro's bundling. The protocol itself is small and stable:
//
// join "__absinthe__:control"
// push "doc" {query, variables}
// -> ok {data, errors} for queries and mutations
// -> ok {subscriptionId} for subscriptions
// event "subscription:data" {subscriptionId, result}
// push "unsubscribe" {subscriptionId}
//
// See absinthe_phoenix's Absinthe.Phoenix.Channel and Absinthe.Phoenix.Endpoint.
const CONTROL_TOPIC = '__absinthe__:control';
const DATA_EVENT = 'subscription:data';
function describe(payload) {
if (!payload) return 'unknown error';
if (typeof payload === 'string') return payload;
if (Array.isArray(payload.errors)) return payload.errors.map((e) => e.message).join(', ');
return payload.error || payload.reason || JSON.stringify(payload);
}
export function createAbsintheSocketLink(endpoint, socketOptions) {
const socket = new PhoenixSocket(endpoint, socketOptions);
// Absinthe's subscription id -> {doc, observer}. The document is kept so a
// subscription can be re-established after a reconnect, which issues a new id.
const subscriptions = new Map();
let channel = null;
let joined = null;
// Registered once per link. Registering it inside join() would add a second
// listener on every retry after a failed join, and phoenix invokes every
// listener it holds, so one timed-out join would double every later result.
//
// Subscription results arrive tagged with the subscription's own topic
// rather than the control channel's, so a channel-level listener never
// matches them. Watch the socket instead, the way @absinthe/socket did.
socket.onMessage(({ event, payload }) => {
if (event !== DATA_EVENT || !payload) return;
const entry = subscriptions.get(payload.subscriptionId);
if (entry) entry.observer.next(payload.result);
});
// Absinthe discards a subscription when its channel process exits, and
// phoenix rejoins the channel silently after a reconnect. Without this every
// subscription goes quiet for good, with no error for Apollo to surface.
// Pushes are buffered until the rejoin lands, so this can be sent right away.
socket.onOpen(() => {
if (!channel) return;
for (const entry of [...subscriptions.values()]) {
channel.push('doc', entry.doc).receive('ok', (payload) => {
if (!payload || !payload.subscriptionId) return;
subscriptions.delete(entry.id);
entry.id = payload.subscriptionId;
subscriptions.set(entry.id, entry);
});
}
});
function join() {
if (joined) return joined;
socket.connect();
channel = socket.channel(CONTROL_TOPIC, {});
joined = new Promise((resolve, reject) => {
channel
.join()
.receive('ok', () => resolve(channel))
.receive('error', (payload) => reject(new Error(describe(payload))))
.receive('timeout', () => reject(new Error(`Timed out joining ${CONTROL_TOPIC}`)));
}).catch((error) => {
// Let the next operation retry instead of caching the rejection forever.
joined = null;
channel = null;
throw error;
});
return joined;
}
return new ApolloLink(
(operation) =>
new Observable((observer) => {
let cancelled = false;
// Held by reference rather than by id, because a reconnect replaces the
// id in place and the teardown below has to unsubscribe the current one.
let entry = null;
const doc = {
query: print(operation.query),
variables: operation.variables || {},
};
join()
.then((control) => {
if (cancelled) return;
control
.push('doc', doc)
.receive('ok', (payload) => {
if (cancelled) return;
if (payload && payload.subscriptionId) {
entry = { id: payload.subscriptionId, doc, observer };
subscriptions.set(entry.id, entry);
return; // stays open until unsubscribed
}
observer.next(payload);
observer.complete();
})
.receive('error', (payload) => {
if (cancelled) return;
// GraphQL errors belong in the result so Apollo's error link
// sees them as graphQLErrors; anything else is a transport fault.
if (payload && payload.errors) {
observer.next(payload);
observer.complete();
} else {
observer.error(new Error(describe(payload)));
}
})
.receive('timeout', () => {
if (!cancelled) observer.error(new Error('Timed out running document'));
});
})
.catch((error) => {
if (!cancelled) observer.error(error);
});
return () => {
cancelled = true;
if (entry) {
subscriptions.delete(entry.id);
if (channel) channel.push('unsubscribe', { subscriptionId: entry.id });
entry = null;
}
};
})
);
}