DEV Community

Cover image for How to Build a Supergraph using Snowflake, Neon PostgreSQL, and Hasura in Five Steps
Praveen Durairaju
Praveen Durairaju

Posted on • Originally published at hasura.io

How to Build a Supergraph using Snowflake, Neon PostgreSQL, and Hasura in Five Steps

By combining Hasura Cloud, Snowflake DB, and PostgreSQL on Neon, you can create a powerful supergraph backend to handle complex data tasks such as joining data across multiple data sources and filtering data on model relations. It's a supergraph framework with modular, unified layers that is continuously evolving and deployed by many companies worldwide.

Supergraph with Hasura

This tutorial will guide you through the creation of an application backend that leverages the powerful features of Snowflake and PostgreSQL databases. I will show how to integrate tables from both Snowflake and PostgreSQL into your supergraphs, allowing you to perform sophisticated, nested queries that connect models from diverse databases. This approach not only increases your backend's capabilities but also streamlines the data handling process for more complex data relationship management across different sources.

1. Setting up Hasura Cloud

Start with Hasura Cloud to quickly create GraphQL APIs.

  • Sign up for a free Hasura Cloud account.
  • Create a new project and connect it to your version control system (e.g., GitHub).
  • Define your data model using Hasura's metadata.
  • Run some exciting queries

Run GraphQL queries

2. Integrating Snowflake DB

Snowflake is a cloud-based data warehousing platform that seamlessly integrates with Hasura Cloud. To integrate Snowflake:

  • Obtain your Snowflake credentials (account, username, password).

Connect to Snowflake

  • In Hasura Cloud, navigate to the "Data" tab and add a new data source.
  • Select "Snowflake" and input your credentials.
  • “Import all tables” to add all the tables as models to the supergraph

Import snowflake tables to Hasura

3. Incorporate PostgreSQL on Neon

Neon is a managed platform for PostgreSQL databases. To incorporate PostgreSQL on Neon into your supergraph backend:

Sign up for Neon and create a new PostgreSQL database instance.

Neon PostgreSQL

  • Obtain the connection details for your Neon PostgreSQL database.
  • In Hasura Cloud, add another data source, but this time, select "Neon Database" and authorize the Neon database connection details.

Connect to Neon

  • Add the models and relationships you want to track in the data tab.

4. Executing individual queries

Now that you have Snowflake and PostgreSQL on Neon integrated into your Hasura Cloud project, you can execute queries individually.

Querying Snowflake

To query data from Snowflake, you can use Hasura's GraphQL API:

query MyQuery {
  SNOWFLAKETABLE {
    COLUMN1
    COLUMN2
  }
}
Enter fullscreen mode Exit fullscreen mode

Replace snowflakeTable, column1, and column2 with your Snowflake table and columns.

Snowflake GraphQL query

Querying PostgreSQL on Neon

Similarly, to query data from your Neon PostgreSQL database:

query MyQuery {
  neontable {
    column1
    column2
    column3
  }
}
Enter fullscreen mode Exit fullscreen mode

Replace neonTable, column1, and column2 with your Neon table and columns.

Postgres GraphQL Query

5. Nested queries for complex data retrieval

GraphQL has the ability to handle complex data retrieval through nested queries. Here’s an example:

query {
  getUser(userId: 123) {
    username
    email
    posts {
      title
      content
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In this query:

  • We retrieve user details by their ID from either Snowflake or PostgreSQL.
  • Then, we fetch the user's posts with their titles and content.

By defining appropriate relationships and foreign keys in Hasura's metadata for both Snowflake and PostgreSQL, you can perform nested queries like the one above, aggregating data from multiple sources into a single response.

Conclusion

Continue to explore and experiment with Hasura Cloud, Snowflake DB, and PostgreSQL on Neon. Discover how to efficiently integrate multiple data sources and execute both individual and nested queries. This will increase performance and scalability across a wide range of applications.

Top comments (0)