noise

Getting started with Hasura

Apr 27, 2020
  • hasura
  • getting started
  • postgres
  • sql
  • graphql
  • api

Introduction

Hasura Architecture

Image Source

Hasura is an open source engine that connects to your databases & microservices and auto-generates a production-ready GraphQL backend.

Hasura maps your Postgres schema in to GraphQL and generates queries, mutations and subscriptions for you. Along with that they also provide different types of query filters like where, order_by, limit, offset and more to help you build complex features on your front end like filtering, sorting, pagination and more.

Deployment

Hasura provides multiple ways to deploy Hasura Engine:

With Docker, you can pretty much deploy the engine on any services that allows you to use docker.

Deploying wih Heroku

To deploy Hasura with Heroku, head over to the this documentation. Tap that Deploy to Heroku button and you'll be redirected to Heroku to deploy your app.

Create Hasura App

Give your app a name and select the region you want to deploy to and then click on Deploy App button and wait for Heroku to deploy your app.

After it's deployed, you can click on View App button and you'll be taken to Hasura console. Hasura console has 4 tabs and if you're using the latest beta version then you'll have one more tab called Actions

Adding Schema

A bit of knowledge of SQL tables might come handy but there's also a GUI way of managing tables which can also be used instead of writing SQL code in their SQL Editor. Let's start with creating a table.

Creating Tables

In Data tab, click on the Create Table button and you'll be taken to table creation form. We'll be taking an example of expenses management app for our demo.

Table Creation Form

Let's add expenses as our table name and we'll be creating following fields.

Your form should look like this now.

Filled table form

Now hit Add Table button in the bottom of the form to add the table. Now if you go to the GraphiQL tab you'll see that Hasura has generated queries for you which you can start exploring.

Playground

Hasura generates following queries, mutations and subscriptions for you.

Here expenses_by_pk is for fetching expense by the primary key. The name of the queries are also generated based on table name, which can be changed from:

Data > Select Table > Modify > Custom GraphQL Root Fields.

Custom Root Fields

After modifying, just save the changes and Hasura will update the query names for you.

Writing Queries

Hasura GraphQL engine auto-generates queries as part of the GraphQL schema from your Postgres schema model. It generates a range of possible queries and operators that also work with relationships defined in your SQL schema.

1query MyQuery {
2 expenses {
3 id
4 date
5 title
6 amount
7 category
8 payment_method
9 }
10}

You can add fields to your query as you would do with GraphQL. What Hasura gives you extra is different types of clauses to make your life easier and get built in filtering, sorting, searching functionality right away.

Writing Mutations

For each table you get three mutations:

Each mutations gives you access to batch operations. So to add a single row/object, you can use following mutation.

1mutation MyMutation {
2 create_expenses(
3 objects: {
4 date: ""
5 title: ""
6 amount: ""
7 category: ""
8 payment_method: ""
9 }
10 ) {
11 returning {
12 id
13 date
14 title
15 amount
16 category
17 payment_method
18 }
19 }
20}

To add multiple rows/objects at once, just pass an array of objects instead of just one object.

1mutation MyMutation {
2 create_expenses(
3 objects: [
4 {
5 amount: "487"
6 title: "Groceries"
7 payment_method: "Card"
8 category: "Food & Drinks"
9 date: "2017-11-17T18:30:00"
10 }
11 {
12 amount: "5400"
13 title: "Table"
14 payment_method: "Card"
15 category: "Furniture"
16 date: "2017-11-16T18:30:00"
17 }
18 ]
19 ) {
20 returning {
21 id
22 date
23 title
24 amount
25 category
26 payment_method
27 }
28 }
29}

Writing Subscriptions

Subscriptions are identical to queries in a way they're written, except for the keyword query, use subscription.

1subscription MyQuery {
2 expenses {
3 id
4 date
5 title
6 amount
7 category
8 payment_method
9 }
10}

Conclusion

I've been using Hasura at work for couple of weeks now. Hasura helped us to build a feature rich apps with little to no backend codebase. Hence decided to share what I've learned in past weeks. I also built an expenses app during last week which can be found on my Github.

I will be writing more articles on integrating Hasura with our front-end, so stay tuned. You can follow me on my Twitter or my Instagram to stay updated with the progress.

Get in touch

Have a project for me, or just want to say Hi🙋🏽‍♂️? Feel free to email me at prvnbist@gmail.com.