Hasura
Hasura is an open-source engine that auto-generates a GraphQL backend on top of a Postgres database. Paired with Sequin, you can easily query across all your data in one place.
With Hasura and Sequin, you can read data from and write data to Salesforce, HubSpot, and Airtable using GraphQL.
This guide will walk you through setting up Sequin, connecting Hasura Cloud, configuring your schema, and querying your data.
Setup Sequin
Step 1: Login to Sequin and select the source you want to sync. If you need more details, follow the step-by-step setup guide for your source.
Step 2: Once you’ve connected your source, select which objects and properties you would like to sync to your database. You can always change this later.
Step 3: Connect your database to Sequin. Click the Connect button in the destination section of the Sequin dashboard. Then, enter your database connection details and set the name of the schema you want to sync your data to. If you need more detail, read our guide on connecting a database.
Step 5: Click Create to start your sync.
Sequin will now create a new schema in your database and start syncing your data to it. You can monitor the progress of your sync in the Sequin dashboard. Within a minute or two, you’ll see your tables and data appear in your database. You can now setup Hasura.
Setup Hasura
Step 1: Create a new Hasura Cloud project. If you don’t have one already, you can sign up for a free account.
Step 2: Connect your database to Hasura. You can do this by selecting Postgres and then clicking the Connect Existing Database button in the Hasura dashboard. Enter your database connection details and click Connect Database. If you need more details, read the Hasura docs.
Step 3: Once Hasura connects to your database, it will appear in the Data Manager. Next, you can configure Hasura to track the tables in your database. Select your database and the schema containing the tables you want to track. Then, click the Track button next to each table you want to query via Hasura. Do this for both your public
schema and your Sequin schema.
_sync
.Configure relationships
Hasura uses relationships to join tables together when executing GraphQL queries. Because your Sequin schema doesn’t contain foreign keys, you’ll manually configure relationships in the Hasura dashboard or using the Hasura CLI.
Step 1: To configure relationships in the Hasura dashboard, select the table you want to define a relationship for in the Data Manager, click the Relationships tab, and then click the Add Relationship button.
Step 2: Give the relationship a name (e.g., deal_associations
), select the table you want to join to, and then select the columns you want to join on. Then click the Create Relationship button:
Depending on which source you are syncing, you’ll need to define relationships differently. Below are the primary relationship types you’ll need to define:
Direct relationships
For sources like Salesforce and Stripe, you’ll often create a relationship between two tables by simply mapping the primary key of one table to the foreign key of the other table. For example, if you want to join the account
table to the opportunity
table, you would map the account_id
column in the opportunity
table to the id
column in the account
table.
To give you ultimate querying options in GraphQL, you’ll create a relationship on each side of this join:
- On the
account
table you’ll create an object relationship betweenaccount.id
andopportunity.account_id
. - On the
opportunity
table you’ll create an array relationship between opportunityopportunity.account_id
andaccount.id
.
Join table relationships
For sources like HubSpot, you’ll often need to create a relationship between two tables using an associations
join table. For example, if you want to join the deal
table to the contact
table, you’ll sync a join table called associations_contact_deal
that contains the relationship between deal_id
and contact_id
. Then, you would map the deal_id
column in the associations_contact_deal
join table to the id
column in the deal
table and the contact_id
column in the associations_contact_deal
table to the id
column in the contact
table.
In this scenario, you’ll create a total of four relationships:
- On the
associations_contact_deal
table you’ll create an object relationship betweenassociations_contact_deal.contact_id
andcontact.id
. You can call thisassociations_contact
. - You’ll then create the other end of the first relationship. On the
contact
table you’ll create an object relationship betweenassociations_contact_deal.contact_id
andcontact.id
. You can call thiscontact_associations
.
You’ll then repeat this process for the deal
table:
- On the
associations_contact_deal
table you’ll create a object relationship betweenassociations_contact_deal.deal_id
anddeal.id
. You can call thisassociations_deal
. - You’ll then create the other end of the relationship. On the
deal
table you’ll create an object relationship betweenassociations_contact_deal.deal_id
anddeal.id
. You can call thisdeal_associations
.
Views and generated columns
Hasura and Sequin support Postgres views and generated columns. You can use these features to create custom views and columns in your database to simplify querying patterns in GraphQL.
For instance, you can create a view called deals_and_contacts
that joins your HubSpot deal
table to your contact
table via the associations_contact_deal
join table. The view can contain just the columns you need:
In Hasura, you can then track this view to include it in your GraphQL schema - simplifying the query pattern.
Alternatively, you may need to use a generated column to ensure joins in Hasura function correctly. For example, let’s say you want to join your public.users
table with your salesforce.contact
table on the users.id
and contact.user_id
columns. However, the users.id
column is type int4
and the contact.user_id
column is type varchar
. You can create a generated column on the contact
table that casts the user_id
column to int4
in a new column called _user_id
:
In Hasura, you’ll then define a relationship between the users
table and the contact
table using the _user_id
column.
Build
Sequin and Hasura are now configured to work together. You can now use all the power of Hasura to query your database using GraphQL. A query to return the the state of a deal for one of your users might look like this:
From here, you can use Hasura’s event triggers, actions, and authentication to rapidly build your application.