Skip to content

Commit

Permalink
Make ObservableQuery#getCurrentResult always call queryInfo.getDiff().
Browse files Browse the repository at this point in the history
Appears to fix #7978.

Note that QueryInfo#getDiff knows how to handle fetchPolicy: "no-cache",
so it's safe to call it even in that case.
  • Loading branch information
benjamn committed Jun 23, 2021
1 parent ea0d6b0 commit c5cfd60
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
41 changes: 17 additions & 24 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ export class ObservableQuery<
}

public getCurrentResult(saveAsLastResult = true): ApolloQueryResult<TData> {
const { lastResult } = this;
const {
lastResult,
options: {
fetchPolicy = "cache-first",
},
} = this;

const networkStatus =
this.queryInfo.networkStatus ||
Expand All @@ -182,33 +187,18 @@ export class ObservableQuery<
networkStatus,
} as ApolloQueryResult<TData>;

if (this.isTornDown) {
return result;
}

const { fetchPolicy = 'cache-first' } = this.options;
if (fetchPolicy === 'no-cache' ||
fetchPolicy === 'network-only') {
// Similar to setting result.partial to false, but taking advantage
// of the falsiness of missing fields.
delete result.partial;
} else if (
!result.data ||
// If this.options.query has @client(always: true) fields, we cannot
// trust result.data, 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). TODO In the future
// (after Apollo Client 3.0), we should find a way to trust
// this.lastResult in more cases, and read from the cache only in
// cases when no result has been received yet.
!this.queryManager.transform(this.options.query).hasForcedResolvers
) {
// 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 diff = this.queryInfo.getDiff();
// XXX the only reason this typechecks is that diff.result is inferred as any

result.data = (
diff.complete ||
this.options.returnPartialData
) ? diff.result : void 0;

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 All @@ -220,7 +210,10 @@ export class ObservableQuery<
result.loading = false;
}
delete result.partial;
} else {
} else if (fetchPolicy !== "no-cache") {
// Since result.partial comes from diff.complete, and we shouldn't be
// using cache data to provide a DiffResult when the fetchPolicy is
// "no-cache", avoid annotating result.partial for "no-cache" results.
result.partial = true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1648,15 +1648,16 @@ describe('ObservableQuery', () => {
});

expect(observable.getCurrentResult()).toEqual({
data: void 0,
data: dataOne,
loading: true,
networkStatus: 1,
networkStatus: NetworkStatus.loading,
});

subscribeAndCount(reject, observable, (handleCount, subResult) => {
if (handleCount === 1) {
expect(subResult).toEqual({
loading: true,
data: dataOne,
networkStatus: NetworkStatus.loading,
});
} else if (handleCount === 2) {
Expand Down

0 comments on commit c5cfd60

Please sign in to comment.