-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ObservableQuery.ts
1169 lines (1034 loc) · 39.9 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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { invariant } from "../utilities/globals/index.js";
import type { DocumentNode } from "graphql";
import { equal } from "@wry/equality";
import { NetworkStatus, isNetworkRequestInFlight } from "./networkStatus.js";
import type {
Concast,
Observer,
ObservableSubscription,
} from "../utilities/index.js";
import {
cloneDeep,
compact,
getOperationDefinition,
Observable,
iterateObserversSafely,
fixObservableSubclass,
getQueryDefinition,
} from "../utilities/index.js";
import type { ApolloError } from "../errors/index.js";
import type { QueryManager } from "./QueryManager.js";
import type {
ApolloQueryResult,
OperationVariables,
TypedDocumentNode,
} from "./types.js";
import type {
WatchQueryOptions,
FetchMoreQueryOptions,
SubscribeToMoreOptions,
NextFetchPolicyContext,
WatchQueryFetchPolicy,
} from "./watchQueryOptions.js";
import type { QueryInfo } from "./QueryInfo.js";
import type { MissingFieldError } from "../cache/index.js";
import type { MissingTree } from "../cache/core/types/common.js";
import { equalByQuery } from "./equalByQuery.js";
import type { TODO } from "../utilities/types/TODO.js";
const { assign, hasOwnProperty } = Object;
export interface FetchMoreOptions<
TData = any,
TVariables = OperationVariables,
> {
updateQuery?: (
previousQueryResult: TData,
options: {
fetchMoreResult?: TData;
variables?: TVariables;
}
) => TData;
}
export interface UpdateQueryOptions<TVariables> {
variables?: TVariables;
}
interface Last<TData, TVariables> {
result: ApolloQueryResult<TData>;
variables?: TVariables;
error?: ApolloError;
}
export class ObservableQuery<
TData = any,
TVariables extends OperationVariables = OperationVariables,
> extends Observable<ApolloQueryResult<TData>> {
public readonly options: WatchQueryOptions<TVariables, TData>;
public readonly queryId: string;
public readonly queryName?: string;
// The `query` computed property will always reflect the document transformed
// by the last run query. `this.options.query` will always reflect the raw
// untransformed query to ensure document transforms with runtime conditionals
// are run on the original document.
public get query(): TypedDocumentNode<TData, TVariables> {
return this.lastQuery || this.options.query;
}
// Computed shorthand for this.options.variables, preserved for
// backwards compatibility.
/**
* An object containing the variables that were provided for the query.
*/
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 waitForOwnResult: boolean;
private last?: Last<TData, TVariables>;
private lastQuery?: DocumentNode;
private queryInfo: QueryInfo;
// When this.concast is defined, this.observer is the Observer currently
// subscribed to that Concast.
private concast?: Concast<ApolloQueryResult<TData>>;
private observer?: Observer<ApolloQueryResult<TData>>;
private pollingInfo?: {
interval: number;
timeout: ReturnType<typeof setTimeout>;
};
constructor({
queryManager,
queryInfo,
options,
}: {
queryManager: QueryManager<any>;
queryInfo: QueryInfo;
options: WatchQueryOptions<TVariables, TData>;
}) {
super((observer: Observer<ApolloQueryResult<TData>>) => {
// 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 most recent error or result.
const last = this.last;
if (last && last.error) {
observer.error && observer.error(last.error);
} else if (last && last.result) {
observer.next && observer.next(last.result);
}
// Initiate observation of this query if it hasn't been reported to
// the QueryManager yet.
if (first) {
// 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.
this.reobserve().catch(() => {});
}
return () => {
if (this.observers.delete(observer) && !this.observers.size) {
this.tearDownQuery();
}
};
});
// related classes
this.queryInfo = queryInfo;
this.queryManager = queryManager;
// active state
this.waitForOwnResult = skipCacheDataFor(options.fetchPolicy);
this.isTornDown = false;
this.subscribeToMore = this.subscribeToMore.bind(this);
const {
watchQuery: { fetchPolicy: defaultFetchPolicy = "cache-first" } = {},
} = queryManager.defaultOptions;
const {
fetchPolicy = defaultFetchPolicy,
// Make sure we don't store "standby" as the initialFetchPolicy.
initialFetchPolicy = fetchPolicy === "standby" ? defaultFetchPolicy : (
fetchPolicy
),
} = options;
this.options = {
...options,
// Remember the initial options.fetchPolicy so we can revert back to this
// policy when variables change. This information can also be specified
// (or overridden) by providing options.initialFetchPolicy explicitly.
initialFetchPolicy,
// This ensures this.options.fetchPolicy always has a string value, in
// case options.fetchPolicy was not provided.
fetchPolicy,
};
this.queryId = queryInfo.queryId || queryManager.generateQueryId();
const opDef = getOperationDefinition(this.query);
this.queryName = opDef && opDef.name && opDef.name.value;
}
public result(): Promise<ApolloQueryResult<TData>> {
return new Promise((resolve, reject) => {
// TODO: this code doesn’t actually make sense insofar as the observer
// will never exist in this.observers due how zen-observable wraps observables.
// https://github.com/zenparsing/zen-observable/blob/master/src/Observable.js#L169
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);
});
}
/** @internal */
public resetDiff() {
this.queryInfo.resetDiff();
}
public getCurrentResult(saveAsLastResult = true): ApolloQueryResult<TData> {
// Use the last result as long as the variables match this.variables.
const lastResult = this.getLastResult(true);
const networkStatus =
this.queryInfo.networkStatus ||
(lastResult && lastResult.networkStatus) ||
NetworkStatus.ready;
const result = {
...lastResult,
loading: isNetworkRequestInFlight(networkStatus),
networkStatus,
} as ApolloQueryResult<TData>;
const { fetchPolicy = "cache-first" } = this.options;
if (
// These fetch policies should never deliver data from the cache, unless
// redelivering a previously delivered result.
skipCacheDataFor(fetchPolicy) ||
// If this.options.query has @client(always: true) fields, we cannot
// trust diff.result, since it was read from the cache without running
// local resolvers (and it's too late to run resolvers now, since we must
// return a result synchronously).
this.queryManager.getDocumentInfo(this.query).hasForcedResolvers
) {
// Fall through.
} else if (this.waitForOwnResult) {
// This would usually be a part of `QueryInfo.getDiff()`.
// which we skip in the waitForOwnResult case since we are not
// interested in the diff.
this.queryInfo["updateWatch"]();
} else {
const diff = this.queryInfo.getDiff();
if (diff.complete || this.options.returnPartialData) {
result.data = diff.result;
}
if (equal(result.data, {})) {
result.data = void 0 as any;
}
if (diff.complete) {
// Similar to setting result.partial to false, but taking advantage of the
// falsiness of missing fields.
delete result.partial;
// If the diff is complete, and we're using a FetchPolicy that
// terminates after a complete cache read, we can assume the next result
// we receive will have NetworkStatus.ready and !loading.
if (
diff.complete &&
result.networkStatus === NetworkStatus.loading &&
(fetchPolicy === "cache-first" || fetchPolicy === "cache-only")
) {
result.networkStatus = NetworkStatus.ready;
result.loading = false;
}
} else {
result.partial = true;
}
if (
__DEV__ &&
!diff.complete &&
!this.options.partialRefetch &&
!result.loading &&
!result.data &&
!result.error
) {
logMissingFieldErrors(diff.missing);
}
}
if (saveAsLastResult) {
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>,
variables?: TVariables
) {
if (!this.last) {
return true;
}
const resultIsDifferent =
this.queryManager.getDocumentInfo(this.query).hasNonreactiveDirective ?
!equalByQuery(this.query, this.last.result, newResult, this.variables)
: !equal(this.last.result, newResult);
return (
resultIsDifferent || (variables && !equal(this.last.variables, variables))
);
}
private getLast<K extends keyof Last<TData, TVariables>>(
key: K,
variablesMustMatch?: boolean
) {
const last = this.last;
if (
last &&
last[key] &&
(!variablesMustMatch || equal(last.variables, this.variables))
) {
return last[key];
}
}
public getLastResult(
variablesMustMatch?: boolean
): ApolloQueryResult<TData> | undefined {
return this.getLast("result", variablesMustMatch);
}
public getLastError(variablesMustMatch?: boolean): ApolloError | undefined {
return this.getLast("error", variablesMustMatch);
}
public resetLastResults(): void {
delete this.last;
this.isTornDown = false;
}
public resetQueryStoreErrors() {
this.queryManager.resetErrors(this.queryId);
}
/**
* 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?: Partial<TVariables>
): Promise<ApolloQueryResult<TData>> {
const reobserveOptions: Partial<WatchQueryOptions<TVariables, TData>> = {
// Always disable polling for refetches.
pollInterval: 0,
};
// 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.
const { fetchPolicy } = this.options;
if (fetchPolicy === "cache-and-network") {
reobserveOptions.fetchPolicy = fetchPolicy;
} else if (fetchPolicy === "no-cache") {
reobserveOptions.fetchPolicy = "no-cache";
} else {
reobserveOptions.fetchPolicy = "network-only";
}
if (__DEV__ && variables && hasOwnProperty.call(variables, "variables")) {
const queryDef = getQueryDefinition(this.query);
const vars = queryDef.variableDefinitions;
if (!vars || !vars.some((v) => v.variable.name.value === "variables")) {
invariant.warn(
`Called refetch(%o) for query %o, which does not declare a $variables variable.
Did you mean to call refetch(variables) instead of refetch({ variables })?`,
variables,
queryDef.name?.value || queryDef
);
}
}
if (variables && !equal(this.options.variables, variables)) {
// Update the existing options with new variables
reobserveOptions.variables = this.options.variables = {
...this.options.variables,
...variables,
} as TVariables;
}
this.queryInfo.resetLastWrite();
return this.reobserve(reobserveOptions, NetworkStatus.refetch);
}
/**
* A function that helps you fetch the next set of results for a [paginated list field](https://www.apollographql.com/docs/react/pagination/core-api/).
*/
public fetchMore<
TFetchData = TData,
TFetchVars extends OperationVariables = TVariables,
>(
fetchMoreOptions: FetchMoreQueryOptions<TFetchVars, TFetchData> & {
updateQuery?: (
previousQueryResult: TData,
options: {
fetchMoreResult: TFetchData;
variables: TFetchVars;
}
) => TData;
}
): Promise<ApolloQueryResult<TFetchData>> {
const combinedOptions = {
...(fetchMoreOptions.query ? fetchMoreOptions : (
{
...this.options,
query: this.options.query,
...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<TFetchVars, TFetchData>;
combinedOptions.query = this.transformDocument(combinedOptions.query);
const qid = this.queryManager.generateQueryId();
// If a temporary query is passed to `fetchMore`, we don't want to store
// it as the last query result since it may be an optimized query for
// pagination. We will however run the transforms on the original document
// as well as the document passed in `fetchMoreOptions` to ensure the cache
// uses the most up-to-date document which may rely on runtime conditionals.
this.lastQuery =
fetchMoreOptions.query ?
this.transformDocument(this.options.query)
: combinedOptions.query;
// Simulate a loading result for the original query with
// result.networkStatus === NetworkStatus.fetchMore.
const { queryInfo } = this;
const originalNetworkStatus = queryInfo.networkStatus;
queryInfo.networkStatus = NetworkStatus.fetchMore;
if (combinedOptions.notifyOnNetworkStatusChange) {
this.observe();
}
const updatedQuerySet = new Set<DocumentNode>();
const updateQuery = fetchMoreOptions?.updateQuery;
const isCached = this.options.fetchPolicy !== "no-cache";
if (!isCached) {
invariant(
updateQuery,
"You must provide an `updateQuery` function when using `fetchMore` with a `no-cache` fetch policy."
);
}
return this.queryManager
.fetchQuery(qid, combinedOptions, NetworkStatus.fetchMore)
.then((fetchMoreResult) => {
this.queryManager.removeQuery(qid);
if (queryInfo.networkStatus === NetworkStatus.fetchMore) {
queryInfo.networkStatus = originalNetworkStatus;
}
if (isCached) {
// Performing this cache update inside a cache.batch transaction ensures
// any affected cache.watch watchers are notified at most once about any
// updates. Most watchers will be using the QueryInfo class, which
// responds to notifications by calling reobserveCacheFirst to deliver
// fetchMore cache results back to this ObservableQuery.
this.queryManager.cache.batch({
update: (cache) => {
const { updateQuery } = fetchMoreOptions;
if (updateQuery) {
cache.updateQuery(
{
query: this.query,
variables: this.variables,
returnPartialData: true,
optimistic: false,
},
(previous) =>
updateQuery(previous!, {
fetchMoreResult: fetchMoreResult.data,
variables: combinedOptions.variables as TFetchVars,
})
);
} else {
// If we're using a field policy instead of updateQuery, the only
// thing we need to do is write the new data to the cache using
// combinedOptions.variables (instead of this.variables, which is
// what this.updateQuery uses, because it works by abusing the
// original field value, keyed by the original variables).
cache.writeQuery({
query: combinedOptions.query,
variables: combinedOptions.variables,
data: fetchMoreResult.data,
});
}
},
onWatchUpdated: (watch) => {
// Record the DocumentNode associated with any watched query whose
// data were updated by the cache writes above.
updatedQuerySet.add(watch.query);
},
});
} else {
// There is a possibility `lastResult` may not be set when
// `fetchMore` is called which would cause this to crash. This should
// only happen if we haven't previously reported a result. We don't
// quite know what the right behavior should be here since this block
// of code runs after the fetch result has executed on the network.
// We plan to let it crash in the meantime.
//
// If we get bug reports due to the `data` property access on
// undefined, this should give us a real-world scenario that we can
// use to test against and determine the right behavior. If we do end
// up changing this behavior, this may require, for example, an
// adjustment to the types on `updateQuery` since that function
// expects that the first argument always contains previous result
// data, but not `undefined`.
const lastResult = this.getLast("result")!;
const data = updateQuery!(lastResult.data, {
fetchMoreResult: fetchMoreResult.data,
variables: combinedOptions.variables as TFetchVars,
});
this.reportResult({ ...lastResult, data }, this.variables);
}
return fetchMoreResult;
})
.finally(() => {
// In case the cache writes above did not generate a broadcast
// notification (which would have been intercepted by onWatchUpdated),
// likely because the written data were the same as what was already in
// the cache, we still want fetchMore to deliver its final loading:false
// result with the unchanged data.
if (isCached && !updatedQuerySet.has(this.query)) {
reobserveCacheFirst(this);
}
});
}
// 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.
/**
* A function that enables you to execute a [subscription](https://www.apollographql.com/docs/react/data/subscriptions/), usually to subscribe to specific fields that were included in the query.
*
* This function returns _another_ function that you can call to terminate the subscription.
*/
public subscribeToMore<
TSubscriptionData = TData,
TSubscriptionVariables extends OperationVariables = TVariables,
>(
options: SubscribeToMoreOptions<
TData,
TSubscriptionVariables,
TSubscriptionData
>
) {
const subscription = this.queryManager
.startGraphQLSubscription({
query: options.document,
variables: options.variables,
context: options.context,
})
.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, TData>>
): Promise<ApolloQueryResult<TData>> {
return this.reobserve(newOptions);
}
public silentSetOptions(
newOptions: Partial<WatchQueryOptions<TVariables, TData>>
) {
const mergedOptions = compact(this.options, newOptions || {});
assign(this.options, mergedOptions);
}
/**
* Update the variables of this observable query, and fetch the new results
* if they've changed. Most users should prefer `refetch` instead of
* `setVariables` in order to to be properly notified of results even when
* they come from the cache.
*
* 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).
*
* @param variables - The new set of variables. If there are missing variables,
* the previous values of those variables will be used.
*/
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();
}
return this.reobserve(
{
// Reset options.fetchPolicy to its original value.
fetchPolicy: this.options.initialFetchPolicy,
variables,
},
NetworkStatus.setVariables
);
}
/**
* A function that enables you to update the query's cached result without executing a followup GraphQL operation.
*
* See [using updateQuery and updateFragment](https://www.apollographql.com/docs/react/caching/cache-interaction/#using-updatequery-and-updatefragment) for additional information.
*/
public updateQuery<TVars extends OperationVariables = TVariables>(
mapFn: (
previousQueryResult: TData,
options: Pick<WatchQueryOptions<TVars, TData>, "variables">
) => TData
): void {
const { queryManager } = this;
const { result } = queryManager.cache.diff<TData>({
query: this.options.query,
variables: this.variables,
returnPartialData: true,
optimistic: false,
});
const newResult = mapFn(result!, {
variables: (this as any).variables,
});
if (newResult) {
queryManager.cache.writeQuery({
query: this.options.query,
data: newResult,
variables: this.variables,
});
queryManager.broadcastQueries();
}
}
/**
* A function that instructs the query to begin re-executing at a specified interval (in milliseconds).
*/
public startPolling(pollInterval: number) {
this.options.pollInterval = pollInterval;
this.updatePolling();
}
/**
* A function that instructs the query to stop polling after a previous call to `startPolling`.
*/
public stopPolling() {
this.options.pollInterval = 0;
this.updatePolling();
}
// Update options.fetchPolicy according to options.nextFetchPolicy.
private applyNextFetchPolicy(
reason: NextFetchPolicyContext<TData, TVariables>["reason"],
// It's possible to use this method to apply options.nextFetchPolicy to
// options.fetchPolicy even if options !== this.options, though that happens
// most often when the options are temporary, used for only one request and
// then thrown away, so nextFetchPolicy may not end up mattering.
options: WatchQueryOptions<TVariables, TData>
) {
if (options.nextFetchPolicy) {
const { fetchPolicy = "cache-first", initialFetchPolicy = fetchPolicy } =
options;
if (fetchPolicy === "standby") {
// Do nothing, leaving options.fetchPolicy unchanged.
} else if (typeof options.nextFetchPolicy === "function") {
// When someone chooses "cache-and-network" or "network-only" as their
// initial FetchPolicy, they often do not want future cache updates to
// trigger unconditional network requests, which is what repeatedly
// applying the "cache-and-network" or "network-only" policies would
// seem to imply. Instead, when the cache reports an update after the
// initial network request, it may be desirable for subsequent network
// requests to be triggered only if the cache result is incomplete. To
// that end, the options.nextFetchPolicy option provides an easy way to
// update options.fetchPolicy after the initial network request, without
// having to call observableQuery.setOptions.
options.fetchPolicy = options.nextFetchPolicy(fetchPolicy, {
reason,
options,
observable: this,
initialFetchPolicy,
});
} else if (reason === "variables-changed") {
options.fetchPolicy = initialFetchPolicy;
} else {
options.fetchPolicy = options.nextFetchPolicy;
}
}
return options.fetchPolicy;
}
private fetch(
options: WatchQueryOptions<TVariables, TData>,
newNetworkStatus?: NetworkStatus,
query?: DocumentNode
) {
// TODO Make sure we update the networkStatus (and infer fetchVariables)
// before actually committing to the fetch.
this.queryManager.setObservableQuery(this);
return this.queryManager["fetchConcastWithInfo"](
this.queryId,
options,
newNetworkStatus,
query
);
}
// Turns polling on or off based on this.options.pollInterval.
private updatePolling() {
// Avoid polling in SSR mode
if (this.queryManager.ssrMode) {
return;
}
const {
pollingInfo,
options: { pollInterval },
} = this;
if (!pollInterval || !this.hasObservers()) {
if (pollingInfo) {
clearTimeout(pollingInfo.timeout);
delete this.pollingInfo;
}
return;
}
if (pollingInfo && pollingInfo.interval === pollInterval) {
return;
}
invariant(
pollInterval,
"Attempted to start a polling query without a polling interval."
);
const info = pollingInfo || (this.pollingInfo = {} as any);
info.interval = pollInterval;
const maybeFetch = () => {
if (this.pollingInfo) {
if (
!isNetworkRequestInFlight(this.queryInfo.networkStatus) &&
!this.options.skipPollAttempt?.()
) {
this.reobserve(
{
// Most fetchPolicy options don't make sense to use in a polling context, as
// users wouldn't want to be polling the cache directly. However, network-only and
// no-cache are both useful for when the user wants to control whether or not the
// polled results are written to the cache.
fetchPolicy:
this.options.initialFetchPolicy === "no-cache" ?
"no-cache"
: "network-only",
},
NetworkStatus.poll
).then(poll, poll);
} else {
poll();
}
}
};
const poll = () => {
const info = this.pollingInfo;
if (info) {
clearTimeout(info.timeout);
info.timeout = setTimeout(maybeFetch, info.interval);
}
};
poll();
}
private updateLastResult(
newResult: ApolloQueryResult<TData>,
variables = this.variables
) {
let error: ApolloError | undefined = this.getLastError();
// Preserve this.last.error unless the variables have changed.
if (error && this.last && !equal(variables, this.last.variables)) {
error = void 0;
}
return (this.last = {
result:
this.queryManager.assumeImmutableResults ?
newResult
: cloneDeep(newResult),
variables,
...(error ? { error } : null),
});
}
public reobserveAsConcast(
newOptions?: Partial<WatchQueryOptions<TVariables, TData>>,
newNetworkStatus?: NetworkStatus
): Concast<ApolloQueryResult<TData>> {
this.isTornDown = false;
const useDisposableConcast =
// Refetching uses a disposable Concast to allow refetches using different
// options/variables, without permanently altering the options of the
// original ObservableQuery.
newNetworkStatus === NetworkStatus.refetch ||
// The fetchMore method does not actually call the reobserve method, but,
// if it did, it would definitely use a disposable Concast.
newNetworkStatus === NetworkStatus.fetchMore ||
// Polling uses a disposable Concast so the polling options (which force
// fetchPolicy to be "network-only" or "no-cache") won't override the original options.
newNetworkStatus === NetworkStatus.poll;
// Save the old variables, since Object.assign may modify them below.
const oldVariables = this.options.variables;
const oldFetchPolicy = this.options.fetchPolicy;
const mergedOptions = compact(this.options, newOptions || {});
const options =
useDisposableConcast ?
// Disposable Concast fetches receive a shallow copy of this.options
// (merged with newOptions), leaving this.options unmodified.
mergedOptions
: assign(this.options, mergedOptions);
// Don't update options.query with the transformed query to avoid
// overwriting this.options.query when we aren't using a disposable concast.
// We want to ensure we can re-run the custom document transforms the next
// time a request is made against the original query.
const query = this.transformDocument(options.query);
this.lastQuery = query;
if (!useDisposableConcast) {
// We can skip calling updatePolling if we're not changing this.options.
this.updatePolling();
// Reset options.fetchPolicy to its original value when variables change,
// unless a new fetchPolicy was provided by newOptions.
if (
newOptions &&
newOptions.variables &&
!equal(newOptions.variables, oldVariables) &&
// Don't mess with the fetchPolicy if it's currently "standby".
options.fetchPolicy !== "standby" &&
// If we're changing the fetchPolicy anyway, don't try to change it here
// using applyNextFetchPolicy. The explicit options.fetchPolicy wins.
(options.fetchPolicy === oldFetchPolicy ||
// A `nextFetchPolicy` function has even higher priority, though,
// so in that case `applyNextFetchPolicy` must be called.
typeof options.nextFetchPolicy === "function")
) {
this.applyNextFetchPolicy("variables-changed", options);
if (newNetworkStatus === void 0) {
newNetworkStatus = NetworkStatus.setVariables;
}
}
}
this.waitForOwnResult &&= skipCacheDataFor(options.fetchPolicy);
const finishWaitingForOwnResult = () => {
if (this.concast === concast) {
this.waitForOwnResult = false;
}
};
const variables = options.variables && { ...options.variables };
const { concast, fromLink } = this.fetch(options, newNetworkStatus, query);
const observer: Observer<ApolloQueryResult<TData>> = {
next: (result) => {
if (equal(this.variables, variables)) {
finishWaitingForOwnResult();
this.reportResult(result, variables);
}
},
error: (error) => {
if (equal(this.variables, variables)) {
finishWaitingForOwnResult();
this.reportError(error, variables);
}
},
};
if (!useDisposableConcast && (fromLink || !this.concast)) {
// We use the {add,remove}Observer methods directly to avoid wrapping
// observer with an unnecessary SubscriptionObserver object.
if (this.concast && this.observer) {
this.concast.removeObserver(this.observer);
}
this.concast = concast;
this.observer = observer;
}
concast.addObserver(observer);
return concast;
}
public reobserve(
newOptions?: Partial<WatchQueryOptions<TVariables, TData>>,