DipDup framework
Search…
hasura
Deployment block
This is an optional section used by DipDup executor to automatically configure Hasura engine to track your tables.
1
hasura:
2
url: http://hasura:8080
3
admin_secret: ${HASURA_ADMIN_SECRET:-changeme}
Copied!
Under the hood DipDup generates Hasura metadata file out from your DB schema and accesses Hasura instance using admin API endpoint.
Metadata configuration is idempotent: each time you do dipdup run it queries the existing schema and do the merge if required.

Schema limitation

The current version of Hasura GraphQL Engine treats public and other schemas differently. Table schema.customer becomes schema_customer root field (or schemaCustomer if camel_case option is enabled in DipDup config). Table public.customer becomes customer field, without schema prefix. There's no way to remove this prefix for now. You can track related issue (opens new window)at Hasura's GitHub to know when the situation will change. Since 3.0.0-rc1 DipDup enforces public schema to avoid ambiguity and issues with the GenQL library. You can still use any schema name if Hasura integration is not enabled.

Authentication

DipDup sets READ only permissions for all tables and enables non-authorized access to the /graphql endpoint.

Limit number of rows

DipDup creates user role allowed to perform queries without authorization. Now you can limit the maximum number of rows such queries return and also disable aggregation queries automatically generated by Hasura:
1
hasura:
2
select_limit: 100
Copied!
Note that with limits enabled you have to use either offset or cursor-based pagination on the client side.

Disable aggregation queries

1
hasura:
2
allow_aggregations: False
Copied!

Convert field names to camel case

For those of you from JavaScript world, it may be more familiar to use camelCase for variable names instead of snake_case Hasura uses by default. DipDup now allows to convert all fields in metadata to this casing:
1
hasura:
2
camel_case: true
Copied!
Now this example query to hic et nunc demo indexer...
1
query MyQuery {
2
hic_et_nunc_token(limit: 1) {
3
id
4
creator_id
5
}
6
}
Copied!
...will become this one:
1
query MyQuery {
2
hicEtNuncToken(limit: 1) {
3
id
4
creatorId
5
}
6
}
Copied!
All fields auto generated by Hasura will be renamed accordingly: hic_et_nunc_token_by_pk to hicEtNuncTokenByPk, delete_hic_et_nunc_token to deleteHicEtNuncToken and so on. To return to defaults, set camel_case to False and run hasura configure again.

REST endpoints

One of the most anticipated features of Hasura 2.0 is the ability to expose arbitrary GraphQL queries as REST endpoints. By default, DipDup will generate GET and POST endpoints to fetch rows by primary key for all tables:
1
$ curl http://127.0.0.1:8080/api/rest/hicEtNuncHolder?address=tz1UBZUkXpKGhYsP5KtzDNqLLchwF4uHrGjw
2
{
3
"hicEtNuncHolderByPk": {
4
"address": "tz1UBZUkXpKGhYsP5KtzDNqLLchwF4uHrGjw"
5
}
6
}
Copied!
However, there's a limitation dictated by the way Hasura parses HTTP requests: only models with primary keys of basic types (int, string and so on) can be fetched with GET requests. An attempt to fetch model with BIGINT primary key will lead to error: Expected bigint for variable id got Number. A workaround to fetching any model is to send a POST request containing a JSON payload with a single key:
1
$ curl -d '{"id": 152}' http://127.0.0.1:8080/api/rest/hicEtNuncToken
2
{
3
"hicEtNuncTokenByPk": {
4
"creatorId": "tz1UBZUkXpKGhYsP5KtzDNqLLchwF4uHrGjw",
5
"id": 152,
6
"level": 1365242,
7
"supply": 1,
8
"timestamp": "2021-03-01T03:39:21+00:00"
9
}
10
}
Copied!
We hope to get rid of this limitation someday and will let you know as soon as it happens.
Now the interesting part. You can put any number of .graphql files into graphql directory in your project's root, and DipDup will create REST endpoints for each of those queries. Let's say we want to fetch not only a specific token, but also the number of all tokens minted by its creator:
1
query token_and_mint_count($id: bigint) {
2
hicEtNuncToken(where: {id: {_eq: $id}}) {
3
creator {
4
address
5
tokens_aggregate {
6
aggregate {
7
count
8
}
9
}
10
}
11
id
12
level
13
supply
14
timestamp
15
}
16
}
Copied!
Save this query as graphql/token_and_mint_count.graphql and run dipdup configure-hasura. Now this query is available via REST endpoint at http://127.0.0.1:8080/api/rest/token_and_mint_count.
You can also disable creation of REST endpoints in the config:
1
hasura:
2
rest: False
Copied!
Last modified 2mo ago