Connecting a Trino database

You can use a Trino database as a data source for your GraphQL API by extending the API with the @dbquery directive.

Any Query field in your GraphQL schema can be annotated with the @dbquery directive to connect to a database backend:

@dbquery (type: String!, query: String, table: String, configuration: String!)

For more information on the @dbquery directive, see Directives.

Use the arguments in the following sections when you want to connect to a Trino database as a data source for your GraphQL API.

type

Required. This argument specifies the type of database to query. Supported values are mysql, postgresql, mssql, presto, snowflake, and trino.

table

Optional. The value of this argument is the name of the database table to be queried. While this value is optional, one of either table or query must be specified.

Using the table argument is the equivalent of writing select * from [table]. The field names of the GraphQL type of the annotated field must match the column names of the underlying database table. Thus, if the table has a NAME column, it will populate the NAME field of the GraphQL type.

If the annotated field has arguments, they are used to construct the WHERE clause of the SQL query. For example, let's look at the following annotated field:

customerById (id: ID!): Customer
  @dbquery (
    type: "trino"
    table: "customers"
    configuration: "trino_config"
  )

The directive passes the following database query to the database specified by the Trino configuration argument (in this example, trino_config):

SELECT "custkey", "name", "address", "nationkey", "phone", "accbal", "mktsegment", "comment" FROM "customer" WHERE "custkey" = ?

where custkey, name, address, nationkey, phone, acctbal, mktsegment, and comment are the columns of the Trino customer table that match the fields of the Customer type.

If the annotated field has multiple arguments, they are combined in the SQL WHERE clause with an AND.

query

Optional. The value of this argument is the SQL query whose results are used to populate the sub-fields of the annotated field. While this value is optional, one of either table or query must be specified.

The query argument is useful when you need to perform a complex query, or when the table column names and GraphQL type fields do not match. For example:

customerById (id: ID!): Customer
  @dbquery (
    type: "trino",
    query: """SELECT "custkey", "name", "address", "nationkey", "phone", "acctbal", "mktsegment", "comment" FROM "customer" WHERE "custkey" = ?""",
    configuration: "trino_config"
  )

The directive executes the specified SQL query on the database specified by the trino_config. The SQL query renames columns if necessary to match the field name in the GraphQL type Customer.

configuration

Required. This argument identifies which configuration in the config.yaml file should be used to connect to the database.

A Trino database configuration contains the dsn for connecting to your database, and will look similar to this:

configurationset:
  - configuration:
      name: trino_config
      dsn: username@<host_name>:port?catalog=<catlog_name>&schema=<schema_name>

In this example, trino_config is the named configuration that will be referenced by the configuration property of @dbquery as configuration: trino_config.

To learn more about the configuration settings for connecting to your Trino database, see Trino configuration.

TrinoDB connector capabilities

The TrinoDB connector in API Connect Essentials supports the following capabilities:
  • Pagination: Supported for table queries.
  • Filter: Supported for table queries.
  • Sort: Supported for table queries.

For explanation about how to use pagination and filtering with the @dbquery directive, see Using @dbquery for pagination and filtering.