Use a GraphQL Mutation to Create a Github Issue Comment

Ian Jones
InstructorIan Jones

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 4 months ago

In this lesson, we use OneGraph's Graphiql editor to create a GraphQL mutation. This is how we can create a comment on a GitHub issue through the editor.

Ian Jones: [0:00] Now that we're successfully querying for comments, let's look at how to create our own. Over in OneGraph we'll go to our dashboard, our app, and then the data explorer. We can add a new mutation.

[0:22] We'll hit the check mark. This mutation is going to be GitHub. It's going to be "Add a comment." For our mutation we need to find the subjectId of the thing that we're commenting on. In our case, this is going to be the GitHub issue.

[0:42] I'm going to change this id to 2. I'll run the commentsListQuery, and then the id is right here, so I'll grab this id. I'll paste it in. We need to pass the body, so the last comment was this one. I will just write, "That was a great movie." Then, we'll submit the mutation.

[1:18] Oops, I need to grab the issueId, not the commentId. If we go back here, now when we run it, we'll start our app, and there's our new comment. As you can see, all you need is the subjectId of the issue that we're working with, and the body of the input that you want to put on your comment.

[2:04] We'll need some return value, so instead of clientMutationId, we will take CommentEdge { node { author login }. We'll take the body, the id, the URL in viewerDidAuthor. We'll rename this mutation to newCommentMutation. It will take a body, which is a string, and it's required, and it will take a subjectId, which is also a string, and it's required.

[2:43] Replace that here. Place this here. We need to grab the subjectId of the thing that we're commenting on. I'm going to run the commentsListQuery, and we'll grab this id, because it's the issue that we're commenting on.

[3:05] We're going to add subjectId to our query variables and then we also need a body for this mutation. Body is "This is a comment." Now, we'll run our newCommentMutation being sure to add comments where they are appropriate. There we go, the mutation works.