Skip to content

Commit

Permalink
Avoid loading:true results for warm cache reads when possible.
Browse files Browse the repository at this point in the history
Fixes #6659 and #6686.
  • Loading branch information
benjamn committed Jul 27, 2020
1 parent 8be3800 commit 5fe8fee
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
12 changes: 11 additions & 1 deletion src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class ObservableQuery<
return result;
}

const { fetchPolicy } = this.options;
const { fetchPolicy = 'cache-first' } = this.options;
if (fetchPolicy === 'no-cache' ||
fetchPolicy === 'network-only') {
result.partial = false;
Expand All @@ -159,6 +159,16 @@ export class ObservableQuery<
diff.complete ||
this.options.returnPartialData
) ? diff.result : void 0;
// If the cache diff is complete, and we're using a FetchPolicy that
// terminates after a complete cache read, we can assume the next
// result we receive will have NetworkStatus.ready and !loading.
if (diff.complete &&
result.networkStatus === NetworkStatus.loading &&
(fetchPolicy === 'cache-first' ||
fetchPolicy === 'cache-only')) {
result.networkStatus = NetworkStatus.ready;
result.loading = false;
}
}

this.updateLastResult(result);
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 @@ -1396,8 +1396,8 @@ describe('ObservableQuery', () => {
});
expect(stripSymbols(observable.getCurrentResult())).toEqual({
data: dataOne,
loading: true,
networkStatus: NetworkStatus.loading,
loading: false,
networkStatus: NetworkStatus.ready,
partial: false,
});
}).then(resolve, reject);
Expand Down
5 changes: 1 addition & 4 deletions src/react/components/__tests__/client/Query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1674,9 +1674,6 @@ describe('Query component', () => {
});
break;
case 4:
expect(props.loading).toBeTruthy();
break;
case 5:
// Good result should be received without any errors.
expect(props.error).toBeFalsy();
expect(props.data.allPeople).toBeTruthy();
Expand All @@ -1698,7 +1695,7 @@ describe('Query component', () => {
</Query>
);

return wait(() => expect(count).toBe(6)).then(resolve, reject);
return wait(() => expect(count).toBe(5)).then(resolve, reject);
}
);

Expand Down
14 changes: 2 additions & 12 deletions src/react/hoc/__tests__/mutations/recycled-queries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ describe('graphql(mutation) update queries', () => {
});
break;
case 3:
expect(this.props.data!.loading).toBeTruthy();
break;
case 4:
expect(stripSymbols(this.props.data!.todo_list)).toEqual({
id: '123',
title: 'how to apollo',
Expand Down Expand Up @@ -228,15 +225,14 @@ describe('graphql(mutation) update queries', () => {
expect(todoUpdateQueryCount).toBe(2);
expect(queryMountCount).toBe(2);
expect(queryUnmountCount).toBe(2);
expect(queryRenderCount).toBe(5);
}, 5);
}, 5);
}, 5);
}, 6);
}, 5);

return wait(() => {
expect(queryRenderCount).toBe(5);
expect(queryRenderCount).toBe(4);
});
});

Expand Down Expand Up @@ -359,12 +355,6 @@ describe('graphql(mutation) update queries', () => {
);
break;
case 3:
expect(this.props.data!.loading).toBeTruthy();
expect(stripSymbols(this.props.data!.todo_list)).toEqual(
updatedData.todo_list
);
break;
case 4:
expect(this.props.data!.loading).toBeFalsy();
expect(stripSymbols(this.props.data!.todo_list)).toEqual(
updatedData.todo_list
Expand Down Expand Up @@ -405,7 +395,7 @@ describe('graphql(mutation) update queries', () => {
});

return wait(() => {
expect(queryRenderCount).toBe(5);
expect(queryRenderCount).toBe(4);
});
});
});
3 changes: 0 additions & 3 deletions src/react/hoc/__tests__/queries/observableQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,6 @@ describe('[queries] observableQuery', () => {
expect(stripSymbols(allPeople)).toEqual(data.allPeople);
break;
case 3:
expect(loading).toBe(true);
break;
case 4:
expect(loading).toBe(false);
expect(stripSymbols(allPeople)).toEqual(data.allPeople);
break;
Expand Down

0 comments on commit 5fe8fee

Please sign in to comment.