Get GraphQL API on Snowflake with Hasura – Sample App
Hasura just launched GA support for Snowflake, and you can read the announcement here.
Hasura provides a high-performance, instant GraphQL API layer over Snowflake. By integrating GraphQL with Snowflake, you can leverage both technologies’ strengths to create efficient, flexible, and responsive APIs for data-driven applications.
Today, we will create a sample app using Weather data available on the Snowflake marketplace, that utilizes user-defined functions and provides instant GraphQL API in just a few steps.
We would use a simple architecture for this application to expose Snowflake data through Hasura. Hasura becomes the API layer through which we expose the data to our clients.
Using GraphQL APIs, we can also showcase charts for analytics purposes.
Setup Snowflake with Hasura
0. Setup Weather Data on Snowflake
You can add the sample data needed for the application from Snowflake’s marketplace for free. Please use this link to follow the instructions.
1. Signup for Hasura Cloud
Sign up for a free Hasura Cloud account and spin up a new project.
2. Connect Your Snowflake to Hasura:
Follow our Snowflake database guide to set up a Snowflake Data Source for Hasura. Enter your Snowflake JDBC connection details in Hasura’s console as shown below.
3. Track tables from Snowflake
Once connected, you can see your Snowflake tables in Hasura. You can select all the tables you want to query using Hasura and click on Track Selected.
4. Query your Snowflake API
Once you connect and track tables, Hasura instantly gives you a GraphQL API on top of Snowflake that you can immediately use to start querying the data.
At the bottom-right of the GraphQL API tab, you'll see the quick response times Hasura's high-performance GraphQL API provides. In this example, computing the average temperature over a few thousand records takes about ~500 milliseconds.
Note: These queries are running directly on Snowflake without replicating the data in a different datastore.
5. Cache your Snowflake API
You can use the @cached directive to your query to decrease your response time. The default TTL is 60 seconds, reducing latency by about ~100 milliseconds.
Caching is useful when querying Snowflake, as latencies could be a bottleneck, primarily when serving customers on web/mobile applications. If the same data is frequently fetched, caching can improve latencies and reduce the load on data warehouse resources.
6. Tracking User-Defined Functions
Hasura now supports user-defined functions (UDF) in Snowflake. To add a UDF, you need to create it from Snowflake’s worksheet, you can find the details here.
In our example, we create a UDF which returns avg temperature grouped by week.
We also need to create a view to define the return type in Hasura, for that we execute the following query.
Now we can track the UDF in Hasura’s console.
After clicking track, we need to select the return type, and select the view you created above.
Post that we can query the UDF, just like any normal table and we get the same great performance we expect from Hasura.
We can also cache the results and get ~100ms response time as we do with normal tables.
7. Query your Snowflake API with React
Now we can expose these data as charts, to help with that we can make use of graphql2chartjs to reshape your GraphQL data per the ChartJS API. This makes it easy to query a GraphQL API and render the output as a ChartJS chart.
You can find the sample code to setup this component here.
As we can observe, Hasura makes it easy to expose our Snowflake data using GraphQL/REST APIs in a few mins.
Hasura, unlike other solutions, doesn’t require you to maintain a copy of your data, and it generates SQL queries based on your request. Hasura also provides a way to cache and rate limit at the same time while offering authentication and authorization options to safeguard your data.
Overall, the combination of Hasura Cloud and Snowflake provides an effective way to build scalable, secure, and performant GraphQL APIs on top of a powerful data warehouse.
Check out this blog on Hasura's RBAC systems helping secure Snowflake data to understand how you can ensure that your APIs are secure and compliant while also providing rich, flexible access to data.
Try out our Snowflake support on Hasura Cloud.