Skip to content

Instantly share code, notes, and snippets.

@benawad
Created October 12, 2020 15:01
Show Gist options
  • Save benawad/695889ba05171dd36ce785fd221ed9ec to your computer and use it in GitHub Desktop.
Save benawad/695889ba05171dd36ce785fd221ed9ec to your computer and use it in GitHub Desktop.
import { produce } from "immer";
import { ApolloCache } from "@apollo/client";
export const updateQuery = (
store,
query,
fn,
variables
) => {
let data;
try {
data = store.readQuery({
query,
variables,
});
} catch {}
if (data) {
const newData = produce(data, fn);
store.writeQuery({
query,
variables,
data: newData,
});
}
};
// example 1
update: store => {
updateQuery(store, postsQuery, x => {
x.posts = x.posts.filter(y => y.id !== id);
});
}
// example 2
update: (store, {data}) => {
updateQuery(store, postsQuery, x => {
x.posts.unshift(data.newPost)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment