Skip to content

Commit

Permalink
Fix cached data being returned in getCurrentResult for certain fetch …
Browse files Browse the repository at this point in the history
…policies

Fixes #8697
  • Loading branch information
brainkim committed Aug 27, 2021
1 parent 69b3336 commit 63fc7c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,30 @@ export class ObservableQuery<
networkStatus,
} as ApolloQueryResult<TData>;

// 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).
if (!this.queryManager.transform(this.options.query).hasForcedResolvers) {
const { fetchPolicy = "cache-first" } = this.options;
const shouldReturnCachedData = lastResult || (
fetchPolicy !== 'network-only' &&
fetchPolicy !== 'no-cache' &&
fetchPolicy !== 'standby'
);
if (
shouldReturnCachedData &&
// 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.transform(this.options.query).hasForcedResolvers
) {
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;
}

const { fetchPolicy = "cache-first" } = this.options;
if (diff.complete) {
// If the diff is complete, and we're using a FetchPolicy that
// terminates after a complete cache read, we can assume the next
Expand Down
4 changes: 2 additions & 2 deletions src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ describe('ObservableQuery', () => {
});

expect(observable.getCurrentResult()).toEqual({
data: dataOne,
data: undefined,
loading: true,
networkStatus: NetworkStatus.loading,
});
Expand All @@ -2036,7 +2036,7 @@ describe('ObservableQuery', () => {
if (handleCount === 1) {
expect(subResult).toEqual({
loading: true,
data: dataOne,
data: undefined,
networkStatus: NetworkStatus.loading,
});
} else if (handleCount === 2) {
Expand Down

0 comments on commit 63fc7c7

Please sign in to comment.