Postgres: Customise 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 the auto-generated table and column field names exposed over the GraphQL API.

Supported from

This feature is supported in versions v1.0.0-beta.8 and later.

Expose columns with a different name in the GraphQL 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.

Customise GraphQL field name

You can customize auto-generated field names in the tables.yaml file inside the metadata directory:

 - table:
     schema: public
     name: author
   configuration:
     custom_column_names:
       addr: address

Apply the metadata by running:

hasura metadata apply

A custom field name can be set for a column via the following 2 methods:

  1. passing a Table Config with the CustomColumnNames to the track_table v2 API while tracking a table:

    POST /v1/query HTTP/1.1
    Content-Type: application/json
    X-Hasura-Role: admin
    
    {
      "type": "track_table",
      "version": 2,
      "args": {
        "table": "author",
        "configuration": {
          "custom_column_names": {
            "addr": "address"
          }
        }
      }
    }
    
  2. using the set_table_custom_fields (deprecated) API to set the CustomColumnNames:

    POST /v1/query HTTP/1.1
    Content-Type: application/json
    X-Hasura-Role: admin
    
    {
      "type": "set_table_custom_fields",
      "version": 2,
      "args": {
        "table": "author",
        "custom_column_names": {
          "addr": "address"
        }
      }
    }
    

Expose table root fields with a different name in the GraphQL 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.

Customise GraphQL root field

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:

  1. passing a Table Config with the Custom Root Fields names to the track_table v2 API while tracking a table:

    POST /v1/query HTTP/1.1
    Content-Type: application/json
    X-Hasura-Role: admin
    
    {
      "type": "track_table",
      "version": 2,
      "args": {
        "table": "author",
        "configuration": {
          "custom_root_fields": {
            "select": "authors",
            "select_by_pk": "author"
          }
        }
      }
    }
    
  2. using the set_table_custom_fields (deprecated) API to set the Custom Root Fields names

    POST /v1/query HTTP/1.1
    Content-Type: application/json
    X-Hasura-Role: admin
    
    {
      "type": "set_table_custom_fields",
      "version": 2,
      "args": {
        "table": "author",
        "custom_root_fields": {
            "select": "authors",
            "select_by_pk": "author"
        }
      }
    }