@@ -28,6 +28,7 @@ import {
28
28
FetchMoreQueryOptions ,
29
29
SubscribeToMoreOptions ,
30
30
NextFetchPolicyContext ,
31
+ WatchQueryFetchPolicy ,
31
32
} from './watchQueryOptions' ;
32
33
import { QueryInfo } from './QueryInfo' ;
33
34
import { MissingFieldError } from '../cache' ;
@@ -86,6 +87,7 @@ export class ObservableQuery<
86
87
private observers = new Set < Observer < ApolloQueryResult < TData > > > ( ) ;
87
88
private subscriptions = new Set < ObservableSubscription > ( ) ;
88
89
90
+ private waitForOwnResult : boolean ;
89
91
private last ?: Last < TData , TVariables > ;
90
92
91
93
private queryInfo : QueryInfo ;
@@ -152,6 +154,7 @@ export class ObservableQuery<
152
154
this . queryManager = queryManager ;
153
155
154
156
// active state
157
+ this . waitForOwnResult = skipCacheDataFor ( options . fetchPolicy ) ;
155
158
this . isTornDown = false ;
156
159
157
160
const {
@@ -240,16 +243,19 @@ export class ObservableQuery<
240
243
if (
241
244
// These fetch policies should never deliver data from the cache, unless
242
245
// redelivering a previously delivered result.
243
- fetchPolicy === 'network-only' ||
244
- fetchPolicy === 'no-cache' ||
245
- fetchPolicy === 'standby' ||
246
+ skipCacheDataFor ( fetchPolicy ) ||
246
247
// If this.options.query has @client (always: true) fields, we cannot
247
248
// trust diff.result, since it was read from the cache without running
248
249
// local resolvers (and it's too late to run resolvers now, since we must
249
250
// return a result synchronously).
250
251
this . queryManager . transform ( this . options . query ) . hasForcedResolvers
251
252
) {
252
- // Fall through.
253
+ // Fall through.
254
+ } else if ( this . waitForOwnResult ) {
255
+ // This would usually be a part of `QueryInfo.getDiff()`.
256
+ // which we skip in the waitForOwnResult case since we are not
257
+ // interested in the diff.
258
+ this . queryInfo [ 'updateWatch' ] ( ) ;
253
259
} else {
254
260
const diff = this . queryInfo . getDiff ( ) ;
255
261
@@ -833,13 +839,22 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
833
839
}
834
840
}
835
841
842
+ this . waitForOwnResult &&= skipCacheDataFor ( options . fetchPolicy ) ;
843
+ const finishWaitingForOwnResult = ( ) => {
844
+ if ( this . concast === concast ) {
845
+ this . waitForOwnResult = false ;
846
+ }
847
+ } ;
848
+
836
849
const variables = options . variables && { ...options . variables } ;
837
850
const { concast, fromLink } = this . fetch ( options , newNetworkStatus ) ;
838
851
const observer : Observer < ApolloQueryResult < TData > > = {
839
852
next : result => {
853
+ finishWaitingForOwnResult ( ) ;
840
854
this . reportResult ( result , variables ) ;
841
855
} ,
842
856
error : error => {
857
+ finishWaitingForOwnResult ( ) ;
843
858
this . reportError ( error , variables ) ;
844
859
} ,
845
860
} ;
@@ -990,3 +1005,7 @@ export function logMissingFieldErrors(
990
1005
} `, missing ) ;
991
1006
}
992
1007
}
1008
+
1009
+ function skipCacheDataFor ( fetchPolicy ?: WatchQueryFetchPolicy /* `undefined` would mean `"cache-first"` */ ) {
1010
+ return fetchPolicy === "network-only" || fetchPolicy === "no-cache" || fetchPolicy === "standby" ;
1011
+ }
0 commit comments