• 4 min read

Use GraphQL with Hasura and Azure Database for PostgreSQL

Azure Database for PostgreSQL provides a fully managed, enterprise-ready community PostgreSQL database as a service. The PostgreSQL Community edition helps you easily migrate existing apps to the cloud or develop cloud-native applications using languages and frameworks of your choice.

Azure Database for PostgreSQL provides a fully managed, enterprise-ready community PostgreSQL database as a service. The PostgreSQL community edition helps you easily migrate existing apps to the cloud or develop cloud-native applications, using languages and frameworks of your choice. The service offers industry leading innovations such as built-in high availability, backed with 99.99 percent SLA, without the need to set up replicas and enabling customers to save over two times the cost. The capability also allows customers to scale compute up or down in seconds, helping you easily adjust to changes in workload demands.

Additionally, built-in intelligent features such as Query Performance Insight and performance recommendations help customers further lower their total cost of ownership (TCO) by providing customized recommendations and insights to optimize the performance of their Postgres databases. These benefits coupled with unparalleled security and compliance, Microsoft Azure’s industry leading global reach, and Azure IP Advantage, empower customers to focus on their business and applications rather than the database.

As part of the broader Postgres community, our aim is to contribute to and partner with others in the community to bring new features to Azure Database for PostgreSQL users. You can now take advantage of the Hasura GraphQL Engine, a lightweight, high performance open-source product that can instantly provide a real time GraphQL API on a Postgres database. This post provides an overview of how to use GraphQL with Azure Database for PostgreSQL.

What is GraphQL?

GraphQL is a query language for APIs and a server-side runtime for executing database queries. The GraphQL spec is centered around a typed schema that is available to users of the API, which are mostly front-end developers, to make any CRUD queries on the exposed fields. It’s agnostic to the underlying database or source of data. One of GraphQL’s main benefits is that clients can specify exactly what they need from the server and receive that data in a predictable way. GraphQL provides a solution to common hurdles faced when using REST APIs, and it is currently being adopted widely to speed up product development cycles.

Hasura GraphQL Engine

The Hasura GraphQL Engine is a lightweight, high performance open-source product that gives you a real time GraphQL API on a Postgres database instantly. The engine comes with an admin UI to help you explore your GraphQL APIs and manage your database schema and data.

Hasura’s GraphQL Engine also allows you to write custom resolvers with schema-stitching and to integrate serverless functions or microservice APIs that get triggered on database events. With Hasura’s GraphQL, you can easily build 3factor apps. Learn more by reading about Hasura.

Using GraphQL with Azure Database for PostgreSQL

Please note, If you already have a database running on Azure and want to use GraphQL on that database, go directly to the “Working with GraphQL with an existing Azure Database for PostgreSQL” section below.

With the Hasura’s GraphQL one-click deploy, you can now get a real time GraphQL API on Azure with Azure Database for PostgreSQL server in under five minutes!

Get started by selecting Deploy to Azure below, which will open the Azure portal in preparation for using GraphQL with Azure Database for PostgreSQL. If you are prompted to log in to the Azure portal, enter your credentials to continue.

Deploy to Azure button

This deployment uses Azure Container Instances for deploying Hasura’s GraphQL and Azure Database for PostgreSQL for provisioning a managed Postgres instance.

Working with GraphQL with an existing Azure Database for PostgreSQL

If you already have a PostgreSQL database on Azure, you can connect Hasura’s GraphSQL to that database and have GraphQL APIs without affecting any other part of your application.

Get started by selecting the Deploy to Azure graphic below, which will open the Azure portal in preparation for working with GraphQL and an existing Azure Database for PostgreSQL database. If you are prompted to log in to the Azure portal, enter your credentials to continue.

Deploy to Azure button

This deployment uses Azure Container Instances for deploying Hasura and connects to an existing Azure Database for PostgreSQL instance.

What else can you do with Hasura’s GraphQL engine?

Explore the ready to use real-time API

Types and/or operators such as SQL for sorting, filtering, pagination, and aggregations are supported out-of-the-box. Read more about Hasura’s powerful syntax for queries and mutations.

Hasura also has built-in live-queries called subscriptions in GraphQL, for getting real-time updates to results of a query. No need to write any code for handling websocket connections!

Add authorization

Hasura’s granular, role-based permissions system lets you configure columns and row level access control rules for data that integrates with any third party. You can also integrate with your own custom authentication services using a JWT or webhook. You can learn more about adding authorization by visiting Hasura’s documentation, “Authentication / Access control.”

Add custom business logic

GraphQL Engine can be used as a gateway for custom business logic like with Remote GraphQL schemas. You can write your own GraphQL servers in your favorite language and expose them at a single endpoint. Hasura will take care of the schema stitching.

Trigger Azure Functions on database events

Hasura can trigger serverless Azure Functions or webhooks on database events like insert, update, or delete. They can be used to execute asynchronous business logic such as sending a “welcome email” to newly registered users. Read more about triggering serverless Azure Functions and webhooks on the Hasura website, or visit Hasura’s documentation, “Event triggers.”

Next steps

Get started and create your PostgreSQL servers today! Learn more about Azure Database for PostgreSQL in the overview documentation, “What is Azure Database for PostgreSQL.”

Please continue to provide UserVoice feedback on the features and functionality that you want to see next. If you need any help or have questions please check out the, “Azure Database for PostgreSQL documentation.”

For support and feedback related to Hasura, please use Discord. You can also follow Hasura’s product updates at @HasuraHQ.

Acknowledgements

Special thanks to the Hasura team for their contributions to this posting.