Skip to content

Instantly share code, notes, and snippets.

@jbaxleyiii
Last active April 11, 2017 00:47
Show Gist options
  • Save jbaxleyiii/ed1043973d3454cec43f06d6658c77d0 to your computer and use it in GitHub Desktop.
Save jbaxleyiii/ed1043973d3454cec43f06d6658c77d0 to your computer and use it in GitHub Desktop.
const SUBMIT_COMMENT_MUTATION = gql`
mutation submitComment($repoFullName: String!, $commentContent: String!) {
submitComment(repoFullName: $repoFullName, commentContent: $commentContent) {
postedBy {
login
html_url
}
createdAt
content
}
}
`;
// used to update the Comment query after the muation runs
const updateQueries = {
Comment: (prev, { mutationResult }) => {
const newComment = mutationResult.data.submitComment;
return update(prev, {
entry: { comments: { $unshift: [newComment] } },
});
},
};
const CommentsPageWithMutations = graphql(SUBMIT_COMMENT_MUTATION, {
props: ({ ownProps, mutate }) ({
submit: ({ repoFullName, commentContent }) => mutate({
variables: { repoFullName, commentContent },
optimisticResponse: {
__typename: 'Mutation',
submitComment: {
__typename: 'Comment',
postedBy: ownProps.currentUser,
createdAt: +new Date,
content: commentContent,
},
},
updateQueries,
});
}),
})(CommentsPage);
@zirho
Copy link

zirho commented Apr 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment