Add workaround for cache misses

This commit is contained in:
Ramon Wenger 2018-10-29 13:23:42 +01:00
parent 3416465f8a
commit daa8491578
2 changed files with 15 additions and 5 deletions

View File

@ -57,14 +57,12 @@
}
},
update(store, {data: {deleteRoom: {success}}}) {
try {
if (success) {
const data = store.readQuery({query: ROOMS_QUERY});
if (success) {
const data = store.readQuery({query: ROOMS_QUERY});
if (data) {
data.rooms.edges.splice(data.rooms.edges.findIndex(edge => edge.node.id === id), 1);
store.writeQuery({query: ROOMS_QUERY, data});
}
} catch (e) {
// Query did not exist in the cache, and apollo throws a generic Error. Do nothing
}
}
})

View File

@ -49,6 +49,18 @@ const cache = new InMemoryCache({
}
});
// TODO: Monkey-patching in a fix for an open issue suggesting that
// `readQuery` should return null or undefined if the query is not yet in the
// cache: https://github.com/apollographql/apollo-feature-requests/issues/1
cache.originalReadQuery = cache.readQuery;
cache.readQuery = (...args) => {
try {
return cache.originalReadQuery(...args);
} catch (err) {
return undefined;
}
};
// Create the apollo client
export default new ApolloClient({
link: composedLink,