This guide is outdated. For a more up-to-date tutorial, check out Build a Cookie Clicker App with Clerk and Hasura.
While traditional applications require both frontend and backend developers, new technologies like Clerk and Hasura are making it possible to build robust backends without writing backend code.
In this tutorial, we'll leverage these new technologies to build a simple to-do list application without writing any backend code. The primary tools we'll use are:
- Hasura Cloud, for creating a frontend-accessible GraphQL API
- Heroku Postgres, for storing to-do list data
- Clerk, for authentication
- Next.js, for frontend development
- Tailwind CSS, for styling
Before we get started, you can see the final result here:
Start by signing up for Hasura Cloud.
If you already have a Hasura Cloud account, you will need to manually create a New Project. If this is your first time, a new project will automatically be created for you.
After your project initializes, you will see something like this (with a different name), go ahead and click the cog wheel to go to the project settings:
From here, you will need our project's GraphQL API URL. Please copy it, you will need it in a second:
We prepared a starter project for this tutorial, the easiest way to get started is with the following "Deploy" button. The button will prompt you through cloning the repo, initializing Clerk, and deploying the app live on Vercel. The starter project uses Next.js, Tailwind CSS and Clerk. It's already setup with some styles using Next.js and Tailwind CSS but you don't have to be proficient in either of these to follow the tutorial.
This button will first prompt you to create a Vercel account if you do not have one. When signing up, Vercel may ask you to grant access to all of your repositories or just selected ones - feel free to choose either option.
The Next step will prompt you to integrate Clerk into your project, click Install.
If you do not have a Clerk account already, you will be asked to create one now.
Next, you will be asked to select an application name and a brand color. Then, click "Create application":
After the window closes, click Continue and you will be prompted to pick a Git provider. In this tutorial, we will use GitHub:
This is where you will use Hasura Cloud's GraphQL API URL you copied earlier. Add it below and click Deploy.
While you wait for Vercel to deploy our project, you can move to GitHub, where Vercel has created a new repository on your behalf. Go ahead and clone it locally.
To clone, go to your desired folder, open a terminal and paste:
Then, go inside the project folder and run:
This will install the necessary dependencies.
After this, go ahead and launch your project:
If you haven’t previously used Vercel on your computer, you will be asked to sign in when you launch the project.
You will be prompted to set up link this local project with the Vercel project. Respond **Y** to each prompt.
Then, you will see your project running on http://localhost:3000.
Hasura is one of the integrations that Clerk offers, with many more coming the future. To use it, you need to enable it. Go to your Clerk dashboard, click on your application -> Development -> Integrations and activate Hasura.
Before leaving the dashboard, go to Home and copy your Frontend API, you'll need to to create the link between Clerk and Hasura.
With your project already running, it's time to go back to Hasura and start setting up the database.
Go back to Hasura, click the cog wheel, click "Env vars" and then "New Env Var".
Pick HASURA_GRAPHQL_JWT_SECRET from the list and then add this, replacing %FRONTEND_API% with the Frontend API you copied from Clerk.
Click "Add" and then, click "Launch Console".
This will bring us to GraphiQL. GraphiQL is the GraphQL integrated development environment (IDE). It's a powerful tool you can use to interact with the API.
After GraphiQL opens, the first thing you need to do is to create a table. Start by clicking Data on the top navbar:
For this tutorial, we recommend creating a Heroku database for free:
If you don't have a Heroku account, now is the time to create one.
Follow the steps and the database will automatically be created and linked for you.
After the database is created, click "Public" and then "Create Table"
Fill the table like this and "Add Table".
This not only creates our table, but also triggers Hasura to create a GraphQL backend.
After creating the table, the next step is to restrict who can access the data. By default, Hasura is configured for all fields to be public. You need to set permissions and fix that.
You need to create a new role called "user" and edit each of the four possible permissions they have. If you are familiar with CRUD (Create, Read, Update, Delete), this is basically the same thing.
For Insert permissions, choose that the user can only set the
title of a to-do when a new one is created. There others all have default values:
idis autogenerated (set during table creation)
created_atis autogenerated to
now()(set during table creation)
user_idis set to the requesting user's ID
user_id is dependent on the particular request, it must be configured as a "Column preset."Set it to
X-Hasura-User-Id from the "session variable."
When you use Clerk's Hasura integration,
X-Hasura-User-ID is automatically set in the session variable that gets sent to Hasura. The code to retrieve the session variable and send it to Hasura is in
For Select permissions, you want to configure Hasura so users can only read their own to-dos. You can verify this by "checking" if the to-do's
user_id is the same as the
X-Hasura-User-Id you receive from the session variable.
If the user ID's match, you can grant read permissions to every column. The exact configuration required is below:
For Update permissions, you want to include the same "check" as Select, to ensure that a user can only update their own to-dos.
However, if the check is valid, you don't want the user to have permission to update every column. Instead, only grant permission to update the
For Delete permissions, you want to include the same "check" as Select, to ensure that a user can only delete their own to-dos.
That's all of the permissions we need to set! Now, let's work on the frontend.
Go to http://localhost:3000 and create an account on your app. Then, click "Start saving your todos" and you will see this:
These is sample data and is still static. In the next steps of the tutorial, we will connect this list to Hasura and your database, so users can create and manage their own to-dos.
The first step is giving users the ability to create a to-do. We will do this from
If you look at the
onSubmit function, you will see that nothing will currently happen when the user clicks add. You must create a GraphQL "mutation" to update the database when add is clicked.
Replace the top of your file (everything above the return statement) with this code:
This mutation accepts a title and passes it to the
insert_todos_one method that Hasura has created for us.
Now, let's go back to our frontend and try adding a todo.
You'll see notice that nothing happens on the frontend, and that's expected because we're still reading static to-dos. But, let's check the database to see if the mutation succeeded. Go back to the Hasura Cloud Console, copy and paste the following query and click the play button:
You should see your todo was created successfully:
Now, we will update the frontend to read the user's to-dos from Hasura. You can do this from
The file starts by showing static data. Update the component to instead run a GraphQL "query":
First, we created a query that gets all to-dos (remember, the user can only see the ones attached to their own
user_id). We set the query to return
completed. We order the to-dos by
created_at descending, so the newest are first in the list.
useQuery returns an object so you can render different things depending on if the data is loading, if there's an error, or if the data has been retrieved.
We've configured an early return while the data is loading or if there's an error, then render the list if that is available. After saving, you should see something like this:
Let's try adding a new todo.
You should see that the form clears after clicking "Add", but the list below doesn't automatically update. However, if you manually refresh the page, you will see new to-do.
That's not the best experience and we will fix this later by implementing a cache, so your can keep your database and your frontend in sync.
Before that, let's implement toggle and delete mutations.
components/SingleTodo.js, which is the component the renders for each individual to-do.
Update the code to add a delete mutation when the delete button is clicked:
Now, try deleting a todo. It works, but you get same experience as inserting. You need to refresh the page to see it.
We will fix this shortly, but first let's add toggle functionality.
components/SingleTodo.js, now you can add a new toggle mutation. Here is the updated component with both delete and toggle functionality:
Now, every CRUD operation works. But you need still need to refresh the page to see changes. Let's fix that.
Notice we are importing
GET_TODOS, we'll need it for the next step.
The GraphQL library this tutorial uses, Apollo, implements a dynamic, local cache. Instead of reloading the full list of updates after each mutation, you can run the mutations against your local cache. Then, the to-do list on your frontend will automatically be updated.
One great feature of this cache is called the
optimisticResponse. With this, you can assume that your GraphQL mutations will succeed and reflect the change in your frontend right away, instead of waiting for the success message from Hasura. The
optimisticResponse is preferred for your to-do app since you're not anticipating any errors, and it results in a faster-feeling user experience.
To use the cache, you need to add the
optimisticResponse parameters to your mutation functions.
Finally, we must leverage the cache in
At the top of the file, add:
And update your
onSubmit as follows:
That's it! You now have a complete to-do list using Clerk, Hasura, and Next.js - and you didn't write any backend code. It's powerful, easy to configure, and easy to scale.