Skip to content

Commit 0850e3a

Browse files
committed
Call reobserveCacheFirst only if cache.watch update not already broadcast.
1 parent 2b62839 commit 0850e3a

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/core/ObservableQuery.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { invariant } from '../utilities/globals';
2-
2+
import { DocumentNode } from 'graphql';
33
import { equal } from '@wry/equality';
44

55
import { NetworkStatus, isNetworkRequestInFlight } from './networkStatus';
@@ -29,6 +29,7 @@ import { QueryInfo } from './QueryInfo';
2929
import { MissingFieldError } from '../cache';
3030
import { MissingTree } from '../cache/core/types/common';
3131

32+
3233
const {
3334
assign,
3435
hasOwnProperty,
@@ -393,6 +394,8 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
393394
this.observe();
394395
}
395396

397+
const updatedQuerySet = new Set<DocumentNode>();
398+
396399
return this.queryManager.fetchQuery(
397400
qid,
398401
combinedOptions,
@@ -401,6 +404,12 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
401404
const data = fetchMoreResult.data as TData;
402405
const { updateQuery } = fetchMoreOptions;
403406

407+
this.queryManager.removeQuery(qid);
408+
409+
if (queryInfo.networkStatus === NetworkStatus.fetchMore) {
410+
queryInfo.networkStatus = originalNetworkStatus;
411+
}
412+
404413
if (updateQuery) {
405414
if (__DEV__ &&
406415
!warnedAboutUpdateQuery) {
@@ -428,33 +437,26 @@ once, rather than every time you call fetchMore.`);
428437
// combinedOptions.variables (instead of this.variables, which is
429438
// what this.updateQuery uses, because it works by abusing the
430439
// original field value, keyed by the original variables).
431-
this.queryManager.cache.writeQuery({
432-
query: combinedOptions.query,
433-
variables: combinedOptions.variables,
434-
data,
435-
// TODO Figure out why this breaks tests, since the result should
436-
// ultimately be broadcast by this.reobserveCacheFirst in the finally
437-
// block below. Alternatively, we rely on the cache broadcast for this
438-
// cache.writeQuery, and avoid using reobserveCacheFirst below.
439-
broadcast: false,
440+
this.queryManager.cache.batch({
441+
update: cache => {
442+
cache.writeQuery({
443+
query: combinedOptions.query,
444+
variables: combinedOptions.variables,
445+
data,
446+
});
447+
},
448+
onWatchUpdated: watch => {
449+
updatedQuerySet.add(watch.query);
450+
},
440451
});
441452
}
442453

443454
return fetchMoreResult as ApolloQueryResult<TData>;
444455

445456
}).finally(() => {
446-
this.queryManager.removeQuery(qid);
447-
448-
if (queryInfo.networkStatus === NetworkStatus.fetchMore) {
449-
queryInfo.networkStatus = originalNetworkStatus;
457+
if (!updatedQuerySet.has(this.options.query)) {
458+
this.reobserveCacheFirst();
450459
}
451-
452-
// Since fetchMore works by updating the cache, we trigger reobservation
453-
// using a temporary fetchPolicy of "cache-first", so (complete) cache
454-
// results written by fetchMore can be delivered without additional
455-
// network requests, which is relevant when this.options.fetchPolicy is
456-
// "cache-and-network" or "network-only".
457-
this.reobserveCacheFirst();
458460
});
459461
}
460462

0 commit comments

Comments
 (0)