/

hasura-header-illustration

Join any OpenAPI or Swagger Rest API with Postgres using Hasura Remote Joins

TL;DR

Use Hasura Remote Joins to join data from your Postgres database with any OpenAPI/Swagger compliant REST API. Boilerplate - openapi-swagger-remote-schema

This post is a part of our Remote Joins series. Remote Joins in Hasura allows you to join data across tables and remote data sources. Data Federation using Hasura Remote Joins is now available in v1.3.0 stable release.
Try it out on Hasura Cloud and let us know what you think!

In this example, we will look at how data from a public API conforming to the OpenAPI spec can be joined with existing data in Postgres using Hasura Remote Joins. We are going to make use of this OpenAPI Service; it is a currency exchange rate API. Given a currency, it will return exchange rates for all other global currencies.

Alright, now let's say there is a users table with columns id, name, currency.  Now we would like to query users data along with the exchange rate data in the public API. Let's see how this can be done using the openapi-to-graphql tool.

Adding OpenAPI Service as Remote Schema

To be able to query OpenAPI serice via Hasura, it needs to be added as a Remote Schema using the Hasura Console. Let's deploy the GraphQL server on Glitch.

Change the OpenAPI schema in the openapi.json file, as necessary for modifications. It currently uses this exchange-rate API.

Get the GraphQL API Endpoint from Glitch and add it as remote schema.

Add Remote Relationship to Join

Now let's add the Remote Relationship called currency_rates

Now the GraphQL query to fetch this data in a single API call would look like the following:

query {
  users {
    id
    name
    currency
    currency_rates {
      date
      rates
    }
  }
}

Notice that, the nested query currency_rates come from exchange rate API following the OpenAPI spec. It will apply the filter of users.currency = currency_rates.baseCurrency, there by only giving exchange rates related to the current user's currency.

Checkout our other posts in the Remote Joins series:

Blog
02 Aug, 2019
Email
Subscribe to stay up-to-date on all things Hasura. One newsletter, once a month.
Loading...
v3-pattern
Accelerate development and data access with radically reduced complexity.