Skip to content

Commit

Permalink
Pull partial data from cache even when fetchType === FetchType.refetch.
Browse files Browse the repository at this point in the history
Should fix apollographql#4741.
  • Loading branch information
benjamn committed Apr 23, 2019
1 parent 9fe470c commit 82e7530
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
73 changes: 73 additions & 0 deletions packages/apollo-client/src/__tests__/local-state/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,79 @@ describe('Cache manipulation', () => {
expect({ ...data }).toMatchObject({ field: '1234' });
});
});

it("should read @client fields from cache on refetch (#4741)", function (done) {
const query = gql`
query FetchInitialData {
serverData @client {
id
title
}
selectedItemId @client
}
`;

const mutation = gql`
mutation Select {
select(itemId: $id) @client
}
`;

const serverData = {
__typename: "ServerData",
id: 123,
title: "Oyez and Onoz",
};

let selectedItemId = -1;
const client = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.empty(),
resolvers: {
Query: {
serverData() {
return serverData;
},
selectedItemId() {
return selectedItemId;
},
},
Mutation: {
select(_, { itemId }) {
selectedItemId = itemId;
}
}
},
});

client.watchQuery({ query }).subscribe({
next(result) {
expect(result).toEqual({
data: {
serverData,
selectedItemId,
},
loading: false,
networkStatus: 7,
stale: false,
});

if (selectedItemId !== 123) {
client.mutate({
mutation,
variables: {
id: 123,
},
refetchQueries: [
"FetchInitialData",
],
});
} else {
done();
}
},
});
});
});

describe('Sample apps', () => {
Expand Down
14 changes: 5 additions & 9 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,13 @@ export class QueryManager<TStore> {
options = { ...options, variables };

let storeResult: any;
let needToFetch: boolean =
const isNetworkOnly =
fetchPolicy === 'network-only' || fetchPolicy === 'no-cache';
let needToFetch = isNetworkOnly;

// If this is not a force fetch, we want to diff the query against the
// store before we fetch it from the network interface.
// TODO we hit the cache even if the policy is network-first. This could be unnecessary if the network is up.
if (
fetchType !== FetchType.refetch &&
fetchPolicy !== 'network-only' &&
fetchPolicy !== 'no-cache'
) {
// Unless we are completely skipping the cache, we want to diff the query
// against the cache before we fetch it from the network interface.
if (!isNetworkOnly) {
const { complete, result } = this.dataStore.getCache().diff({
query,
variables,
Expand Down

0 comments on commit 82e7530

Please sign in to comment.