How to Use GraphQL to Add Comment Features to a Frontend App

This lesson will show how to extend the hook to handle adding new comment logic. It will cover adding a new GraphQL mutation and using it from the frontend application.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Full Stack Comments with Hasura and React course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Full Stack Comments with Hasura and React, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course The newline Guide to Full Stack Comments with Hasura and React

In the two previous lessons, we implemented one part of the hook and we covered the UI. Now it's time to cover adding new comments. We'll write a graph computation for inserting a new entry to the database. And then we'll call it from the frontend. Firstly, let's go back to the Hasura console to create and test a graph computation. In the Explorer, we can see insert comment1 and insert comment mutation. The latter allows us to add multiple comments, but we'll only add one at the time. For that reason, we'll go with insert comments1. In the mutation, we're going to pass topic, author, and content as arguments. We'll store it as a constant in our code and then implement the add comment function inside our hook. It's going to be pretty similar to our fetch comments function. We also need to send a post request and handle the response. Let's start with the function signature. Our graph computation takes three arguments. Topic, author, and content. We already know the topic inside of the hook as it was provided as the hook parameter. Our function needs to take two remaining pieces of information. What's the type of the parameters? Instead of writing a new one, we can use a peak utility type. Now that we have the skeleton of the function, we can add the Hasura API call. We'll use a native fetch function, which arguments will look very similar to fetching the comments. We'll provide an URL to our Hasura instance and specify the configuration. Again, it will be a post request with the same headers as before. We're going to pass the X hasura all and content. In the request body, we'll provide created graph computation and variables. Topic, content, and author. Now we need to add response handling. In case of an error, we'll set the error message in the same way as we did in lesson 2. However, what should happen in the case of a successful call? We know that it means that a new comment was added and we need to display it along with the rest of the comments. There are two ways to do so. After a successful mutation, we can fetch all the comments again. The second option is that we can assume that the request was successful and since we have all the information about the comment, we can push it to the comment state variable. If the request was indeed successful, we don't do anything. Otherwise, we need to remove it from the list. This approach is called an optimistic update. Right now, we'll go the first option but we'll cover the second one in the following lesson. We just finished the hook implementation. Let's go back to the post component and try out the add comment function. We need to extract add comment from the hook and then we'll replace the console lock in on stop_mit with the add comment function. Now let's go back to the browser and see how it works. I will add a test comment and here it goes. Awesome! So we just implemented the logic to add new comments and tested it out in our sample block application. That means we have completed the basic commenting system. In the next module, we'll improve it by adding optimistic updates and pag ination. We'll check out a few other exciting ideas as well. In this module, we have one thing left to do. Is testing the hook? We'll see how to test react hooks and components and we'll write a few unit tests. See you in a bit!