This tutorial shows you how to create a GraphQL application using Hasura Cloud and CockroachDB Dedicated.
The Hasura GraphQL Engine creates GraphQL schemas and resolvers based on the tables and views in your CockroachDB cluster, allowing you to submit GraphQL queries to access and manipulate your data.
Hasura GraphQL Engine v2.16 and later supports CockroachDB v22.2 and later.
Overview
This tutorial will show you how to configure a Hasura project with a CockroachDB data source, allow connections to your CockroachDB cluster from Hasura Cloud, and create a table that is then used with Hasura GraphQL Engine to retrieve and modify data.
Before you begin
Before you start this tutorial, you need:
- An existing CockroachDB Cloud cluster, running CockroachDB v22.2 or later.
- A Hasura Cloud account.
Configure your cluster
- In the CockroachDB Cloud console, select your cluster and click Connect.
- Select the SQL user you want to use for the Hasura Cloud connection under Select SQL user.
- If you have not set up a SQL user for this cluster, follow the instructions to create a new SQL user. Be sure to copy and save the password to a secure location.
- Select General connection string.
- Expand Download CA Cert (Required only once)
- Select your operating system under Select operating system and then in a terminal run the command to download the certificate.
- Copy the connection string under General connection string and paste it in a secure location. You will use this connection string later to configure Hasura GraphQL Engine with your cluster.
- In the CockroachDB Cloud console, select your cluster and click Connect.
- If you have not set up IP Allowlists under Network Security, follow the instructions to add connections to your cluster from your machine.
- Select the SQL user you want to use for the Hasura Cloud connection under Select SQL user. If you have not set up a SQL user for this cluster, follow the instructions to create a new SQL user. Be sure to copy and save the password to a secure location.
- Select General connection String.
- Copy the connection string under General connection string and paste it in a secure location. You will use this connection string later to configure Hasura GraphQL Engine with your cluster.
Create a new project in Hasura Cloud
In the Hasura Cloud console, select Projects, then click New Project.
In the Create Project panel select the cloud infrastructure provider and region.
The cloud infrastructure provider and region should match your cluster. For example, if you created a CockroachDB Dedicated cluster in GCP's
us-east1
region, choose a GCP region closest tous-east1
.Click Create Free Project.
Add environment variables to your project
You will store your cluster root certificate and connection string in your Hasura project's environment variables. Storing this connection information in environment variables is considered a best practice.
Create a SSL_ROOT_CERT
environment variable for your cluster's CA cert.
- Select Env vars in your project settings, and click New Env Var.
- Under Key type
SSL_ROOT_CERT
, then press Enter. Copy the contents of your cluster's CA certificate file you downloaded earlier.
For example, on Mac you can copy the contents of the CA certificate in a terminal using
pbcopy
:pbcopy < $HOME/.postgresql/root.crt
Paste the contents of the CA certificate file under Value.
Click Add.
- Select Env vars in your project settings, and click New Env Var.
- Under Key type
SSL_ROOT_CERT
, then press Enter. Copy the contents of your CockroachDB Dedicated cluster's CA certificate file you downloaded earlier.
For example, on Mac you can copy the contents of the CA certificate in a terminal using
pbcopy
:pbcopy < $HOME/Library/CockroachCloud/certs/fake-puppy-ca.crt
Paste the contents of the CA certificate file under Value.
Click Add.
Create a CRDB_URL
environment variable to store the connection string.
- Click New Env Var.
- Under Key type
CRDB_URL
, then press Enter. - Paste the connection string for your cluster you copied earlier. Add your SQL user password if necessary to the connection string.
Remove the
sslmode
andsslrootcert
query parameters from the end of the connection string. You will add the SSL settings later.For example, if your connection string in the CockroachDB Cloud console is:
postgresql://maxroach:NotAGoodPassword@fake-puppy-595.g95.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full
You would modify the connection string to:
postgresql://maxroach:NotAGoodPassword@fake-puppy-595.g95.cockroachlabs.cloud:26257/defaultdb
Click Add.
Create a Data Source in Hasura Cloud
- In the overview page in Hasura Cloud, select Projects, find the project you created earlier, and click Launch Console.
- Click Data in the console header to open the Data Manager.
- Click Connect Database.
- In the Connect Existing Database tab configure the connection to your cluster.
- Under Database Display Name type a name for your connection. Your cluster name is a good option. For example,
fake-puppy
. - Set Data Source Driver to CockroachDB.
- Select Environment Variable, then enter
CRDB_URL
in the Environment Variable input box.
- Under Database Display Name type a name for your connection. Your cluster name is a good option. For example,
- Expand Connection Settings, then expand SSL Certificates Settings and set SSL Mode to
verify-full
. - Enter
SSL_ROOT_CERT
under SSL Root Certificate, the environment variable you configured earlier. - Click Connect Database.
Add the Hasura Cloud network to your cluster allowlist
Your CockroachDB Dedicated cluster needs to be configured to allow incoming client connections from Hasura Cloud.
In the Hasura Cloud overview page select Projects, then click the Config icon for your project.
In the General section, copy the IP address listed under Hasura Cloud IP.
In the CockroachDB Cloud console select your cluster, click Networking, then Add Network.
In the Add Network dialog, set Network name to
Hasura Cloud
.Under Network, select New Network. Paste the IP address of your Hasura Cloud instance you copied earlier.
Check CockroachDB Client to access the databases, then click Apply.
Create a table in your CockroachDB cluster
Connect to your cluster using the SQL client.
cockroach sql --url "{connection string}"
Where
{connection string}
is the connection string for your cluster you copied earlier.In the SQL client, create an
accounts
table.CREATE TABLE IF NOT EXISTS accounts ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), balance INT8 );
Insert some data into the
accounts
table.UPSERT INTO accounts (balance) VALUES (1000), (250);
This will create two accounts, one with $1000 and another with $250.
Track the accounts
table in the Hasura Cloud Data Manager
- In the Hasura Cloud Data Manager click
public
under your connection name. - Click the Track All button.
- Click Yes when prompted to expose the table to the GraphQL API.
Create a GraphQL query in the Hasura Cloud API Explorer
In the Hasura Cloud Explorer, you will see the accounts
table. You can expand accounts
to construct a GraphQL query against the accounts
table.
For example, to create a query that finds all accounts with balances greater than or equal to $500:
- Enter
GetLargeAccounts
as the query name in the Explorer. - Expand
accounts
in the Explorer. - Check
balance
andid
, then expandwhere
, thenbalance
. - Check
_gte
to create a "greater than or equal to" where clause forbalance
, then enter500
.
This creates the following GraphQL query:
query GetLargeAccounts {
accounts(where: {balance: {_gte: "500"}}) {
balance
id
}
}
Click the Execute Query button to run the GetLargeAccounts
GraphQL query. The returned data should look like the following:
{
"data": {
"accounts": [
{
"balance": 1000,
"id": "2ed41abf-e831-42df-aff4-f88a6f2b0afc"
}
]
}
}
Next steps
Now that you have connected your cluster to Hasura GraphQL Engine, you can use the Explorer to create GraphQL queries to access and modify data in your cluster's tables. You can then use these queries in your application. For example, you can create Hasura Actions that use these queries. See the Hasura documentation for detailed information on integrating and deploying Hasura in your applications.