1
1
import { invariant } from '../utilities/globals' ;
2
-
2
+ import { DocumentNode } from 'graphql' ;
3
3
import { equal } from '@wry/equality' ;
4
4
5
5
import { NetworkStatus , isNetworkRequestInFlight } from './networkStatus' ;
@@ -29,6 +29,7 @@ import { QueryInfo } from './QueryInfo';
29
29
import { MissingFieldError } from '../cache' ;
30
30
import { MissingTree } from '../cache/core/types/common' ;
31
31
32
+
32
33
const {
33
34
assign,
34
35
hasOwnProperty,
@@ -393,6 +394,8 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
393
394
this . observe ( ) ;
394
395
}
395
396
397
+ const updatedQuerySet = new Set < DocumentNode > ( ) ;
398
+
396
399
return this . queryManager . fetchQuery (
397
400
qid ,
398
401
combinedOptions ,
@@ -401,6 +404,12 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
401
404
const data = fetchMoreResult . data as TData ;
402
405
const { updateQuery } = fetchMoreOptions ;
403
406
407
+ this . queryManager . removeQuery ( qid ) ;
408
+
409
+ if ( queryInfo . networkStatus === NetworkStatus . fetchMore ) {
410
+ queryInfo . networkStatus = originalNetworkStatus ;
411
+ }
412
+
404
413
if ( updateQuery ) {
405
414
if ( __DEV__ &&
406
415
! warnedAboutUpdateQuery ) {
@@ -428,33 +437,26 @@ once, rather than every time you call fetchMore.`);
428
437
// combinedOptions.variables (instead of this.variables, which is
429
438
// what this.updateQuery uses, because it works by abusing the
430
439
// 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
+ } ,
440
451
} ) ;
441
452
}
442
453
443
454
return fetchMoreResult as ApolloQueryResult < TData > ;
444
455
445
456
} ) . 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 ( ) ;
450
459
}
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 ( ) ;
458
460
} ) ;
459
461
}
460
462
0 commit comments