Introduction to Hasura
We'll learn about Hasura and play a bit with its features and writing GraphQL queries. This knowledge will let us move on to the commenting system!
As we mentioned in the previous lesson, Hasura can generate GraphQL APIs from our database. It means that we don't have to build a GraphQL server on our own! Hasura connects to databases (or other GraphQL services) and provides us with a GraphQL API. In this lesson, we're going to explore Hasura's basic functionalities and see them in action!
How does Hasura work?#
Under the hood, Hasura compiles a GraphQL query into a single SQL query. We introduced new words, so let's quickly see what they mean.
An SQL — Structured Query Language — is a query language for managing data held in databases.
A compiler is a program that translates code written in one programming language into another language.
Hasura comes with more features than automatic GraphQL APIs generation. In this course, we're going to learn about role based permissions system. It lets us protect our data and restrict access to it.
Spinning up a new Hasura project#
There are various ways to "have" our own Hasura instance. We can self-host it — deploy it to any platform we wish, e.g., Heroku, Digital Ocean, etc. Or, we can use Hasura Cloud, which offers hosting for the Hasura projects and takes care of managing the infrastructure for us.
We will use the second option in this course as it's the most straightforward, and their free tier covers all of our needs to implement the commenting system.
We need to visit Hasura Cloud Dashboard in order to start using Hasura. You'll need to create a new account first. You can sign up with GitHub, Google, or using email and password.
After successfully logging into the Cloud dashboard, you'll see a welcome modal and a new project that was created for you out of the box.
We can go see the details of the project. On the main screen, there is basic project information, including the GraphQL endpoint URL. On the left sidebar navigation, we can see:
Collaborators — we can invite other people to our project.
Env vars — if needed, we can update environmental variables — e.g. if we need to enable or disable some Hasura features controlled via env vars.
Domains — we can connect the project to a domain that we own.
Usage — provides basic project usage metrics.
Creating a new database and a sample table#
In the right top corner, there's a
Launch Console button. Clicking it opens a
Hasura Console application. It's an admin dashboard to configure the databases
and additional Hasura's features. In other words, it's a GUI for the underlying
backend responsible for generating the GraphQL APIs.
In our newly created project, we don't have any database connected yet. However, Hasura provides us with one-click Heroku integration, which will create a database for us, and Hasura Console will take care of connecting it to the Hasura backend. Let's do it then!
When you click on the Data button in the top navigation bar, Hasura Console will take you to the Connect Database section. Let's click the Create Heroku Database tab and then the big button with Heroku's logo.
Note: This will require having an account on Heroku. If you don't have one, you can quickly create it for free.