Tutorials

|

10

minutes read

Making CRUD APIs easier with Hasura

Hien Nguyen

September 19, 2022

feature image

A better way to debug your PHP code

A better way to debug your PHP code

Khoa Pham

November 16, 2022

Discover iOS Dark Mode Programming

Discover iOS Dark Mode Programming

thomas.le

November 1, 2022

Introduction

If you're a Backend developer, you must be used to Ctrl-C and Ctrl-V CRUD codes for different resources. It’s pretty tiring since CRUD APIs take up quite an amount - up to 70-80% of APIs.

Well, it is time you get rid of all that hard labor! With Hasura, you can build and ship APIs in real-time. In this article, we are going to introduce you to this instant real-time GraphQL APIs engine, and how you can create CRUD APIs with it.

What is Hasura?

Hasura is a service that allows us to create APIs for CRUD in the form of GraphQL. It makes your data instantly accessible over a real-time GraphQL API, so you can build and ship modern apps and APIs much faster.

Hasura connects to your databases, REST servers, GraphQL servers, and third-party APIs to provide a unified, instant real-time GraphQL API across all your data sources. Plus, we can also create APIs with custom logic if needed.

The main functions of Hasura

Databases

Hasura allows us to connect to the database, based on which Hasura can create the corresponding APIs for the tables or views existing in the database.

At the moment, Hasura only supports 4 types of databases:

Actions

a graph of Hasura actions

Actions are customized queries and mutations used for custom business logic, data validation, data enrichment, and transformation.

Remote Schema

Hasura's remote schema graph

Hasura is only capable of creating APIs for basic CRUD, so using remote schema will allow us to create APIs with more complex logic like payment APIs or connect to third-party services.

Once we have a GraphQL service, Hasura will merge our APIs with the APIs generated by Hasura together to form unified GraphQL APIs.

Events

Events will be of two types, Event Triggers, and Scheduled Triggers:

how Hasura processes events

Event triggers will catch events on specified tables in the Postgres database, and invoke webhooks to execute custom logic.

Those events can be of the following types:

  • INSERT: When a row is inserted into a table
  • UPDATE: When a row is updated in a table
  • DELETE: When a row is deleted from a table
  • MANUAL: Using the console or API, an event can be triggered manually on a row

Scheduled Triggers

how Hasura schedule triggers work

Scheduled Triggers are similar to Event Triggers, but they will be triggered at a specified time instead of catching events in the database.

Authentication and Authorization

how Hasura Authentication and Authorization works

Hasura is unable to directly perform authentication, so we need to do it externally. After authentication, the engine will receive information about the role. We can specify permissions on roles that Hasura can perform authorization.

Migrations, Seeders, and Metadata

Hasura allows us to create migrations and seeders to make deployment easier. In addition, it also allows us to export metadata, which is a snapshot of states managed by Hasura itself.

Deploy

Hasura supports quite a few deploy methods, and I recommend you deploy to cloud.hasura.io. Plus, we can also set up auto-deploy with GitHub. You can find out more here.

Hasura CLI

Hasura also supports CLI to make it easier for you to manipulate. Read more about Hasura CLI here.

How to make API using Hasura

Now I'm going to show you how to make API the simplest way with Hasura.

Register and Login

Registration and log-in with Hasura are quite easy, you just need to access the link https://cloud.hasura.io/login, then register or log in. Hasura also supports login with Google or GitHub. Once you have successfully logged in, this is what you should see:

Hasura log-in screen

Connect database

After logging in, we proceed to connect the database. First, we need to click the launch console to enter the Hasura console, then switch to the Data tab. This is where you enter your database information, once succeeded, this is what you should see:

Connect your database in Hasura

Create schema

Then we proceed to create the tables, or we can also check if the tables already exist.

Create a new schema in Hasura

Also, we can also customize the names of the fields used in GraphQL if we want.

Query

Finally, let's see the results in the API tab.

the query result in the API tab

And that's it! We have successfully created a simple CRUD GraphQL API.

Hasura Advantages & Disadvantages

Advantages

  • Saves up to 70 - 80% of codes, reducing software development time
  • Allows us to add custom business logic if needed
  • Supports CLI
  • Easy-to-use interface

Disadvantages

  • Only supports GraphQL, which will be difficult for newcomers to switch from RestAPI
  • Doesn't support many types of databases, especially MySQL (only supported in preview mode)

Conclusions

And that is how our team at FABA Technology cut down the time of copying and pasting CRUD APIs to focus more on developing. Follow us for more cool technical stuff from an Offshore Development team in Vietnam. If you are curious about our services, visit https://fabatechnology.com/ to get your questions answered.