Ready to get started?

Download a free trial of the GraphQL Driver to get started:

 Download Now

Learn more:

GraphQL Icon GraphQL JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with GraphQL.

Create a Data Access Object for GraphQL Data using JDBI



A brief overview of creating a SQL Object API for GraphQL data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for GraphQL integrates connectivity to live GraphQL data in Java applications. By pairing these technologies, you gain simple, programmatic access to GraphQL data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read GraphQL data.

Create a DAO for the GraphQL Users Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyUsersDAO { //request specific data from GraphQL (String type is used for simplicity) @SqlQuery("SELECT Email FROM Users WHERE UserLogin = :userLogin") String findEmailByUserLogin(@Bind("userLogin") String userLogin); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to GraphQL

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to GraphQL.

You must specify the URL of the GraphQL service. The driver supports two types of authentication:

  • Basic: Set AuthScheme to Basic. You must specify the User and Password of the GraphQL service.
  • OAuth 1.0 & 2.0: Take a look at the OAuth section in the Help documentation for detailed instructions.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the GraphQL JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.graphql.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for GraphQL will typically look like the following:

jdbc:graphql:AuthScheme=Basic;User=username;Password=password;URL=https://mysite.com;InitiateOAuth=GETANDREFRESH

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:graphql:AuthScheme=Basic;User=username;Password=password;URL=https://mysite.com;InitiateOAuth=GETANDREFRESH"); MyUsersDAO dao = dbi.open(MyUsersDAO.class); //do stuff with the DAO dao.close();

Read GraphQL Data

With the connection open to GraphQL, simply call the previously defined method to retrieve data from the Users entity in GraphQL.

//disply the result of our 'find' method String email = dao.findEmailByUserLogin("admin"); System.out.println(email);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for GraphQL by integrating with the CData JDBC Driver for GraphQL. Download a free trial and work with live GraphQL data in custom Java applications today.