Hasura - Effortless unified API on all your data
How to ship a rock-solid API on your data – in minutes
Overview
I want to show you something incredible. In this post, I’ll talk about Hasura - an open-source engine that provides instant, real-time GraphQL and REST APIs for your databases and applications. It acts as a data access layer, simplifying the process of building and managing APIs by automating the generation of GraphQL or REST endpoints based on your existing database structure. The Hasura engine is an open source project which supercharges the building of modern applications by providing access to data via a single, composable, secure API endpoint.
But first … a quick refresher on GraphQL
GraphQL is a query language for APIs and a runtime for executing those queries by using a type system that you define for your data. It was developed by Facebook and released as an open-source project in 2015. Unlike traditional REST APIs, where each endpoint returns a fixed structure of data, GraphQL allows clients to request exactly the data they need, making data retrieval more efficient and flexible.
Below is an example of a Graphql API call
I’ve added a link at the end of the article, in case you’d like to experiment with GraphQL queries yourself.
So how does Hasura help?
Hasura lets you provision instant, real-time GraphQL and REST APIs for your data, making it very easy to build
Data-Driven Applications: It is ideal for quickly building applications where real-time data interaction is important, such as dashboards, collaborative tools, or social feeds.
Backend for Mobile Apps: Hasura can serve as the backend to mobile applications needing consistent and efficient data querying and updates.
Microservices Integration: It can be used to unify different data sources, making it easier to expose a unified API across a microservices architecture.
Basically, as long as you have a database, Hasura makes it super easy to build an API on top of your existing data.
Key Features of Hasura
Instant GraphQL API Hasura automatically generates a fully functional GraphQL API based on your database schema, allowing for flexible and efficient data querying.
Real-Time Capabilities: It supports real-time data updates with GraphQL subscriptions, enabling applications to receive live updates as data changes.
REST Endpoints: While primarily a GraphQL engine, Hasura also allows users to create REST endpoints from the same data source.
Authorization and Security: Built-in role-based access control (RBAC) and security policies let you define granular access permissions for different users.
Supports Multiple Databases: Hasura supports various databases, with strong initial support for PostgreSQL and extending to others like MySQL and SQL Server.
Remote Schemas and Integrations: It allows the integration of other GraphQL services as remote schemas, combining various APIs into a single endpoint.
Custom Business Logic: Users can add custom business logic through serverless functions or REST APIs that Hasura can integrate with.
Let’s play around with Hasura
I created an account on Hasura.io and chose the free-tier for my experimentation. This did not require a credit card, which was very welcome. I chose AWS as my cloud provider for this demo and I was surprised to see this setup becoming available to me in under 30 seconds. Here’s how my account looked like, after creation.
Now let us see what all we can do with Hasura
It’s really simple to connect your data
I did not have a database lying around, so I provisioned a free Postgres instance using Neon. This was pretty good but I needed data to start playing around with the same. I wrote a simple ChatGPT prompt to create some mock data and populate my database with the same.
Now that my table was created and the data became available, it was time to start exploring the graphql API. And voila! This happened instantly. The moment my database connection became available, the API was ready to go. This is absolutely amazing because I can start using this API in a web application right away.
I’ve never seen a way to create an API this fast, ever in my entire career.
What about security?
This is also where I was very pleasantly surprised to find very intuitive controls available in the UI. I was able to enable
Rate-limits
Node limits
Depth limits
Timeout and
Batch-request limits
for a global user as well as a user that I created for testing. This is very useful as implementing this was actually quite a messy affair, when I did this on my own, using a flask-backend in python a few years ago.
Schema-introspection can also be limited to roles and that is a very welcome feature that is available right in the UI.
Making a REST endpoint
It was comically easy to make a REST endpoint on top of my data. All of the methods of GET, POST, PUT, PATCH and DELETE are available to the user while creating the endpoint. This is a great feature for systems that want to leverage everything that Hasura offers, without having to switch to a GraphQL API.
Actions
Actions in Hasura are custom queries or mutations that are resolved via HTTP handlers. Actions can be used to carry out complex data validations, data enrichment from external sources or execute just about any custom business logic.
An action would end up executing custom logic that you can call via a webhook or a REST endpoint and basically work with any external system. Creation of an action is also very straightforward. You define your configuration and select how you want to handle the same. The execution allows for both synchronous and asynchronous calls which is excellent and ends up allowing for all sorts of custom implementations.
Remote Schemas
Remote schemas in Hasura are external GraphQL services which can be merged with Hasura to provide a unified GraphQL API. Think of it like automated schema stitching. All you need to do is build a GraphQL service and then provide its HTTP endpoint to Hasura. Your GraphQL service can be written in any language or framework.
Event triggers
An Event Trigger atomically captures events (insert, update, delete) on a specified table and then reliably calls a HTTP webhook to run some custom business logic.
Monitoring
Hasura's GraphQL Engine includes several built-in monitoring features and integrates with external monitoring tools to help users track, optimize, and troubleshoot their API performance. Here are the main monitoring features available in Hasura:
Health Check Endpoint: Hasura provides a
/healthz
endpoint to check the status of the Hasura server, which is useful for setting up basic health monitoring.Readiness Endpoint: The
/readiness
endpoint can be used to check if Hasura is ready to accept traffic, helpful for auto-scaling and load balancing setups.GraphQL Engine Logs: Hasura generates logs for requests, response times, and errors. These logs can be configured for various verbosity levels and can be directed to log aggregation tools for better insights.
Request/Response Metrics: Hasura provides metrics for query and mutation execution time, including metadata about each query, mutation, and subscription.
Real-time Analytics for Active Subscriptions: Hasura’s console allows you to monitor active subscriptions in real-time, showing which users are subscribed to which queries and for how long.
Hasura integrates with OpenTelemetry and Jaeger for distributed tracing, allowing users to monitor each stage of query execution. You can trace requests from the frontend through Hasura to the underlying database and identify slow-performing queries.
Hasura provides a native integration with Prometheus to export monitoring metrics, such as request latency, response status codes, and other performance indicators.
Hasura can also work with third-party observability tools such as Datadog, New Relic, and others by exporting logs and metrics. This flexibility enables more advanced monitoring setups for larger applications or specific enterprise needs.
Conclusion
All in all, Hasura is an excellent project and I can’t wait to implement my upcoming projects with it. With a generous-free tier on Hasura cloud, I want to start hosting some of my APIs and start implementing some web dev projects using the same. Also, I am going to going to self-host Hasura engine and see how I can leverage it with an existing infrastructure on my personal cloud subscription.
If you’re interested in reading about my implementations or similar topics in the engineering space, feel free to subscribe to my blog for free using the button below.
This was a long post and if you’ve stuck around, I’d like to thank you for reading. And I hope you have a wonderful day!
Resources
Want to experiment with how GraphQL API works? Here’s a handy link for you to experiment with queries in GraphQL.