November 16, 2022
November 1, 2022
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.
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.
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 are customized queries and mutations used for custom business logic, data validation, data enrichment, and transformation.
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 will be of two types, Event Triggers, and Scheduled Triggers:
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:
Scheduled Triggers are similar to Event Triggers, but they will be triggered at a specified time instead of catching events in the database.
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.
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.
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 also supports CLI to make it easier for you to manipulate. Read more about Hasura CLI here.
Now I'm going to show you how to make API the simplest way with Hasura.
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:
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:
Then we proceed to create the tables, or we can also check if the tables already exist.
Also, we can also customize the names of the fields used in GraphQL if we want.
Finally, let's see the results in the API tab.
And that's it! We have successfully created a simple CRUD GraphQL API.
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.