Skip to main content
Version: v2.x

BigQuery: Data Validations

Introduction

Many times, we need to perform validations of input data before selecting, inserting or updating objects.

BigQuery does not natively support constraints, primary or foreign keys.

However, if you would like the validation logic to be at the GraphQL API layer, Hasura permissions can be used to add your validation.

These solutions are explained in some more detail below.

Using Hasura permissions

If the validation logic can be expressed declaratively using static values and data from the database, then you can use row level permissions to perform the validations. (Read more about Authorization).

Example 1: Validate that the editor role can only select articles with empty titles.

Suppose, we have the following tables in our schema:

authors (id int, name text)
articles (id int, title text, author_id int, body text)

Now, we can create a role editor and add a select validation rule as follows:

If we set the X-Hasura-Role to editor and try to select all articles, only those with empty string titles will be returned:

GraphiQL
Query Variables
Request Headers

Example 2: Validate that author roles can only select an article if the author_id matches X-Hasura-User-Id

Suppose, we have 2 tables:

authors (id int, name text)
articles (id int, title text, author_id int, body text)

Also, suppose there is an object relationship articles.author defined as:

articles.author_id -> authors.id

Now, we can create a role author and add an select validation rule as follows:

If we set the X-Hasura-Role and X-Hasura-User-Id headers, then try to select all articles, only those where the author_id matches the current X-Hasura-User-Id will be returned:

GraphiQL
Query Variables
Request Headers
Note

Permissions are scoped to a user's role. So, if a validation check needs to be global then you will have to define it for all roles which have insert/update permissions.