Slide 31
Slide 31 text
あえてupdate
を使ってcache
を更新してみた
const StarBadgeWithMutations = compose(
graphql(ADD_STAR_MUTATION, {
props: ({ ownProps, mutate }) => ({
addStar: (id: string) =>
mutate({ variables: { input: { starrableId: id } }, })
})
}),
graphql(REMOVE_STAR_MUTATION, {
props: ({ ownProps, mutate }) => {
return {
removeStar(id: string) {
mutate({
variables: { input: { starrableId: id } },
update: (store, { data }) => {
const queryOption = {
query: RELEASES_QUERY,
variables: { owner: ownProps.repository.owner.login, name:
ownProps.repository.name }
};
const cache = store.readQuery(queryOption);
cache.repository.viewerHasStarred = data.removeStar.starrable.viewerHasStarred;
cache.repository.stargazers.totalCount -= 1;
store.writeQuery(Object.assign({}, queryOption, { data: cache }));
}
})
}
}
}
})
)(StarBadge);