Postgres: Customize Auto-Generated Field Names
Introduction
Hasura auto-generates GraphQL field names based on your database table and column names. If you'd like to change the defaults, it is possible to override and rename the auto-generated table and column field names exposed over the GraphQL API.
This feature is supported in versions v1.0.0-beta.8
and later.
Expose columns with a different name in the GraphQL API
- Console
- CLI
- API
Head to the Data -> [table-name] -> Modify
. On the relevant field, click Edit
and change the GraphQL field name to a
name of your choice.
You can customize auto-generated field names in the tables.yaml
file inside the metadata
directory:
- table:
schema: public
name: author
configuration:
column_config:
addr:
custom_name: address
Apply the Metadata by running:
hasura metadata apply
A custom field name can be set for a column via the following 2 methods:
passing a Table Config with the ColumnConfig to the pg_track_table API while tracking a table:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "pg_track_table",
"args": {
"source": "<db_name>",
"table": "author",
"configuration": {
"column_config": {
"addr": {
"custom_name": "address"
}
}
}
}
}using the pg_set_table_customization API to set the ColumnConfig:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "pg_set_table_customization",
"args": {
"source": "<db_name>",
"table": "author",
"column_config": {
"addr": {
"custom_name": "address"
}
}
}
}
Expose table root fields with a different name in the GraphQL API
- Console
- CLI
- API
Head to the Data -> [table-name] -> Modify
. Click the Edit
button in the Custom GraphQL Root Fields
section and
define names over which you'd like to expose the table root fields.
You can expose table root fields with a different name in the GraphQL API in the tables.yaml
file inside the
metadata
directory:
- table:
schema: public
name: author
configuration:
custom_root_fields:
select_by_pk: author
select: authors
After that, apply the Metadata by running:
hasura metadata apply
A custom field name can be set for a table root field via the following 2 methods:
passing a Table Config with the Custom Root Fields names to the pg_track_table API while tracking a table:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "pg_track_table",
"args": {
"source": "<db_name>",
"table": "author",
"configuration": {
"custom_root_fields": {
"select": "authors",
"select_by_pk": "author"
}
}
}
}using the pg_set_table_customization Metadata API to set the Custom Root Fields names
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "pg_set_table_customization",
"args": {
"source": "<db_name>",
"table": "author",
"custom_root_fields": {
"select": "authors",
"select_by_pk": "author"
}
}
}