Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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