-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathObservableQuery.ts
636 lines (556 loc) · 19.6 KB
/
ObservableQuery.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import { invariant, InvariantError } from 'ts-invariant';
import { equal } from '@wry/equality';
import { tryFunctionOrLogError } from '../utilities/common/errorHandling';
import { cloneDeep } from '../utilities/common/cloneDeep';
import { getOperationDefinition } from '../utilities/graphql/getFromAST';
import { NetworkStatus, isNetworkRequestInFlight } from './networkStatus';
import {
Observable,
Observer,
ObservableSubscription
} from '../utilities/observables/Observable';
import { iterateObserversSafely } from '../utilities/observables/iteration';
import { ApolloError } from '../errors/ApolloError';
import { QueryManager } from './QueryManager';
import { ApolloQueryResult, OperationVariables } from './types';
import {
WatchQueryOptions,
FetchMoreQueryOptions,
SubscribeToMoreOptions,
ErrorPolicy,
} from './watchQueryOptions';
import { QueryStoreValue } from './QueryInfo';
import { isNonEmptyArray } from '../utilities/common/arrays';
import { Reobserver } from './Reobserver';
export type ApolloCurrentQueryResult<T> = ApolloQueryResult<T> & {
error?: ApolloError;
partial?: boolean;
};
export interface FetchMoreOptions<
TData = any,
TVariables = OperationVariables
> {
updateQuery?: (
previousQueryResult: TData,
options: {
fetchMoreResult?: TData;
variables?: TVariables;
},
) => TData;
}
export interface UpdateQueryOptions<TVariables> {
variables?: TVariables;
}
export const hasError = (
storeValue: QueryStoreValue,
policy: ErrorPolicy = 'none',
) => storeValue && (
storeValue.networkError ||
(policy === 'none' && isNonEmptyArray(storeValue.graphQLErrors))
);
export class ObservableQuery<
TData = any,
TVariables = OperationVariables
> extends Observable<ApolloQueryResult<TData>> {
public readonly options: WatchQueryOptions<TVariables>;
public readonly queryId: string;
public readonly queryName?: string;
// Computed shorthand for this.options.variables, preserved for
// backwards compatibility.
public get variables(): TVariables | undefined {
return this.options.variables;
}
private isTornDown: boolean;
private queryManager: QueryManager<any>;
private observers = new Set<Observer<ApolloQueryResult<TData>>>();
private subscriptions = new Set<ObservableSubscription>();
private lastResult: ApolloQueryResult<TData>;
private lastResultSnapshot: ApolloQueryResult<TData>;
private lastError: ApolloError;
constructor({
queryManager,
options,
}: {
queryManager: QueryManager<any>;
options: WatchQueryOptions<TVariables>;
}) {
super((observer: Observer<ApolloQueryResult<TData>>) =>
this.onSubscribe(observer),
);
// active state
this.isTornDown = false;
// query information
this.options = options;
this.queryId = queryManager.generateQueryId();
const opDef = getOperationDefinition(options.query);
this.queryName = opDef && opDef.name && opDef.name.value;
// related classes
this.queryManager = queryManager;
}
public result(): Promise<ApolloQueryResult<TData>> {
return new Promise((resolve, reject) => {
const observer: Observer<ApolloQueryResult<TData>> = {
next: (result: ApolloQueryResult<TData>) => {
resolve(result);
// Stop the query within the QueryManager if we can before
// this function returns.
//
// We do this in order to prevent observers piling up within
// the QueryManager. Notice that we only fully unsubscribe
// from the subscription in a setTimeout(..., 0) call. This call can
// actually be handled by the browser at a much later time. If queries
// are fired in the meantime, observers that should have been removed
// from the QueryManager will continue to fire, causing an unnecessary
// performance hit.
this.observers.delete(observer);
if (!this.observers.size) {
this.queryManager.removeQuery(this.queryId);
}
setTimeout(() => {
subscription.unsubscribe();
}, 0);
},
error: reject,
};
const subscription = this.subscribe(observer);
});
}
public getCurrentResult(): ApolloCurrentQueryResult<TData> {
const {
lastResult,
lastError,
options: { fetchPolicy },
} = this;
const isNetworkFetchPolicy =
fetchPolicy === 'network-only' ||
fetchPolicy === 'no-cache';
const networkStatus =
lastError ? NetworkStatus.error :
lastResult ? lastResult.networkStatus :
isNetworkFetchPolicy ? NetworkStatus.loading :
NetworkStatus.ready;
const result: ApolloCurrentQueryResult<TData> = {
data: !lastError && lastResult && lastResult.data || void 0,
error: lastError,
loading: isNetworkRequestInFlight(networkStatus),
networkStatus,
};
if (this.isTornDown) {
return result;
}
const { data, partial } = this.getCurrentQueryResult();
Object.assign(result, { data, partial });
const queryStoreValue = this.queryManager.getQueryStoreValue(this.queryId);
if (queryStoreValue) {
const { networkStatus } = queryStoreValue;
if (hasError(queryStoreValue, this.options.errorPolicy)) {
return Object.assign(result, {
data: void 0,
networkStatus,
error: new ApolloError({
graphQLErrors: queryStoreValue.graphQLErrors,
networkError: queryStoreValue.networkError,
}),
});
}
// Variables might have been added dynamically at query time, when
// using `@client @export(as: "varname")` for example. When this happens,
// the variables have been updated in the query store, but not updated on
// the original `ObservableQuery`. We'll update the observable query
// variables here to match, so retrieving from the cache doesn't fail.
if (queryStoreValue.variables) {
this.options.variables = {
...this.options.variables,
...(queryStoreValue.variables as TVariables),
};
}
Object.assign(result, {
loading: isNetworkRequestInFlight(networkStatus),
networkStatus,
});
if (queryStoreValue.graphQLErrors && this.options.errorPolicy === 'all') {
result.errors = queryStoreValue.graphQLErrors;
}
}
if (!partial) {
this.updateLastResult(result);
}
return result;
}
// Compares newResult to the snapshot we took of this.lastResult when it was
// first received.
public isDifferentFromLastResult(newResult: ApolloQueryResult<TData>) {
return !equal(this.lastResultSnapshot, newResult);
}
// Returns the last result that observer.next was called with. This is not the same as
// getCurrentResult! If you're not sure which you need, then you probably need getCurrentResult.
public getLastResult(): ApolloQueryResult<TData> {
return this.lastResult;
}
public getLastError(): ApolloError {
return this.lastError;
}
public resetLastResults(): void {
delete this.lastResult;
delete this.lastResultSnapshot;
delete this.lastError;
this.isTornDown = false;
}
public resetQueryStoreErrors() {
const queryStore = this.queryManager.getQueryStoreValue(this.queryId);
if (queryStore) {
queryStore.networkError = undefined;
queryStore.graphQLErrors = [];
}
}
/**
* Update the variables of this observable query, and fetch the new results.
* This method should be preferred over `setVariables` in most use cases.
*
* @param variables: The new set of variables. If there are missing variables,
* the previous values of those variables will be used.
*/
public refetch(variables?: TVariables): Promise<ApolloQueryResult<TData>> {
let { fetchPolicy } = this.options;
// early return if trying to read from cache during refetch
if (fetchPolicy === 'cache-only') {
return Promise.reject(new InvariantError(
'cache-only fetchPolicy option should not be used together with query refetch.',
));
}
// Unless the provided fetchPolicy always consults the network
// (no-cache, network-only, or cache-and-network), override it with
// network-only to force the refetch for this fetchQuery call.
if (fetchPolicy !== 'no-cache' &&
fetchPolicy !== 'cache-and-network') {
fetchPolicy = 'network-only';
}
if (variables && !equal(this.options.variables, variables)) {
// Update the existing options with new variables
this.options.variables = {
...this.options.variables,
...variables,
};
}
return this.newReobserver(false).reobserve({
fetchPolicy,
variables: this.options.variables,
// Always disable polling for refetches.
pollInterval: 0,
}, NetworkStatus.refetch);
}
public fetchMore<K extends keyof TVariables>(
fetchMoreOptions: FetchMoreQueryOptions<TVariables, K> &
FetchMoreOptions<TData, TVariables>,
): Promise<ApolloQueryResult<TData>> {
const combinedOptions = {
...(fetchMoreOptions.query ? fetchMoreOptions : {
...this.options,
...fetchMoreOptions,
variables: {
...this.options.variables,
...fetchMoreOptions.variables,
},
}),
// The fetchMore request goes immediately to the network and does
// not automatically write its result to the cache (hence no-cache
// instead of network-only), because we allow the caller of
// fetchMore to provide an updateQuery callback that determines how
// the data gets written to the cache.
fetchPolicy: "no-cache",
} as WatchQueryOptions;
const qid = this.queryManager.generateQueryId();
return this.queryManager.fetchQuery(
qid,
combinedOptions,
NetworkStatus.fetchMore,
).then(fetchMoreResult => {
this.updateQuery((previousResult: any) => {
const data = fetchMoreResult.data as TData;
const { updateQuery } = fetchMoreOptions;
return updateQuery ? updateQuery(previousResult, {
fetchMoreResult: data,
variables: combinedOptions.variables as TVariables,
}) : data;
});
return fetchMoreResult as ApolloQueryResult<TData>;
}).finally(() => {
this.queryManager.stopQuery(qid);
this.reobserve();
});
}
// XXX the subscription variables are separate from the query variables.
// if you want to update subscription variables, right now you have to do that separately,
// and you can only do it by stopping the subscription and then subscribing again with new variables.
public subscribeToMore<
TSubscriptionData = TData,
TSubscriptionVariables = TVariables
>(
options: SubscribeToMoreOptions<
TData,
TSubscriptionVariables,
TSubscriptionData
>,
) {
const subscription = this.queryManager
.startGraphQLSubscription({
query: options.document,
variables: options.variables,
})
.subscribe({
next: (subscriptionData: { data: TSubscriptionData }) => {
const { updateQuery } = options;
if (updateQuery) {
this.updateQuery<TSubscriptionVariables>(
(previous, { variables }) =>
updateQuery(previous, {
subscriptionData,
variables,
}),
);
}
},
error: (err: any) => {
if (options.onError) {
options.onError(err);
return;
}
invariant.error('Unhandled GraphQL subscription error', err);
},
});
this.subscriptions.add(subscription);
return () => {
if (this.subscriptions.delete(subscription)) {
subscription.unsubscribe();
}
};
}
public setOptions(
newOptions: Partial<WatchQueryOptions<TVariables>>,
): Promise<ApolloQueryResult<TData>> {
return this.reobserve(newOptions);
}
/**
* This is for *internal* use only. Most users should instead use `refetch`
* in order to be properly notified of results even when they come from cache.
*
* Update the variables of this observable query, and fetch the new results
* if they've changed. If you want to force new results, use `refetch`.
*
* Note: the `next` callback will *not* fire if the variables have not changed
* or if the result is coming from cache.
*
* Note: the promise will return the old results immediately if the variables
* have not changed.
*
* Note: the promise will return null immediately if the query is not active
* (there are no subscribers).
*
* @private
*
* @param variables: The new set of variables. If there are missing variables,
* the previous values of those variables will be used.
*
* @param tryFetch: Try and fetch new results even if the variables haven't
* changed (we may still just hit the store, but if there's nothing in there
* this will refetch)
*/
public setVariables(
variables: TVariables,
): Promise<ApolloQueryResult<TData> | void> {
if (equal(this.variables, variables)) {
// If we have no observers, then we don't actually want to make a network
// request. As soon as someone observes the query, the request will kick
// off. For now, we just store any changes. (See #1077)
return this.observers.size
? this.result()
: Promise.resolve();
}
this.options.variables = variables;
// See comment above
if (!this.observers.size) {
return Promise.resolve();
}
let { fetchPolicy } = this.options;
if (fetchPolicy !== 'cache-first' &&
fetchPolicy !== 'no-cache' &&
fetchPolicy !== 'network-only') {
fetchPolicy = 'cache-and-network';
}
return this.reobserve({
fetchPolicy,
variables,
}, NetworkStatus.setVariables);
}
public updateQuery<TVars = TVariables>(
mapFn: (
previousQueryResult: TData,
options: Pick<WatchQueryOptions<TVars>, "variables">,
) => TData,
): void {
const { queryManager } = this;
const previousResult = this.getCurrentQueryResult(false).data;
const newResult = tryFunctionOrLogError(
() => mapFn(previousResult!, {
variables: (this as any).variables,
}),
);
if (newResult) {
queryManager.cache.writeQuery({
query: this.options.query,
data: newResult,
variables: this.variables,
});
queryManager.broadcastQueries();
}
}
private getCurrentQueryResult(
optimistic: boolean = true,
): {
data?: TData;
partial: boolean;
} {
const { fetchPolicy } = this.options;
if (fetchPolicy === 'no-cache' ||
fetchPolicy === 'network-only') {
return {
data: this.lastResult?.data,
partial: false,
};
}
const { result, complete } = this.queryManager.cache.diff<TData>({
query: this.options.query,
variables: this.variables,
previousResult: this.lastResult?.data,
returnPartialData: true,
optimistic,
});
return {
data: (complete || this.options.returnPartialData) ? result : void 0,
partial: !complete,
};
}
public startPolling(pollInterval: number) {
this.getReobserver().updateOptions({ pollInterval });
}
public stopPolling() {
if (this.reobserver) {
this.reobserver.updateOptions({ pollInterval: 0 });
}
}
private updateLastResult(newResult: ApolloQueryResult<TData>) {
const previousResult = this.lastResult;
this.lastResult = newResult;
this.lastResultSnapshot = this.queryManager.assumeImmutableResults
? newResult
: cloneDeep(newResult);
if (!isNonEmptyArray(newResult.errors)) {
delete this.lastError;
}
return previousResult;
}
private onSubscribe(observer: Observer<ApolloQueryResult<TData>>) {
// Subscribing using this.observer will create an infinite notificaion
// loop, but the intent of broadcasting results to all the other
// this.observers can be satisfied without doing anything, which is
// why we do not bother throwing an error here.
if (observer === this.observer) {
return () => {};
}
// Zen Observable has its own error function, so in order to log correctly
// we need to provide a custom error callback.
try {
var subObserver = (observer as any)._subscription._observer;
if (subObserver && !subObserver.error) {
subObserver.error = defaultSubscriptionObserverErrorCallback;
}
} catch {}
const first = !this.observers.size;
this.observers.add(observer);
// Deliver initial result
if (observer.next && this.lastResult) observer.next(this.lastResult);
if (observer.error && this.lastError) observer.error(this.lastError);
// Initiate observation of this query if it hasn't been reported to
// the QueryManager yet.
if (first) {
this.reobserve().catch(_ => {
// Blindly catching here prevents unhandled promise rejections,
// and is safe because the ObservableQuery handles this error with
// this.observer.error, so we're not just swallowing the error by
// ignoring it here.
});
}
return () => {
if (this.observers.delete(observer) && !this.observers.size) {
this.tearDownQuery();
}
};
}
private reobserver?: Reobserver<TData, TVariables>;
private getReobserver(): Reobserver<TData, TVariables> {
return this.reobserver || (this.reobserver = this.newReobserver(true));
}
private newReobserver(shareOptions: boolean) {
const { queryManager, queryId } = this;
queryManager.setObservableQuery(this);
return new Reobserver<TData, TVariables>(
this.observer,
// Sharing options allows this.reobserver.options to be ===
// this.options, so we don't have to worry about synchronizing the
// properties of two distinct objects.
shareOptions ? this.options : { ...this.options },
(currentOptions, newNetworkStatus) => {
queryManager.setObservableQuery(this);
return queryManager.fetchQueryObservable(
queryId,
currentOptions,
newNetworkStatus,
);
},
// Avoid polling during SSR and when the query is already in flight.
!queryManager.ssrMode && (() => !queryManager.checkInFlight(queryId)),
);
}
public reobserve(
newOptions?: Partial<WatchQueryOptions<TVariables>>,
newNetworkStatus?: NetworkStatus,
): Promise<ApolloQueryResult<TData>> {
this.isTornDown = false;
return this.getReobserver().reobserve(newOptions, newNetworkStatus);
}
private observer: Observer<ApolloQueryResult<TData>> = {
next: result => {
if (this.lastError || this.isDifferentFromLastResult(result)) {
this.updateLastResult(result);
iterateObserversSafely(this.observers, 'next', result);
}
},
error: (error: ApolloError) => {
// Since we don't get the current result on errors, only the error, we
// must mirror the updates that occur in QueryStore.markQueryError here
this.updateLastResult({
...this.lastResult,
errors: error.graphQLErrors,
networkStatus: NetworkStatus.error,
loading: false,
});
iterateObserversSafely(this.observers, 'error', this.lastError = error);
},
};
private tearDownQuery() {
const { queryManager } = this;
if (this.reobserver) {
this.reobserver.stop();
delete this.reobserver;
}
this.isTornDown = true;
// stop all active GraphQL subscriptions
this.subscriptions.forEach(sub => sub.unsubscribe());
this.subscriptions.clear();
queryManager.stopQuery(this.queryId);
this.observers.clear();
}
}
function defaultSubscriptionObserverErrorCallback(error: ApolloError) {
invariant.error('Unhandled error', error.message, error.stack);
}