Connecting JDBC supported databases

You can use any JDBC supported databases as a data source for your GraphQL API by extending your GraphQL API with the @dbquery directive.

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

@dbquery (type: String!, schema: 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 JDBC supported database as a data source for your GraphQL API.

type

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

schema

Required. This argument specifies the schema of database to query.

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: "jdbc"
    schema: "retail"
    table: "customers"
    configuration: "jdbc_config"
  )

This example passes the following database query to the database specified by the jdbc_config configuration (See Managing backend connections for more information about configurations).

SELECT "id", "name", "email", "creditCard" FROM "retail"."customers" WHERE "id" = ?

where id, name, email and creditCard are the columns of the database customers 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.

Note: JDBC supported database comparisons are case-sensitive, so be careful when naming the arguments to match the database table columns.

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: "jdbc",
    query: 'SELECT "id", "full_name" AS "name", "email" FROM "retail"."customers" WHERE "id" = ? AND "creditCard" IS NOT NULL',
    configuration: "jdbc_config"
  )

The directive executes the specified SQL query on the database specified by the jdbc_config. The SQL query both renames full_name to name so it matches the field name in the GraphQL type Customer, and retrieves only those customers who have a credit card.

Note: Unquoted column names in the database table are converted to lower case, so make sure that you match the referenced columns in the specified query to the database table columns.

configuration

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

A JDBC supported database configuration contains the url for connecting to your database, and will look similar to this:

configurationset:
  - configuration:
      name: jdbc_config
      url:  <a valid JDBC URL>

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