@hyper-fetch/graphql 10.0.0 → 10.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +152 -84
- package/__tests__/e2e/e2e.conformance.spec.ts +206 -0
- package/__tests__/e2e/e2e.graphql-ws-server.ts +187 -0
- package/__tests__/e2e/e2e.graphql-ws.spec.ts +306 -0
- package/__tests__/e2e/e2e.react-subscriptions.spec.tsx +208 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +23 -5
- package/__tests__/features/adapter/adapter.document.spec.ts +17 -13
- package/__tests__/features/adapter/adapter.headers.spec.ts +65 -0
- package/__tests__/features/adapter/adapter.keys.spec.ts +235 -0
- package/__tests__/features/adapter/adapter.persisted-queries.spec.ts +207 -0
- package/__tests__/features/adapter/adapter.progress.spec.ts +39 -0
- package/__tests__/features/adapter/adapter.responses.spec.ts +162 -1
- package/__tests__/features/adapter/adapter.server.spec.ts +6 -6
- package/__tests__/features/adapter/adapter.types.spec.ts +121 -2
- package/__tests__/features/adapter/adapter.utils.spec.ts +4 -2
- package/__tests__/features/adapter/adapter.variables.spec.ts +3 -3
- package/__tests__/features/socket/socket.connection.spec.ts +892 -0
- package/__tests__/features/socket/socket.fuzz.spec.ts +329 -0
- package/__tests__/features/socket/socket.subscriptions.spec.ts +515 -0
- package/__tests__/features/socket/socket.types.spec.ts +160 -0
- package/__tests__/utils/controlled-websocket.ts +238 -0
- package/dist/adapter/adapter.constants.d.ts +5 -0
- package/dist/adapter/adapter.constants.d.ts.map +1 -1
- package/dist/adapter/adapter.types.d.ts +55 -2
- package/dist/adapter/adapter.types.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts +51 -4
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +958 -128
- package/dist/index.mjs.map +1 -1
- package/dist/socket/index.d.ts +5 -0
- package/dist/socket/index.d.ts.map +1 -0
- package/dist/socket/socket.constants.d.ts +44 -0
- package/dist/socket/socket.constants.d.ts.map +1 -0
- package/dist/socket/socket.subscriptions.d.ts +74 -0
- package/dist/socket/socket.subscriptions.d.ts.map +1 -0
- package/dist/socket/socket.types.d.ts +102 -0
- package/dist/socket/socket.types.d.ts.map +1 -0
- package/dist/socket/socket.utils.d.ts +80 -0
- package/dist/socket/socket.utils.d.ts.map +1 -0
- package/dist/socket/socket.websocket.d.ts +30 -0
- package/dist/socket/socket.websocket.d.ts.map +1 -0
- package/package.json +17 -18
- package/src/adapter/adapter.constants.ts +6 -0
- package/src/adapter/adapter.types.ts +69 -3
- package/src/adapter/adapter.utils.ts +196 -25
- package/src/adapter/http-adapter.fetch.ts +93 -43
- package/src/index.ts +1 -0
- package/src/socket/index.ts +4 -0
- package/src/socket/socket.constants.ts +59 -0
- package/src/socket/socket.subscriptions.ts +160 -0
- package/src/socket/socket.types.ts +118 -0
- package/src/socket/socket.utils.ts +150 -0
- package/src/socket/socket.websocket.ts +555 -0
- package/vite.config.ts +4 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
2
|
+
import gql from "graphql-tag";
|
|
3
|
+
|
|
4
|
+
import type { GraphqlWsAdapterOptionsType, GraphqlWsExtraType } from "../../../src";
|
|
5
|
+
import { GraphqlSubscriptionError, GraphqlWsAdapter } from "../../../src";
|
|
6
|
+
import {
|
|
7
|
+
acknowledgeWebSocket,
|
|
8
|
+
flushPromises,
|
|
9
|
+
installControlledWebSocket,
|
|
10
|
+
openWebSocket,
|
|
11
|
+
uninstallControlledWebSocket,
|
|
12
|
+
} from "../../utils/controlled-websocket";
|
|
13
|
+
|
|
14
|
+
type Message = { onMessage: { text: string } };
|
|
15
|
+
type MessageVariables = { room: string };
|
|
16
|
+
type Event<T> = { data: T; extra: GraphqlWsExtraType };
|
|
17
|
+
|
|
18
|
+
const url = "ws://localhost:4000/graphql";
|
|
19
|
+
|
|
20
|
+
const onMessageDocument = gql`
|
|
21
|
+
subscription OnMessage($room: String!) {
|
|
22
|
+
onMessage(room: $room) {
|
|
23
|
+
text
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
`;
|
|
27
|
+
const onMessageQuery = `subscription OnMessage($room: String!) {
|
|
28
|
+
onMessage(room: $room) {
|
|
29
|
+
text
|
|
30
|
+
}
|
|
31
|
+
}`;
|
|
32
|
+
|
|
33
|
+
const createSocket = (adapterOptions?: GraphqlWsAdapterOptionsType) => {
|
|
34
|
+
const socket = new Socket({
|
|
35
|
+
url,
|
|
36
|
+
adapter: GraphqlWsAdapter(),
|
|
37
|
+
adapterOptions: adapterOptions ?? { autoConnect: false },
|
|
38
|
+
reconnectTime: 100,
|
|
39
|
+
});
|
|
40
|
+
const errors: Error[] = [];
|
|
41
|
+
socket.onError(({ error }) => {
|
|
42
|
+
errors.push(error as Error);
|
|
43
|
+
});
|
|
44
|
+
const onMessage = socket.createListener<Message, MessageVariables>()({
|
|
45
|
+
topic: "OnMessage",
|
|
46
|
+
options: { document: onMessageDocument },
|
|
47
|
+
});
|
|
48
|
+
const onTick = socket.createListener<{ tick: number }>()({ topic: "subscription OnTick { tick }" });
|
|
49
|
+
return { socket, errors, onMessage, onTick };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
describe("Graphql Ws Adapter [ Subscriptions ]", () => {
|
|
53
|
+
// The lower end of the reconnection jitter - every delay is exactly `reconnectTime`
|
|
54
|
+
let random = vi.spyOn(Math, "random");
|
|
55
|
+
let setup = createSocket();
|
|
56
|
+
|
|
57
|
+
const connect = async (current = setup) => {
|
|
58
|
+
current.socket.connect();
|
|
59
|
+
return acknowledgeWebSocket();
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
random = vi.spyOn(Math, "random").mockReturnValue(0);
|
|
64
|
+
installControlledWebSocket();
|
|
65
|
+
setup = createSocket();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterEach(async () => {
|
|
69
|
+
await setup.socket.disconnect();
|
|
70
|
+
vi.useRealTimers();
|
|
71
|
+
uninstallControlledWebSocket();
|
|
72
|
+
random.mockRestore();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("when starting a subscription", () => {
|
|
76
|
+
it("should send the document, the operation name and the variables", async () => {
|
|
77
|
+
const ws = await connect();
|
|
78
|
+
|
|
79
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
80
|
+
|
|
81
|
+
expect(ws.subscriptions).toStrictEqual([
|
|
82
|
+
{
|
|
83
|
+
id: "1",
|
|
84
|
+
type: "subscribe",
|
|
85
|
+
payload: { query: onMessageQuery, operationName: "OnMessage", variables: { room: "a" } },
|
|
86
|
+
},
|
|
87
|
+
]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("should send the topic as the document when the document option is omitted", async () => {
|
|
91
|
+
const ws = await connect();
|
|
92
|
+
|
|
93
|
+
setup.onTick.listen(vi.fn());
|
|
94
|
+
|
|
95
|
+
expect(ws.subscriptions).toStrictEqual([
|
|
96
|
+
{ id: "1", type: "subscribe", payload: { query: "subscription OnTick { tick }", operationName: "OnTick" } },
|
|
97
|
+
]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should send no operation name for an anonymous document", async () => {
|
|
101
|
+
const ws = await connect();
|
|
102
|
+
|
|
103
|
+
setup.socket.createListener<{ tick: number }>()({ topic: "subscription { tick }" }).listen(vi.fn());
|
|
104
|
+
|
|
105
|
+
expect(ws.lastSubscription.payload).toStrictEqual({ query: "subscription { tick }" });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should send the subscriptions only after the server acknowledged the connection", async () => {
|
|
109
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
110
|
+
setup.socket.connect();
|
|
111
|
+
const ws = await openWebSocket();
|
|
112
|
+
|
|
113
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
114
|
+
|
|
115
|
+
ws.serverAck();
|
|
116
|
+
await flushPromises();
|
|
117
|
+
|
|
118
|
+
expect(ws.subscriptions).toHaveLength(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should share one server subscription between the listeners of the same variables", async () => {
|
|
122
|
+
const ws = await connect();
|
|
123
|
+
const first = vi.fn();
|
|
124
|
+
const second = vi.fn();
|
|
125
|
+
|
|
126
|
+
const stopFirst = setup.onMessage.setPayload({ room: "a" }).listen(first);
|
|
127
|
+
const stopSecond = setup.onMessage.listen(second, { payload: { room: "a" } });
|
|
128
|
+
ws.serverNext("1", { data: { onMessage: { text: "hi" } } });
|
|
129
|
+
|
|
130
|
+
expect(ws.subscriptions).toHaveLength(1);
|
|
131
|
+
expect(first).toHaveBeenCalledTimes(1);
|
|
132
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
133
|
+
|
|
134
|
+
stopFirst();
|
|
135
|
+
expect(ws.completed).toStrictEqual([]);
|
|
136
|
+
stopSecond();
|
|
137
|
+
expect(ws.completed).toStrictEqual(["1"]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should keep the events of different variables apart", async () => {
|
|
141
|
+
const ws = await connect();
|
|
142
|
+
const roomA = vi.fn();
|
|
143
|
+
const roomB = vi.fn();
|
|
144
|
+
|
|
145
|
+
setup.onMessage.setPayload({ room: "a" }).listen(roomA);
|
|
146
|
+
setup.onMessage.setPayload({ room: "b" }).listen(roomB);
|
|
147
|
+
ws.serverNext("2", { data: { onMessage: { text: "for b" } } });
|
|
148
|
+
|
|
149
|
+
expect(ws.subscriptions.map(({ payload }) => payload.variables)).toStrictEqual([{ room: "a" }, { room: "b" }]);
|
|
150
|
+
expect(roomA).not.toHaveBeenCalled();
|
|
151
|
+
expect(roomB).toHaveBeenCalledWith({
|
|
152
|
+
data: { onMessage: { text: "for b" } },
|
|
153
|
+
extra: { errors: undefined, extensions: {}, payload: { room: "b" } },
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("should keep documents with the same topic apart", async () => {
|
|
158
|
+
const ws = await connect();
|
|
159
|
+
const withText = vi.fn();
|
|
160
|
+
const withAuthor = vi.fn();
|
|
161
|
+
const authorDocument = gql`
|
|
162
|
+
subscription OnMessage($room: String!) {
|
|
163
|
+
onMessage(room: $room) {
|
|
164
|
+
author
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
`;
|
|
168
|
+
|
|
169
|
+
setup.onMessage.setPayload({ room: "a" }).listen(withText);
|
|
170
|
+
setup.onMessage.setOptions({ document: authorDocument }).setPayload({ room: "a" }).listen(withAuthor);
|
|
171
|
+
ws.serverNext("2", { data: { onMessage: { author: "Ann" } } });
|
|
172
|
+
|
|
173
|
+
expect(ws.subscriptions).toHaveLength(2);
|
|
174
|
+
expect(withText).not.toHaveBeenCalled();
|
|
175
|
+
expect(withAuthor).toHaveBeenCalledTimes(1);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("should never reuse an id", async () => {
|
|
179
|
+
const ws = await connect();
|
|
180
|
+
|
|
181
|
+
const stop = setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
182
|
+
stop();
|
|
183
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
184
|
+
|
|
185
|
+
expect(ws.subscriptions.map(({ id }) => id)).toStrictEqual(["1", "2"]);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("should start the listener once the subscription is sent", async () => {
|
|
189
|
+
const onStart = vi.fn();
|
|
190
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn(), { onStart });
|
|
191
|
+
setup.socket.connect();
|
|
192
|
+
const ws = await openWebSocket();
|
|
193
|
+
|
|
194
|
+
expect(onStart).not.toHaveBeenCalled();
|
|
195
|
+
ws.serverAck();
|
|
196
|
+
await flushPromises();
|
|
197
|
+
|
|
198
|
+
expect(onStart).toHaveBeenCalledWith({ topic: "OnMessage" });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("should start a listener joining a running subscription right away", async () => {
|
|
202
|
+
await connect();
|
|
203
|
+
const onStart = vi.fn();
|
|
204
|
+
|
|
205
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
206
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn(), { onStart });
|
|
207
|
+
|
|
208
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("should report a subscription that could not be sent and send it after the next acknowledgement", async () => {
|
|
212
|
+
vi.useFakeTimers();
|
|
213
|
+
const ws = await connect();
|
|
214
|
+
const onStart = vi.fn();
|
|
215
|
+
const failure = new Error("Send failed");
|
|
216
|
+
vi.spyOn(ws, "send").mockImplementationOnce(() => {
|
|
217
|
+
throw failure;
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn(), { onStart });
|
|
221
|
+
|
|
222
|
+
expect(setup.errors).toStrictEqual([failure]);
|
|
223
|
+
expect(onStart).not.toHaveBeenCalled();
|
|
224
|
+
ws.serverClose({ code: 1006 });
|
|
225
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
226
|
+
const next = await acknowledgeWebSocket(2);
|
|
227
|
+
|
|
228
|
+
expect(next.subscriptions).toHaveLength(1);
|
|
229
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("should report a document that can't be printed and still let the listener unsubscribe", async () => {
|
|
233
|
+
const ws = await connect();
|
|
234
|
+
const broken = setup.socket.createListener<unknown>()({
|
|
235
|
+
topic: "Broken",
|
|
236
|
+
options: { document: { kind: "Unknown" } as never },
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const stop = broken.listen(vi.fn());
|
|
240
|
+
|
|
241
|
+
expect(ws.subscriptions).toHaveLength(0);
|
|
242
|
+
expect(setup.errors).toHaveLength(1);
|
|
243
|
+
expect(setup.socket.adapter.listeners.get("Broken")?.size).toBe(1);
|
|
244
|
+
stop();
|
|
245
|
+
expect(setup.socket.adapter.listeners.get("Broken")?.size ?? 0).toBe(0);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("should report variables that can't be serialized", async () => {
|
|
249
|
+
const ws = await connect();
|
|
250
|
+
const listener = setup.socket.createListener<unknown, { id: bigint }>()({ topic: "subscription { tick }" });
|
|
251
|
+
|
|
252
|
+
listener.setPayload({ id: 1n }).listen(vi.fn());
|
|
253
|
+
|
|
254
|
+
expect(ws.subscriptions).toHaveLength(0);
|
|
255
|
+
expect(setup.errors[0]).toBeInstanceOf(TypeError);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
describe("when receiving events", () => {
|
|
260
|
+
it("should deliver the data with the errors and extensions of the event", async () => {
|
|
261
|
+
const ws = await connect();
|
|
262
|
+
const callback = vi.fn<(event: Event<Message | null>) => void>();
|
|
263
|
+
setup.onMessage.setPayload({ room: "a" }).listen(callback);
|
|
264
|
+
|
|
265
|
+
ws.serverNext("1", {
|
|
266
|
+
data: { onMessage: { text: "partial" } },
|
|
267
|
+
errors: [{ message: "Author is unavailable", path: ["onMessage", "author"] }],
|
|
268
|
+
extensions: { cost: 3 },
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
expect(callback).toHaveBeenCalledWith({
|
|
272
|
+
data: { onMessage: { text: "partial" } },
|
|
273
|
+
extra: {
|
|
274
|
+
errors: [{ message: "Author is unavailable", path: ["onMessage", "author"] }],
|
|
275
|
+
extensions: { cost: 3 },
|
|
276
|
+
payload: { room: "a" },
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
expect(setup.errors).toHaveLength(0);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("should deliver null data for an event without data and keep the subscription", async () => {
|
|
283
|
+
const ws = await connect();
|
|
284
|
+
const callback = vi.fn<(event: Event<Message | null>) => void>();
|
|
285
|
+
setup.onMessage.setPayload({ room: "a" }).listen(callback);
|
|
286
|
+
|
|
287
|
+
ws.serverNext("1", { errors: [{ message: "Resolver failed" }] });
|
|
288
|
+
ws.serverNext("1", { data: { onMessage: { text: "next" } } });
|
|
289
|
+
|
|
290
|
+
expect(callback.mock.calls.map(([event]) => event.data)).toStrictEqual([null, { onMessage: { text: "next" } }]);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("should ignore events of a subscription the client stopped", async () => {
|
|
294
|
+
const ws = await connect();
|
|
295
|
+
const callback = vi.fn();
|
|
296
|
+
const stop = setup.onMessage.setPayload({ room: "a" }).listen(callback);
|
|
297
|
+
|
|
298
|
+
stop();
|
|
299
|
+
ws.serverNext("1", { data: { onMessage: { text: "late" } } });
|
|
300
|
+
|
|
301
|
+
expect(callback).not.toHaveBeenCalled();
|
|
302
|
+
expect(setup.errors).toHaveLength(0);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("should ignore a late complete of a stopped subscription that was started again", async () => {
|
|
306
|
+
const ws = await connect();
|
|
307
|
+
const callback = vi.fn();
|
|
308
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn())();
|
|
309
|
+
const stop = setup.onMessage.setPayload({ room: "a" }).listen(callback);
|
|
310
|
+
|
|
311
|
+
// The server ends the first subscription while the client completes it
|
|
312
|
+
ws.serverComplete("1");
|
|
313
|
+
ws.serverNext("2", { data: { onMessage: { text: "second" } } });
|
|
314
|
+
stop();
|
|
315
|
+
|
|
316
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
317
|
+
expect(ws.completed).toStrictEqual(["1", "2"]);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("should report a throwing callback and keep the subscription", async () => {
|
|
321
|
+
const ws = await connect();
|
|
322
|
+
const failure = new Error("Callback failed");
|
|
323
|
+
const callback = vi.fn().mockImplementationOnce(() => {
|
|
324
|
+
throw failure;
|
|
325
|
+
});
|
|
326
|
+
setup.onMessage.setPayload({ room: "a" }).listen(callback);
|
|
327
|
+
|
|
328
|
+
ws.serverNext("1", { data: { onMessage: { text: "1" } } });
|
|
329
|
+
ws.serverNext("1", { data: { onMessage: { text: "2" } } });
|
|
330
|
+
|
|
331
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
332
|
+
expect(setup.errors).toStrictEqual([failure]);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
describe("when the server ends a subscription", () => {
|
|
337
|
+
it("should end the listener on complete", async () => {
|
|
338
|
+
const ws = await connect();
|
|
339
|
+
const onEnd = vi.fn();
|
|
340
|
+
const callback = vi.fn();
|
|
341
|
+
setup.onMessage.setPayload({ room: "a" }).listen(callback, { onEnd });
|
|
342
|
+
|
|
343
|
+
ws.serverComplete("1");
|
|
344
|
+
ws.serverNext("1", { data: { onMessage: { text: "late" } } });
|
|
345
|
+
|
|
346
|
+
expect(onEnd).toHaveBeenCalledWith({ topic: "OnMessage", error: null });
|
|
347
|
+
expect(callback).not.toHaveBeenCalled();
|
|
348
|
+
expect(ws.completed).toStrictEqual([]);
|
|
349
|
+
expect(setup.socket.adapter.listeners.get("OnMessage")?.size ?? 0).toBe(0);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("should end the listener with a GraphqlSubscriptionError on error", async () => {
|
|
353
|
+
const ws = await connect();
|
|
354
|
+
const onEnd = vi.fn();
|
|
355
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn(), { onEnd });
|
|
356
|
+
|
|
357
|
+
ws.serverError("1", [{ message: "Unknown room", extensions: { code: "NOT_FOUND" } }]);
|
|
358
|
+
|
|
359
|
+
const [{ error }] = onEnd.mock.calls[0];
|
|
360
|
+
expect(error).toBeInstanceOf(GraphqlSubscriptionError);
|
|
361
|
+
expect(error).toMatchObject({
|
|
362
|
+
message: "Unknown room",
|
|
363
|
+
errors: [{ message: "Unknown room", extensions: { code: "NOT_FOUND" } }],
|
|
364
|
+
topic: "OnMessage",
|
|
365
|
+
payload: { room: "a" },
|
|
366
|
+
});
|
|
367
|
+
expect(setup.errors).toStrictEqual([error]);
|
|
368
|
+
expect(ws.completed).toStrictEqual([]);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("should join the messages of every error", async () => {
|
|
372
|
+
const ws = await connect();
|
|
373
|
+
const onEnd = vi.fn();
|
|
374
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn(), { onEnd });
|
|
375
|
+
|
|
376
|
+
ws.serverError("1", [{ message: "First" }, { message: "Second" }]);
|
|
377
|
+
|
|
378
|
+
expect(onEnd.mock.calls[0][0].error.message).toBe("First\nSecond");
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("should start a new subscription for a later listener of an ended one", async () => {
|
|
382
|
+
const ws = await connect();
|
|
383
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
384
|
+
|
|
385
|
+
ws.serverComplete("1");
|
|
386
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
387
|
+
|
|
388
|
+
expect(ws.subscriptions.map(({ id }) => id)).toStrictEqual(["1", "2"]);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("should keep the other subscriptions running", async () => {
|
|
392
|
+
const ws = await connect();
|
|
393
|
+
const roomB = vi.fn();
|
|
394
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
395
|
+
setup.onMessage.setPayload({ room: "b" }).listen(roomB);
|
|
396
|
+
|
|
397
|
+
ws.serverError("1", [{ message: "Unknown room" }]);
|
|
398
|
+
ws.serverNext("2", { data: { onMessage: { text: "still here" } } });
|
|
399
|
+
|
|
400
|
+
expect(roomB).toHaveBeenCalledTimes(1);
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
describe("when the connection drops", () => {
|
|
405
|
+
it("should send every subscription again with new ids after the next acknowledgement", async () => {
|
|
406
|
+
vi.useFakeTimers();
|
|
407
|
+
const ws = await connect();
|
|
408
|
+
const callback = vi.fn();
|
|
409
|
+
setup.onMessage.setPayload({ room: "a" }).listen(callback);
|
|
410
|
+
setup.onTick.listen(vi.fn());
|
|
411
|
+
|
|
412
|
+
ws.serverClose({ code: 1006 });
|
|
413
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
414
|
+
const next = await openWebSocket(2);
|
|
415
|
+
expect(next.subscriptions).toHaveLength(0);
|
|
416
|
+
next.serverAck();
|
|
417
|
+
await flushPromises();
|
|
418
|
+
|
|
419
|
+
expect(next.subscriptions.map(({ id, payload }) => [id, payload.operationName])).toStrictEqual([
|
|
420
|
+
["3", "OnMessage"],
|
|
421
|
+
["4", "OnTick"],
|
|
422
|
+
]);
|
|
423
|
+
next.serverNext("3", { data: { onMessage: { text: "back" } } });
|
|
424
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it("should start the listeners again after the reconnect", async () => {
|
|
428
|
+
vi.useFakeTimers();
|
|
429
|
+
const ws = await connect();
|
|
430
|
+
const onStart = vi.fn();
|
|
431
|
+
setup.onMessage.setPayload({ room: "a" }).listen(vi.fn(), { onStart });
|
|
432
|
+
|
|
433
|
+
ws.serverClose({ code: 1006 });
|
|
434
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
435
|
+
await acknowledgeWebSocket(2);
|
|
436
|
+
|
|
437
|
+
expect(onStart).toHaveBeenCalledTimes(2);
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it("should not send a subscription stopped while disconnected", async () => {
|
|
441
|
+
vi.useFakeTimers();
|
|
442
|
+
const ws = await connect();
|
|
443
|
+
const stop = setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
444
|
+
|
|
445
|
+
ws.serverClose({ code: 1006 });
|
|
446
|
+
stop();
|
|
447
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
448
|
+
const next = await acknowledgeWebSocket(2);
|
|
449
|
+
|
|
450
|
+
expect(next.subscriptions).toHaveLength(0);
|
|
451
|
+
expect(next.completed).toHaveLength(0);
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
it("should ignore the messages of the previous connection", async () => {
|
|
455
|
+
vi.useFakeTimers();
|
|
456
|
+
const ws = await connect();
|
|
457
|
+
const callback = vi.fn();
|
|
458
|
+
setup.onMessage.setPayload({ room: "a" }).listen(callback);
|
|
459
|
+
|
|
460
|
+
ws.serverClose({ code: 1006 });
|
|
461
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
462
|
+
await acknowledgeWebSocket(2);
|
|
463
|
+
ws.serverNext("1", { data: { onMessage: { text: "ghost" } } });
|
|
464
|
+
|
|
465
|
+
expect(callback).not.toHaveBeenCalled();
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
describe("when stopping a subscription", () => {
|
|
470
|
+
it("should send complete for the subscription", async () => {
|
|
471
|
+
const ws = await connect();
|
|
472
|
+
const stop = setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
473
|
+
|
|
474
|
+
stop();
|
|
475
|
+
|
|
476
|
+
expect(ws.messages.at(-1)).toStrictEqual({ id: "1", type: "complete" });
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it("should send nothing for a subscription that was not sent yet", async () => {
|
|
480
|
+
setup.socket.connect();
|
|
481
|
+
const ws = await openWebSocket();
|
|
482
|
+
const stop = setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
483
|
+
|
|
484
|
+
stop();
|
|
485
|
+
ws.serverAck();
|
|
486
|
+
await flushPromises();
|
|
487
|
+
|
|
488
|
+
expect(ws.messages).toStrictEqual([{ type: "connection_init" }]);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("should report a complete that could not be sent", async () => {
|
|
492
|
+
const ws = await connect();
|
|
493
|
+
const stop = setup.onMessage.setPayload({ room: "a" }).listen(vi.fn());
|
|
494
|
+
const failure = new Error("Send failed");
|
|
495
|
+
vi.spyOn(ws, "send").mockImplementation(() => {
|
|
496
|
+
throw failure;
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
stop();
|
|
500
|
+
|
|
501
|
+
expect(setup.errors).toStrictEqual([failure]);
|
|
502
|
+
});
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
describe("when emitting", () => {
|
|
506
|
+
it("should throw, queries and mutations are sent with the GraphqlAdapter", async () => {
|
|
507
|
+
await connect();
|
|
508
|
+
const emitter = setup.socket.createEmitter<{ id: string }>()({ topic: "anything" });
|
|
509
|
+
|
|
510
|
+
expect(() => emitter.emit({ payload: { id: "1" } })).toThrow(
|
|
511
|
+
"Cannot emit events with the GraphQL WebSocket adapter",
|
|
512
|
+
);
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { ExtractListenerPayloadType, ExtractListenerResponseType } from "@hyper-fetch/sockets";
|
|
2
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
3
|
+
import type { DocumentNode, GraphQLFormattedError } from "graphql";
|
|
4
|
+
import gql from "graphql-tag";
|
|
5
|
+
import { expectTypeOf } from "vitest";
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
GraphqlSubscriptionError,
|
|
9
|
+
GraphqlWsAdapterType,
|
|
10
|
+
GraphqlWsExtraType,
|
|
11
|
+
GraphqlWsListenerOptionsType,
|
|
12
|
+
} from "../../../src";
|
|
13
|
+
import { GraphqlWsAdapter } from "../../../src";
|
|
14
|
+
|
|
15
|
+
/** The decoration graphql-codegen and gql.tada add to documents - `DocumentTypeDecoration` of `@graphql-typed-document-node/core` */
|
|
16
|
+
type TypedDocument<Result, Variables> = DocumentNode & { __apiType?: (variables: Variables) => Result };
|
|
17
|
+
|
|
18
|
+
type OnMessageResult = { onMessage: { text: string } };
|
|
19
|
+
const OnMessageDocument = gql`
|
|
20
|
+
subscription OnMessage($room: String!) {
|
|
21
|
+
onMessage(room: $room) {
|
|
22
|
+
text
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
` as TypedDocument<OnMessageResult, { room: string; limit?: number }>;
|
|
26
|
+
// Codegen types an operation without variables this way
|
|
27
|
+
const OnTickDocument = gql`
|
|
28
|
+
subscription OnTick {
|
|
29
|
+
tick
|
|
30
|
+
}
|
|
31
|
+
` as TypedDocument<{ tick: number }, { [key: string]: never }>;
|
|
32
|
+
|
|
33
|
+
describe("Graphql Ws Adapter [ Types ]", () => {
|
|
34
|
+
const socket = new Socket({ url: "ws://localhost", adapter: GraphqlWsAdapter(), adapterOptions: { lazy: true } });
|
|
35
|
+
|
|
36
|
+
it("should type the adapter", () => {
|
|
37
|
+
expectTypeOf(socket.adapter).toEqualTypeOf<GraphqlWsAdapterType>();
|
|
38
|
+
expectTypeOf<GraphqlWsListenerOptionsType["document"]>().toEqualTypeOf<string | DocumentNode | undefined>();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("when the listener has a typed document", () => {
|
|
42
|
+
const onMessage = socket.createListener()({ topic: "OnMessage", options: { document: OnMessageDocument } });
|
|
43
|
+
|
|
44
|
+
it("should take the data and the variables from the document", () => {
|
|
45
|
+
expectTypeOf<ExtractListenerResponseType<typeof onMessage>>().toEqualTypeOf<OnMessageResult>();
|
|
46
|
+
expectTypeOf<ExtractListenerPayloadType<typeof onMessage>>().toEqualTypeOf<{ room: string; limit?: number }>();
|
|
47
|
+
|
|
48
|
+
onMessage.listen(
|
|
49
|
+
({ data, extra }) => {
|
|
50
|
+
expectTypeOf(data).toEqualTypeOf<OnMessageResult>();
|
|
51
|
+
expectTypeOf(extra).toEqualTypeOf<GraphqlWsExtraType>();
|
|
52
|
+
expectTypeOf(extra.errors).toEqualTypeOf<GraphQLFormattedError[] | undefined>();
|
|
53
|
+
},
|
|
54
|
+
{ payload: { room: "a" } },
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should require the variables of the document", () => {
|
|
59
|
+
onMessage.setPayload({ room: "a" }).listen(() => null);
|
|
60
|
+
onMessage.setPayload({ room: "a", limit: 1 }).listen(() => null);
|
|
61
|
+
// @ts-expect-error the variables are required by the document
|
|
62
|
+
onMessage.listen(() => null);
|
|
63
|
+
// @ts-expect-error room is a string
|
|
64
|
+
onMessage.setPayload({ room: 1 });
|
|
65
|
+
// @ts-expect-error room is required
|
|
66
|
+
onMessage.setPayload({ limit: 1 });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should not need a payload for a document without variables", () => {
|
|
70
|
+
const onTick = socket.createListener()({ topic: "OnTick", options: { document: OnTickDocument } });
|
|
71
|
+
|
|
72
|
+
expectTypeOf<ExtractListenerPayloadType<typeof onTick>>().toEqualTypeOf<undefined>();
|
|
73
|
+
onTick.listen(({ data }) => {
|
|
74
|
+
expectTypeOf(data).toEqualTypeOf<{ tick: number }>();
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should keep the declared generics over the document", () => {
|
|
79
|
+
const declared = socket.createListener<{ custom: true }, { id: string }>()({
|
|
80
|
+
topic: "OnMessage",
|
|
81
|
+
options: { document: OnMessageDocument },
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expectTypeOf<ExtractListenerResponseType<typeof declared>>().toEqualTypeOf<{ custom: true }>();
|
|
85
|
+
expectTypeOf<ExtractListenerPayloadType<typeof declared>>().toEqualTypeOf<{ id: string }>();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should type the delivery strategy with the data of the document", () => {
|
|
89
|
+
const batched = socket.createListener()({
|
|
90
|
+
topic: "OnMessage",
|
|
91
|
+
options: { document: OnMessageDocument },
|
|
92
|
+
delivery: { strategy: "batch", interval: 100 },
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
batched.listen(
|
|
96
|
+
({ data }) => {
|
|
97
|
+
expectTypeOf(data).toEqualTypeOf<OnMessageResult[]>();
|
|
98
|
+
},
|
|
99
|
+
{ payload: { room: "a" } },
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("when the listener has no typed document", () => {
|
|
105
|
+
it("should keep an untyped document untyped", () => {
|
|
106
|
+
const untyped = socket.createListener()({
|
|
107
|
+
topic: "OnMessage",
|
|
108
|
+
options: {
|
|
109
|
+
document: gql`
|
|
110
|
+
subscription {
|
|
111
|
+
a
|
|
112
|
+
}
|
|
113
|
+
`,
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expectTypeOf<ExtractListenerResponseType<typeof untyped>>().toEqualTypeOf<unknown>();
|
|
118
|
+
expectTypeOf<ExtractListenerPayloadType<typeof untyped>>().toEqualTypeOf<undefined>();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should use the generics for a document in the topic", () => {
|
|
122
|
+
const listener = socket.createListener<{ tick: number }>()({ topic: "subscription { tick }" });
|
|
123
|
+
|
|
124
|
+
expectTypeOf<ExtractListenerResponseType<typeof listener>>().toEqualTypeOf<{ tick: number }>();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("should reject options of other adapters", () => {
|
|
128
|
+
// @ts-expect-error document has to be a string or a DocumentNode
|
|
129
|
+
socket.createListener()({ topic: "OnMessage", options: { document: 1 } });
|
|
130
|
+
// @ts-expect-error unknown listener option
|
|
131
|
+
socket.createListener()({ topic: "OnMessage", options: { other: true } });
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should type the subscription error", () => {
|
|
136
|
+
expectTypeOf<GraphqlSubscriptionError["errors"]>().toEqualTypeOf<GraphQLFormattedError[]>();
|
|
137
|
+
expectTypeOf<GraphqlSubscriptionError["payload"]>().toEqualTypeOf<unknown>();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should accept only valid adapter options", () => {
|
|
141
|
+
// eslint-disable-next-line no-new
|
|
142
|
+
new Socket({
|
|
143
|
+
url: "ws://localhost",
|
|
144
|
+
adapter: GraphqlWsAdapter(),
|
|
145
|
+
adapterOptions: {
|
|
146
|
+
connectionParams: async () => ({ token: "a" }),
|
|
147
|
+
keepAlive: { enabled: true, intervalMs: 1000 },
|
|
148
|
+
lazy: true,
|
|
149
|
+
lazyCloseTimeout: 100,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
// eslint-disable-next-line no-new
|
|
153
|
+
new Socket({
|
|
154
|
+
url: "ws://localhost",
|
|
155
|
+
adapter: GraphqlWsAdapter(),
|
|
156
|
+
// @ts-expect-error the connection params are an object
|
|
157
|
+
adapterOptions: { connectionParams: "token" },
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|