Boolean Expressions
Introduction
Hasura provides powerful tools to control filtering and selecting data. Boolean expression types let you control which filters are available for a model or command. They can be used to configure filters on models, such as in the filtering section, or as the types of arguments to commands or models.
How Boolean expressions work
Lifecycle
There are two types of boolean expressions:
| Type | Description |
|---|---|
| Scalar | This specifies how a user able to compare a scalar field. |
| Object | This specifies how fields of a type can be filtered. |
Regardless of the type, boolean expressions are used to define how a user is able to filter data and are defined in your metadata.
Examples
Scalar
This specifies how a user is able to compare a scalar field. For instance, you might want to say that a user can only
check if a String type is equals to another, or whether it is null or not. You can do that with the following
metadata:
kind: BooleanExpressionType
version: v1
definition:
name: String_comparison_exp_with_eq_and_is_null
operand:
scalar:
type: String
comparisonOperators:
- name: equals
argumentType: String!
dataConnectorOperatorMapping:
- dataConnectorName: postgres
dataConnectorScalarType: varchar
operatorMapping:
equals: _eq
logicalOperators:
enable: true
isNull:
enable: true
graphql:
typeName: String_comparison_exp_with_eq_and_is_null
Note the dataConnectorOperatorMapping. This allows us to define what these operators mean in zero or more data
connectors. Here, we want our equals operator to use Postgres's _eq operator.
This would allow us to write filters like:
{ "first_name": { "_is_null": true } }
{ "last_name": { "equals": "Bruce" } }
Object
An object BooleanExpressionType is used to define how fields of a type can be filtered. Note that nothing here talks
about specific data connectors - instead you can specify which BooleanExpressionType is used to filter each field or
relationship, and then defer the mappings of individual scalar types to those BooleanExpressionTypes.
kind: BooleanExpressionType
version: v2
definition:
name: Album_bool_exp
operand:
object:
type: Album
comparableFields:
- fieldName: AlbumId
booleanExpressionType: Int_comparison_exp
- fieldName: ArtistId
booleanExpressionType: Int_comparison_exp_with_is_null
- fieldName: Address
booleanExpressionType: Address_bool_exp
comparableRelationships:
- relationshipName: artist
booleanExpressionType: Artist_bool_exp
logicalOperators:
enable: true
isNull:
enable: true
graphql:
typeName: Album_bool_exp
Note here that we can specify different comparison operators for AlbumId and ArtistId by using different
BooleanExpressionTypes for them. We are also able to define filtering on nested objects such as Address.
The above would let us write filters on Album types like:
{ "album": { "AlbumId": { "equals": 100 } } }
{ "album": { "Address": { "postcode": { "like": "N1" } } } }
{ "album": { "artist": { "name": { "equals": "Madonna" } } } }
Metadata structure
BooleanExpressionType
Definition of a type representing a boolean expression on an OpenDD type.
| Key | Value | Required | Description |
|---|---|---|---|
kind | BooleanExpressionType | true | |
version | v1 | true | |
definition | BooleanExpressionTypeV1 | true | Definition of a type representing a boolean expression on an OpenDD object type. |
Example:
kind: BooleanExpressionType
version: v1
definition:
name: Album_bool_exp
operand:
object:
type: Album
comparableFields:
- fieldName: AlbumId
booleanExpressionType: pg_Int_Comparison_exp
- fieldName: ArtistId
booleanExpressionType: pg_Int_Comparison_exp_with_is_null
- fieldName: Address
booleanExpressionType: Address_bool_exp
comparableRelationships:
- relationshipName: artist
booleanExpressionType: Artist_bool_exp
logicalOperators:
enable: true
isNull:
enable: true
graphql:
typeName: App_Album_bool_exp