Skip to content

Instantly share code, notes, and snippets.

@0916dhkim
Created January 31, 2021 20:34
Show Gist options
  • Select an option

  • Save 0916dhkim/caf645c3e6894b09ed8bb7b55d823b5d to your computer and use it in GitHub Desktop.

Select an option

Save 0916dhkim/caf645c3e6894b09ed8bb7b55d823b5d to your computer and use it in GitHub Desktop.
How to use Relay updater function
const contentMutation = graphql`
mutation MyComponentContentMutation(
$postId: ID!
) {
deletePostContent(postId: $postId) {
id
content
}
}
`;
const POST_ID = 'uniquepostid';
const MyComponent: FC = () => {
const [deleteContent] = useMutation<MyComponentContentMutation>(contentMutation);
const onClick = () => {
deleteContent({
variables: {postId: POST_ID},
updater: (store) => {
// Find the `Post` record of interest in Relay cache.
const postRecord = store.get(POST_ID);
// Update its `content` field to `null` if the record exists.
postRecord?.setValue(null, 'content');
},
});
}
return <button onClick={onClick}>Delete Post Content!</button>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment