Lang
Blog

Hasura - An Open-Source GraphQL Engine

Build real-time and powerful GraphQL APIs in an instant.

ByPayal Mittal
April 24th . 5 min read
Hasura - An Open-Source GraphQL Engine

Hasura launched an open-source GraphQL engine that connects to various databases, serverless functions, and microservices and auto-generates a production-ready GraphQL backend.

The Hasura GraphQL-based engine lets you configure event triggers for the Postgres database within minutes and helps to build instant and real-time GraphQL applications backed by PostgreSQL. While designing the GraphQL engine, team Hasura subjected to fulfill two main objectives in the nutshell, i.e.

  • To make it compatible to run anywhere, including a local machine, Heroku free tier or on Google, Amazon, and Microsoft servers.
  • To extract maximum performance/dollar.

It can accept client-app requests and easily evolve with your needs by processing 1000 queries/second (approx.), consuming only 50MB of RAM storage. The Hasura GraphQL engine contains a cutting-edge Postgres ecosystem with your favorite extensions and scales up to 1 million active GraphQL subscriptions and live-queries.

This blog is about giving a quick overview of the Hasura engine, its architecture, features, and use-cases. To get a better understanding, you must read it till the end-

Features of Hasura GraphQL Engine

Hasura comes with a wide string of features, as given under-

  • Out-of-box Powerful Queries: Auto generating of a range of all possible queries, built-in filtering, bulk insert, delete, update, and insert mutations.
  • Dynamic access control system integrating with your auth: Creating field-level allow and deny rules, role-based schemas, and integration with existing auth system.
  • Trigger Webhooks on Database Events: Build asynchronous and reactive apps, work with live and existing databases, capturing & delivering of events.

hasura_1.gif

  • Adding to an Existing, Live PostgreSQL database: You can add Hasura to any existing Postgres database as-it-is by using Heroku, Kubernetes, or Docker and get an all-set GraphQL API almost instantly.
  • Realtime with GraphQL subscriptions & live-queries: Conversion of any GraphQL query to a live query by using subscriptions.
  • Restrict access in production: Allow-list for safe queries stored in the metadata of the engine, execute only those particular queries that are present in the list.
  • Merge Remote GraphQL Schemas: Seamless merging of remote schemas behind a single GraphQL API, using them for custom business logic, delegating to HTTP APIs, etc.
  • Query Performance Analysis: Identification and fixing of performance holdups in the console.
  • GraphQL Tight Integration
  • Pre-configured API Gateway
  • Admin UI & Schema Migrations

Getting Started with Hasura

Hasura helps you setup the GraphQL server with the lease of hassle and you can start building applications within a fraction of time.

In order to use the Hasura GraphQL engine, first, we need to run the engine having access to the PostgreSQL database and then use the console, that connects to the GraphQL engine, to build schema and run queries and mutations.

hasura_1.jpg

The Hasura GraphQL engine automatically generates a GraphQL schema in the Postgres database and process queries, mutations, and subscriptions.

Let’s see what actually the Hasura GraphQL engine does-

Schema Generation:

When one tracks a Postgres table/view in Hasura, the GraphQL engine generates GraphQL schema components and create a connection among them.

Tables: While tracking Postgres tables, it routinely generates the following schema components, -

  • A GraphQL type definition
  • A query and subscription field with where, order_by, limit and offset argument.
  • An insert mutation field with on_conflict argument to support upsert and bulk inserts
  • An update and delete mutation field with where argument to support majority updates and deletes.

Views: While tracking Postgres views, the Hasura GraphQL engine generates the same components as does for the table, except for mutation fields.

Relationships: While creating a relationship between table/view with another table/view, the Hasura GraphQL engine-

  • Fetches nesting objects and extend the table/view by adding a reference.
  • Augments order_byand whereclauses to filter and sort nested objects.

Metadata:

All the information about schema generation is been stored as ‘Metadata’ by the Hasura GraphQL engine in its catalog.

Whenever one performs certain actions on the console, the Hasura GraphQL engine records it in the hasura_metadata_schema which is a hdb_catalog schema in the Postgres database. For instance, when a table is being tracked, an entry as hdb_catalog.hdb_table table is created in the Postgres database and quite similarly there are more tables to track functions, event triggers, relationships, and remote schemas.

This metadata can be exported in JSON format as hasura_metadata.jsonfile as a representation of the state of Hasura.

GraphQL & Microservices with Hasura

Hasura provides a way of accelerating GraphQL API adoption and application developing productivity on your microservices & serverless functions. It can connect and configure relationships across databases, external SaaS APIs, “mid-tier” microservices, and event-driven business logic without any upstream changes and picturize a unified GraphQL API.

It takes in metadata configuration at runtime so that the developers can map API resources and upstream data-models to an interconnected graph.

hasura.gif

End-to-End GraphQL Architecture Using Hasura Actions

Actions are user-defined mutations that seamlessly integrate and migrate an event-driven business logic into your GraphQL APIs. Team Hasura thought about using these ‘Actions’ in event-driven patterns in which the ‘read-models’ and ‘write-models’ are kept separate from each other. This architecture is able to deal with increasing complexity.

Hasura also helps in creating custom GraphQL mutation specifications which are converted to events and further delivered to microservices & serverless functions. The Hasura GraphQL engine interacts with microservices under event-driven logic workflow and allows easier decoupling and fault-tolerance without compromising on scalability.

hasura_2.jpg

Benefits & Use Cases of Hasura Actions-

Hasura Actions have several benefits over backend workflow, such as-

  • Completely independent event handlers thus, no need to care about GraphQL types, schemas, and relationships.
  • Easier event-driven backends building without much operational and setup efforts Convenient GraphQL mutations
  • Switching between sync & async modes considering the best for API consumers without burdening the event handlers

Merging Remote Schemas in Hasura

The Hasura GraphQL Engine merges schemas from multiple remote GraphQL servers and stitches them together at a single endpoint in the form of a unified GraphQL API. All you have to do is to create a GraphQL service and then provide its endpoint to Hasura. The GraphQL services provide support to all languages/frameworks.

hasura_3.jpg

The directory comprises GraphQL backend boilerplates (regular as well as serverless) for custom GraphQL servers using various languages, deployment runtimes, and serverless platforms-

  • Node.js: AWS, Azure, Google
  • Python: Zeit
  • Rust

The remote schemas have several use cases, that include various supporting features for payments, customizing mutations, and fetching dissimilar data from distinct sources.

Due to faster query processing, easier setup of permissions and relationships, blazing-fast production, and great UX, it is gaining huge popularity among fortune 500 companies.

Reference-

https://hasura.io/

https://hasura.io/docs/

https://github.com/hasura/graphql-engine/

Share:
0
+0