Create a No-code GraphQL Server Using Hasura and PostgreSQL

Reading Time: 7 minutes
No-code GraphQL Server Using Hasura and PostgreSQL

To handle CRUD, authorization, and business logic, backend developers frequently need to write several lines of code. All of this code must be tested, debugged, and maintained for the duration of the project. This consumes a significant amount of time that developers could be used to create new features.

This blog will demonstrate to you how Hasura and PostgreSQL can help you accelerate app development and easily launch backends.

Hasura is the real deal when it comes to building fast and preventing reinventing the wheel. Adopting Hasura allows you to launch faster and create a product that is highly performant, secure, fault-tolerant, scalable, and simple to maintain.

Having no infrastructure headaches will also lower your operating costs and allow you to concentrate on front-end development and the user-facing aspects of your app.

In this blog, we will cover:

  • GraphQL
  • Hasura
  • PostgreSQL
  • Hands-on
  • Conclusion

GraphQL

GraphQL is an API query language and runtime for answering queries with existing data. GraphQL provides a comprehensive and understandable description of the data in your API, enables clients to request only what they require, simplifies the evolution of APIs over time, and enables powerful developer tools.

The appeal of GraphQL stems primarily from the concept of asking for and receiving exactly what you require – nothing more, nothing less. GraphQL returns a very predictable result when queries are sent to your API, with no over or under-fetching, ensuring that GraphQL apps are fast, stable, and scalable.

Hasura

Hasura is an open-source GraphQL engine that generates GraphQL and REST API endpoints based on the schema of your database. It allows you to run custom business logic over GraphQL by supporting data modeling, real-time querying, event programming, role-based authorization, and actions.

Why Hasura is Fast?

No-code GraphQL Server Using Hasura and PostgreSQL
  • High Productivity: Hasura can process a large number of queries per second (1000 q/sec) while using a small amount of RAM (50MB) and with low latency.
  • Concurrency and Realtime: Hasura allows for massive concurrency, which is particularly useful in real-time applications. We tested this, for example, by benchmarking our subscriptions; with 1 million subscriptions sending unique updates to 1 million connected clients every second.
  • Large queries & Results: Despite being a layer on top of Postgres, Hasura has been able to match native Postgres performance for fairly large queries and (or) large results.

PostgreSQL

PostgreSQL is a popular and powerful open-source, object-oriented relational database that competes with Oracle Database and Microsoft SQL Server in terms of enterprise-class features. PostgreSQL is ranked fourth in terms of global popularity by DB-Engines, and its popularity is growing. This should come as no surprise given that PostgreSQL databases are used by many web and mobile applications, as well as analytical tools.

Hands-on

No-code GraphQL Server Using Hasura and PostgreSQL

For this blog, we are going to use the docker image for Hasura. Once we run a container, it will spin up a GUI that includes GraphiQL IDE which is just a console to help us test our GraphQL endpoints, and a dashboard to manage our data.

Note: you can also configure using the docker-compose.yml in the root of the project.

Folder Structure

No-code GraphQL Server Using Hasura and PostgreSQL

Installations

For this blog, we need to install Docker because we are going to use the docker image for Hasura. The database is PostgreSQL and you can either install it locally or you can use a cloud solution such as ElephantSQL which is used in this blog.

Go to ElephantSQL and create an account. They offer free tiers so you do not have to worry about billing. Once on the dashboard, you can create a new instance

No-code GraphQL Server Using Hasura and PostgreSQL

Pick the tiny turtle plan because it is free. For the AWS region, select any that you feel will be the better option in terms of geographical region and latency.

After it has been created, copy/take note of the URL which we shall use on the Hasura dashboard.

No-code GraphQL Server Using Hasura and PostgreSQL

Running Hasura

Once you have Docker installed and the docker daemon is running, go to the terminal and create a project folder and open it in the terminal.

Type either of the following commands: curl https://raw.githubusercontent.com/hasura/graphql-engine/stable/install-manifests/docker-compose/docker-compose.yaml -o docker-compose.yml or wget https://raw.githubusercontent.com/hasura/graphql-engine/stable/install-manifests/docker-compose/docker-compose.yaml.

Then run a container using docker compose up or docker compose up -d to run in detached mode. If it runs successfully, you will be able to access the dashboard at http://localhost:8080/console

You should be able to see the interface as shown below (don’t worry if you don’t have any mutation or query):

No-code GraphQL Server Using Hasura and PostgreSQL

Let’s begin by connecting our database, for that, click on ‘DATA’ on the navigation bar. You should be able to see the interface below:

No-code GraphQL Server Using Hasura and PostgreSQL

Once you have filled in all the details most importantly the Database URL, click on Connect Database, and Hasura will do the rest for you.

Click on View Database, then ‘Create Table’ to create a new table.

You can use the ‘Frequently use columns’ button to create columns like id(UUID) and created_at(Timestamp)

No-code GraphQL Server Using Hasura and PostgreSQL

Then click on ‘Add Table’

Note: You can create an aggregated primary key or just use the UUID.

Note: If your table had any existing tables, click on ‘public’ on the left menu then expand the ‘Untracked tables or views’ section and click on ‘Track’ on the respective table.

Navigate back to the ‘API’ page i.e the GraphiQL IDE and you will see that a query is automatically created in the explorer.

In GraphQL there are two main types of operations i.e a Query and a Mutation. A Query is the equivalent of a GET request in REST and a Mutation is the equivalent of a POST request.

We can add a mutation in the ‘Add New’ section at the bottom of the explorer. Note that it creates all the CRUD operations.

Let us make a mutation to create a user, start by expanding the ‘insert_users’ section in the explorer. We can just check the name field because the others are auto-generated and most importantly, check the ‘affected_rows’ field to return the affected rows.

No-code GraphQL Server Using Hasura and PostgreSQL

Next, we’ll query perform a query operation. Make sure you check at least one field that you want to be included in the response. Note: These queries can become nested once you start defining relationships in your table.

Let us try adding a ‘sys_roles’ table and defining the relationship between users and system roles so that we see how we can perform more complex queries.

No-code GraphQL Server Using Hasura and PostgreSQL

Note: Make sure that you track all foreign key relationships of your schema as well.

Conclusion

In this blog, we demonstrated how we can create a no-code Hasura GraphQL server using the PostgreSQL database. We also learned how we can use the Hasura GUI to configure the GraphQL server. Configuration can also be alternatively done using the docker-compose.yml file.

This may be an advantage to those who want to switch from REST to GraphQL without adding more configurations to the underlying architecture. It may also be a good option for those who are considering a serverless architecture because Hasura is stateless. 

In future blogs, we shall look at how we can secure a Hasura GraphQL server. We will come up with more such use cases in our upcoming blogs.

Meanwhile…

If you are a Backend Developer and want to explore more about the above topics, here are a few of our blogs for your reference:

Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.

Keep Exploring -> Keep Learning -> Keep Mastering 

At Workfall, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you’re looking to work with global clients, build cutting-edge products, and make big bucks doing so, give it a shot at workfall.com/partner today!

Back To Top