Created
August 25, 2017 22:50
-
-
Save johntran/e938e877ba6c35f79da276704810ba43 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 { 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