Created
January 28, 2020 06:15
-
-
Save jelorivera08/cba7496ad28a7cbc7a715f772dfc7353 to your computer and use it in GitHub Desktop.
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
import { commitMutation, graphql } from 'react-relay'; | |
import environment from '../../../../environment'; | |
const mutation = graphql` | |
mutation updateNoteMutation($content: String, $_id: ID) { | |
updateNote(_id: $_id, content: $content) { | |
_id | |
content | |
} | |
} | |
`; | |
function updateNoteMutation(_id, content) { | |
const variables = { | |
_id, | |
content | |
}; | |
commitMutation(environment, { | |
mutation, | |
variables, | |
onCompleted: (response, errors) => { | |
console.log('Response received from server.'); | |
}, | |
updater: store => { | |
const newUpdatedNote = store.getRootField('updateNote'); | |
const root = store.getRoot(); | |
const notes = root.getLinkedRecords('notes'); | |
const newNotes = notes.filter(v => v.getValue('_id') !== _id); | |
root.setLinkedRecords([...newNotes, newUpdatedNote], 'notes'); | |
}, | |
onError: err => console.error(err) | |
}); | |
} | |
export default updateNoteMutation; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment