Skip to content

Commit b93388d

Browse files
phryneasjerelmillerLenz Weber-Tronicbenjamn
authored
ObservableQuery.getCurrentResult: skip the cache (#10631)
--------- Co-authored-by: Jerel Miller <[email protected]> Co-authored-by: Lenz Weber-Tronic <[email protected]> Co-authored-by: Ben Newman <[email protected]>
1 parent 6875b62 commit b93388d

File tree

8 files changed

+409
-14
lines changed

8 files changed

+409
-14
lines changed

.changeset/khaki-months-rhyme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
ObservableQuery.getCurrentResult: skip the cache if the running query should not access the cache

.git-blame-ignore-revs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
# format "ObservableQuery" test
2+
0a67647b73abd94b706242f32b88d21a1400ad50
13
# format "ObservableQuery" test (in #10597)
24
104bf11765b1db50292f9656aa8fe48e2d749a83
3-
45
# Format "DisplayClientError.js" (#10909)
56
0cb68364f2c3828badde8c74de44e9c1864fb6d4

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"typescript.tsdk": "node_modules/typescript/lib",
88
"cSpell.enableFiletypes": [
99
"mdx"
10-
]
10+
],
11+
"jest.jestCommandLine": "node_modules/.bin/jest --config ./config/jest.config.js --ignoreProjects 'ReactDOM 17' --runInBand",
1112
}

config/bundlesize.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join } from "path";
33
import { gzipSync } from "zlib";
44
import bytes from "bytes";
55

6-
const gzipBundleByteLengthLimit = bytes("33.02KB");
6+
const gzipBundleByteLengthLimit = bytes("33.07KB");
77
const minFile = join("dist", "apollo-client.min.cjs");
88
const minPath = join(__dirname, "..", minFile);
99
const gzipByteLen = gzipSync(readFileSync(minPath)).byteLength;

src/core/ObservableQuery.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
FetchMoreQueryOptions,
2929
SubscribeToMoreOptions,
3030
NextFetchPolicyContext,
31+
WatchQueryFetchPolicy,
3132
} from './watchQueryOptions';
3233
import { QueryInfo } from './QueryInfo';
3334
import { MissingFieldError } from '../cache';
@@ -86,6 +87,7 @@ export class ObservableQuery<
8687
private observers = new Set<Observer<ApolloQueryResult<TData>>>();
8788
private subscriptions = new Set<ObservableSubscription>();
8889

90+
private waitForOwnResult: boolean;
8991
private last?: Last<TData, TVariables>;
9092

9193
private queryInfo: QueryInfo;
@@ -152,6 +154,7 @@ export class ObservableQuery<
152154
this.queryManager = queryManager;
153155

154156
// active state
157+
this.waitForOwnResult = skipCacheDataFor(options.fetchPolicy);
155158
this.isTornDown = false;
156159

157160
const {
@@ -240,16 +243,19 @@ export class ObservableQuery<
240243
if (
241244
// These fetch policies should never deliver data from the cache, unless
242245
// redelivering a previously delivered result.
243-
fetchPolicy === 'network-only' ||
244-
fetchPolicy === 'no-cache' ||
245-
fetchPolicy === 'standby' ||
246+
skipCacheDataFor(fetchPolicy) ||
246247
// If this.options.query has @client(always: true) fields, we cannot
247248
// trust diff.result, since it was read from the cache without running
248249
// local resolvers (and it's too late to run resolvers now, since we must
249250
// return a result synchronously).
250251
this.queryManager.transform(this.options.query).hasForcedResolvers
251252
) {
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']();
253259
} else {
254260
const diff = this.queryInfo.getDiff();
255261

@@ -833,13 +839,22 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
833839
}
834840
}
835841

842+
this.waitForOwnResult &&= skipCacheDataFor(options.fetchPolicy);
843+
const finishWaitingForOwnResult = () => {
844+
if (this.concast === concast) {
845+
this.waitForOwnResult = false;
846+
}
847+
};
848+
836849
const variables = options.variables && { ...options.variables };
837850
const { concast, fromLink } = this.fetch(options, newNetworkStatus);
838851
const observer: Observer<ApolloQueryResult<TData>> = {
839852
next: result => {
853+
finishWaitingForOwnResult();
840854
this.reportResult(result, variables);
841855
},
842856
error: error => {
857+
finishWaitingForOwnResult();
843858
this.reportError(error, variables);
844859
},
845860
};
@@ -990,3 +1005,7 @@ export function logMissingFieldErrors(
9901005
}`, missing);
9911006
}
9921007
}
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

Comments
 (0)