/

hasura-header-illustration

Announcing General Availability of BigQuery with Hasura

Introduction

We recently announced the general availability of BigQuery in Hasura! Now you can connect a BigQuery database to your Hasura application to consume data.

This article teaches you how to:

  • configure BigQuery with Hasura
  • connect BigQuery database to Hasura
  • set up relationships between BigQuery tables
  • use Hasura permissions to perform data validation

Supported versions: Hasura GraphQL engine v2.0.0-alpha.1 onwards.

Configure BigQuery with Hasura

To connect a BigQuery database to Hasura, you need a "service account key" file. That file contains the credentials required by Hasura to connect to the database.

Navigate to the project's settings, create the BIGQUERY_SERVICE_ACCOUNT environment variable and set it to the content of the "service account key" file.

Set env variable in Hasura project settings

If you need help obtaining your service account key, check this section in the documentation.

Connect BigQuery Database to Hasura

Navigate to the Connect Existing Database page in the project console to set up the database:

  1. Choose a name for the database
  2. Choose Big Query for the "Data Source Drive"
  3. Select the Environment Variable option
  4. Enter the newly created environment variable BIGQUERY_SERVICE_ACCOUNT
  5. Enter your GCP project id
  6. Enter the dataset (or datasets)

Connect BigQuery database in Hasura

After connecting the database, you should be able to track the tables from the specified dataset. Click on "Track All".

Track database tables in Hasura

The database is now ready! You can perform GraphQL queries on your data. For a more comprehensive guide on getting started with BigQuery on Hasura Cloud, check the documentation.

Let's test the integration with the following query:

query authors {
  publication_authors {
    id
    name
  }
}

Running the query returns a list of all authors from the database, as shown in the figure below.

Hasura BigQuery query

You can query authors and articles individually, but there is no relationship between them. For example, you cannot retrieve all authors and their articles.

Set up relationships between BigQuery tables

Nested object queries refer to fetching data for a type and data from a nested or related type.

To make nested object queries, you need to set up relationships between the two tables - authors and articles.

Create array relationship

Navigate to the Relationships page in the authors table and click on the "Configure" button.

Set up the authors relationship in Hasura

That opens a new section where you can configure the relationship. Configure it as follows:

  1. Choose Array Relationship (one-to-many) for the "Relationship Type" field
  2. Name your relationship - e.g. articles
  3. Leave the "Reference Schema" field as it is
  4. Choose articles for the "Reference Table" field
  5. Select id for the "From" field and author_id for the "To" field
  6. Save the relationship

Configure the authors relationship in Hasura

Let's test the relationship by running the following query:

query author_articles {
  publication_authors {
    id
    name
    articles {
      id
      title
      published_on
      body
    }
  }
}

Running the query returns all the authors and their articles, as illustrated in the image below.

Authors articles query

Create object relationship

The next step involves creating an object relationship between the articles and authors tables. Configure the relationship as follows:

  1. Choose Object Relationship (one-to-one) for the "Relationship Type" field
  2. Name your relationship - e.g. authors
  3. Leave the "Reference Schema" field as it is
  4. Choose authors for the "Reference Table" field
  5. Select author_id for the "From" field and id for the "To" field
  6. Save the relationship

Set up rticles-author relationship

Let's test the relationship by fetching the articles and their authors.

query articles_author {
  publication_articles {
    id
    title
    published_on
    body
    author {
      id
      name
    }
  }
}

The image shows the list of articles and the id and name of each article's author.

Articles author Query

By this point, you set both object and array relationships. These relationships enabled you to perform nested queries. Check the documentation on BigQuery: nested object queries for more information.

Data validation with BigQuery

Even though BigQuery does not support constraints natively, you can use Hasura permissions to perform data validation.

With this example application, let's consider the following scenarios:

  • authors should only be able to access their details
  • authors should only be able to access their articles

Navigate to the "Permissions" tab in the "authors" table and add the author role.

Set the permissions for the authors table

Click on the "X" icon to open the configuration section and add the following custom check:

{
  "id": {
    "_eq": "X-Hasura-User-Id"
  }
}

Then toggle all the columns and save the permissions.

Set up the authors table permissions for the author role

Before performing any queries, you need to set the X-Hasura-Role and X-Hasura-User-Id headers to the author role and the author's id, respectively.

Once the headers are in place, you can fetch the author's details.

query getAuthor {
  publication_authors {
    id
    name
  }
}

The x-hasura-user-id header is set to "1", meaning the query returns the author's details with the user_id of "1".

GraphQL query to retrieve a specific author

Similarly, configure the author role for the "articles" table as follows:

{
  "author_id": {
    "_eq": "X-Hasura-User-Id"
  }
}

Then toggle all the columns and save the permissions.

Set up the articles table permissions for the author role

Let's test the permissions by running the following query in the GraphiQL editor:

query getArticles {
  publication_articles {
    id
    title
    published_on
    body
    author {
      id
      name
    }
  }
}

It should return the author's articles specified in the x-hasura-user-id header. The image illustrates that it works as expected.

GraphQL query to retrieve articles and authors

If you remove the x-hasura-user-id header, Hasura returns an empty array.

This is how you perform data validations with the Hasura permission system. If you want to read more, check the documentation on BigQuery: Data Validation.

Next steps

There is also a video that covers the topic. You can watch it here:

We would love to hear about your use cases with BigQuery. Let us know in the comments!

Note: Check the documentation for information on the features supported.

29 Sep, 2022

5 MIN READ

Share
Blog
29 Sep, 2022
Email
Subscribe to stay up-to-date on all things Hasura. One newsletter, once a month.
Loading...
v3-pattern
Accelerate development and data access with radically reduced complexity.