Created
October 12, 2020 15:01
-
-
Save benawad/695889ba05171dd36ce785fd221ed9ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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