-
Notifications
You must be signed in to change notification settings - Fork 36
/
AccumulateMultipartResponsesLink.test.ts
207 lines (182 loc) · 5.11 KB
/
AccumulateMultipartResponsesLink.test.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type {
ExecutionPatchResult,
FetchResult,
} from "@apollo/client/index.js";
import { ApolloLink, Observable, gql } from "@apollo/client/index.js";
import { test, mock } from "node:test";
import assert from "node:assert";
import { fromPartial } from "@total-typescript/shoehorn";
import type { SubscriptionObserver } from "zen-observable-ts";
const { DebounceMultipartResponsesLink: AccumulateMultipartResponsesLink } =
await import("#bundled");
await test("normal queries can resolve synchronously", () => {
const query = gql`
query {
fastField
}
`;
const link = new AccumulateMultipartResponsesLink({
cutoffDelay: 1000,
});
const nextLink = getFinalLinkWithExposedObserver();
const subscriptionStatus = trackSubscriptionStatus(
ApolloLink.from([link, nextLink.link]).request(fromPartial({ query }))
);
assert(nextLink.observer);
assert.deepStrictEqual(subscriptionStatus, {
results: [],
error: undefined,
complete: false,
});
nextLink.observer.next({ data: { fastField: "fast" } });
nextLink.observer.complete();
assert.deepStrictEqual(subscriptionStatus, {
results: [{ data: { fastField: "fast" } }],
error: undefined,
complete: true,
});
});
await test("deferred query will complete synchonously if maxDelay is 0", () => {
const query = gql`
query {
fastField
... @defer {
slowField
}
}
`;
const link = new AccumulateMultipartResponsesLink({
cutoffDelay: 0,
});
const nextLink = getFinalLinkWithExposedObserver();
const subscriptionStatus = trackSubscriptionStatus(
ApolloLink.from([link, nextLink.link]).request(fromPartial({ query }))
);
assert(nextLink.observer);
assert.deepStrictEqual(subscriptionStatus, {
results: [],
error: undefined,
complete: false,
});
nextLink.observer.next({ data: { fastField: "fast" } });
// no complete call here!
assert.ok(nextLink.observer.closed);
assert.deepStrictEqual(subscriptionStatus, {
results: [{ data: { fastField: "fast" } }],
error: undefined,
complete: true,
});
});
await test("`next` call will be debounced and results will be merged together", () => {
mock.timers.enable();
const query = gql`
query {
fastField
... @defer {
slowField
}
... @defer {
verySlowField
}
}
`;
const link = new AccumulateMultipartResponsesLink({
cutoffDelay: 1000,
});
const nextLink = getFinalLinkWithExposedObserver();
const subscriptionStatus = trackSubscriptionStatus(
ApolloLink.from([link, nextLink.link]).request(fromPartial({ query }))
);
assert(nextLink.observer);
assert.deepStrictEqual(subscriptionStatus, {
results: [],
error: undefined,
complete: false,
});
mock.timers.tick(100);
// initial response after 100ms
nextLink.observer.next({ data: { fastField: "fast" } });
mock.timers.tick(100);
assert.ok(!nextLink.observer.closed);
assert.deepStrictEqual(subscriptionStatus, {
results: [],
error: undefined,
complete: false,
});
// incremental response after 200ms
nextLink.observer.next({
incremental: [
{
path: [],
data: { slowField: "slow" },
},
],
});
mock.timers.tick(899);
// at 1099ms, 999ms after the initial response, we still don't have our final result
assert.ok(!nextLink.observer.closed);
assert.deepStrictEqual(subscriptionStatus, {
results: [],
error: undefined,
complete: false,
});
mock.timers.tick(2);
// after 1101ms, 1001ms after the initial response, we have our final result
assert.ok(nextLink.observer.closed);
assert.deepStrictEqual(subscriptionStatus, {
results: [{ data: { fastField: "fast", slowField: "slow" } }],
error: undefined,
complete: true,
});
// more responses would be ignored
nextLink.observer.next({
incremental: [
{
path: [],
data: { verySlowField: "very slow" },
},
],
});
assert.ok(nextLink.observer.closed);
assert.deepStrictEqual(subscriptionStatus, {
results: [{ data: { fastField: "fast", slowField: "slow" } }],
error: undefined,
complete: true,
});
});
function trackSubscriptionStatus(
observable: Observable<ExecutionPatchResult> | null
) {
assert(observable);
const subscriptionStatus = {
results: [] as FetchResult[],
error: undefined as Error | undefined,
complete: false,
};
observable.subscribe({
next: (result) => {
subscriptionStatus.results.push(result);
},
error: (err) => {
subscriptionStatus.error = err;
},
complete: () => {
subscriptionStatus.complete = true;
},
});
return subscriptionStatus;
}
function getFinalLinkWithExposedObserver() {
const returnValue = {
observer: undefined as SubscriptionObserver<FetchResult> | undefined,
link: new ApolloLink(() => {
return new Observable((observer) => {
returnValue.observer = observer;
});
}),
};
return returnValue;
}