Created
January 31, 2021 20:34
-
-
Save 0916dhkim/caf645c3e6894b09ed8bb7b55d823b5d to your computer and use it in GitHub Desktop.
How to use Relay updater function
This file contains hidden or 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
| 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