Skip to content

Instantly share code, notes, and snippets.

@johntran
Created August 25, 2017 22:50
Show Gist options
  • Save johntran/e938e877ba6c35f79da276704810ba43 to your computer and use it in GitHub Desktop.
Save johntran/e938e877ba6c35f79da276704810ba43 to your computer and use it in GitHub Desktop.
import { commitMutation, graphql } from 'react-relay';
import { ConnectionHandler } from 'relay-runtime';
const mutation = graphql`
mutation ProductReviewAddMutation($input: CreateProductReviewInput!) {
productReviewAdd(input: $input) {
user {
id
role
productReviews {
edges {
__typename
cursor
node {
id
productId
rating
firstName
lastName
reviewId
canEdit
comment
title
date
}
}
}
}
productReviewEdge {
__typename
cursor
node {
id
productId
rating
firstName
lastName
reviewId
canEdit
comment
title
date
}
}
}
}
`;
function commit({ environment, input, onCompleted, onError }) {
const { rating, comment, title, productId, user } = input;
return commitMutation(environment, {
mutation,
variables: {
input: { productId, rating, comment, title },
},
updater: store => {
const payload = store.getRootField('productReviewAdd');
const newEdge = payload.getLinkedRecord('productReviewEdge');
const userProxy = store.get(user.id);
const conn = ConnectionHandler.getConnection(
userProxy,
'review_productReviews',
);
if (conn && newEdge) ConnectionHandler.insertEdgeAfter(conn, newEdge);
},
onCompleted,
onError,
});
}
export default { commit };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment