Marcio Cunha

What is Hasura and how to turn database schemas directly into GraphQL APIs

Learn how Hasura automates the creation of instant GraphQL APIs from relational databases, eliminating repetitive backend boilerplate with high performance.

Marcio Cunha12 min
Also available in:EspañolPortuguês
Summary
  • Hasura reads relational database schemas like PostgreSQL and instantly generates a comprehensive GraphQL API.
  • The tool dramatically reduces the need for repetitive backend code dedicated to basic reading and writing operations.
  • Role-based access control rules ensure granular security directly at the database schema layer.
  • Real-time subscriptions powered by WebSockets update the user interface the exact moment data changes.
  • Using Hasura requires careful indexing planning and consideration for complex business logic outside standard CRUD.

The traditional challenge of building database APIs

When building a modern application, one of the most repetitive tasks is creating backend routes to connect the database to the user interface. In practice, this means writing dozens of lines of code just to receive a request, validate data, assemble an SQL query, and return the result in JSON format. This process consumes valuable engineering time, creates development bottlenecks, and generates an extra software layer that must be constantly maintained, tested, and updated.

Furthermore, data consumption needs change constantly. If the web interface requires a new field, the programmer must alter the server code, update the route, test, and deploy everything all over again. This rigid workflow slows down product teams and makes system maintenance expensive and cumbersome. It is precisely in this scenario that tools designed to eliminate mechanical work and accelerate delivery value emerge.

What Hasura is and how it works in practice

Hasura is an open-source engine that connects directly to relational databases, such as PostgreSQL, and instantly generates a robust, high-performance GraphQL API. Simply put, Hasura acts as an intelligent translator: it looks at your table structures, foreign keys, and relationships, and builds a complete catalog of queries and data mutations without requiring you to write a single line of server code.

GraphQL, in turn, is an API query language developed by Facebook that allows applications to request exactly the data they need, nothing more and nothing less. When we combine Hasura with GraphQL, the magic happens: the front-end talks directly to the database securely, asking only for the required fields in a single request. In practice, this means the application loads faster and consumes less network bandwidth, improving the browsing experience.

Architecture and the role of the real-time engine

Under the hood, Hasura is written in Haskell, a programming language known for its extreme reliability and efficiency in managing concurrent processes. When a request arrives at Hasura, it optimally translates the operation into a pure SQL statement before sending it to the database. This ensures query performance is virtually identical to that of a traditional application built from scratch, with no perceptible speed penalties.

Another major differentiator is the ability to generate real-time updates using WebSockets, a technology that maintains an open, bidirectional connection between the browser and the server. In practice, this means that if a user changes a record in the database, all other connected clients watching that information receive the update immediately, without needing to refresh the page. This feature is ideal for dashboards, chats, and team collaboration tools.

Granular security and role-based access control

One of the biggest concerns when exposing data directly to the front-end is security. After all, how do we ensure a regular user cannot access sensitive information belonging to someone else or edit data they shouldn't? Hasura solves this problem by introducing a sophisticated permission system based on roles, configured directly through the visual interface or via metadata files.

In practice, you define logical rules tied to the current user's session variables. For example, you can configure a rule stating that a customer can only read and edit records where the id field matches their own identifier. Hasura automatically injects these restrictions into the SQL query generated behind the scenes. This prevents data leaks caused by human error in application code and safeguards the system against unauthorized access.

Below is a classic example of a GraphQL query sent to Hasura to fetch a user and their respective orders in a single server trip:

query GetCustomerProfile {  customers(where: { id: { _eq: 42 } }) {  name  email  orders {  id  total_amount  status  created_at  }  }}

This simplicity in structuring queries drastically reduces client-side code complexity, allowing interface developers to build rich, interactive screens much faster.

When to use and the limits of the tool

Despite all the agility provided, Hasura is not a silver bullet that fits absolutely every software engineering scenario. It shines brightly in data-rich applications, administrative dashboards, registration systems, and mobile apps where the operational pattern heavily relies on structured queries, inserts, updates, and deletes.

On the other hand, when an application requires highly complex business logic, heavy integrations with dozens of legacy external APIs, or heavy asynchronous processing, relying solely on native database tools can become limiting. In those cases, Hasura offers custom logic features called Actions and Remote Webhooks, allowing you to connect traditional server functions to complement the data flow when needed.

Final thoughts on productivity impact

The adoption of technologies like Hasura represents a profound shift in how we think about modern system architecture. By eliminating the need to build the repetitive data access layer from scratch, companies can redirect the talent of their engineers to solve real business problems and create competitive advantages for their products.

Ultimately, turning database schemas directly into secure and efficient GraphQL APIs doesn't just mean writing less code—it means building cleaner, easier-to-maintain systems prepared to scale alongside a growing user base. Evaluating the balance between immediate productivity and long-term architecture is the secret to getting the most out of this transformative approach.