Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jelorivera08/51514d05d39fbaf289c58cf692cf9fb1 to your computer and use it in GitHub Desktop.
Save jelorivera08/51514d05d39fbaf289c58cf692cf9fb1 to your computer and use it in GitHub Desktop.
import { commitMutation, graphql } from 'react-relay';
import environment from '../../../../environment';
const mutation = graphql`
mutation createNoteMutation($content: String) {
createNote(content: $content) {
_id
content
}
}
`;
function createNoteMutation(content) {
const variables = {
content
};
commitMutation(environment, {
mutation,
variables,
onCompleted: (response, errors) => {
console.log('Response received from server.');
},
updater: store => {
const payload = store.getRootField('createNote');
const root = store.getRoot();
const notes = root.getLinkedRecords('notes');
const newNotes = [...notes, payload];
root.setLinkedRecords(newNotes, 'notes');
},
onError: err => console.error(err)
});
}
export default createNoteMutation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment