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

02 August, 2019

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:

search icon

About Hasura

Hasura allows you to mobilize & federate your organisation’s data by building a powerful, secure & flexible GraphQL API, that can query data in your databases, HTTP services, serverless functions as well as third party APIs.
Like what you read? Join our team! We’re hiring

Praveen Durairaju

Praveen Durairaju

Application Engineer. Tech Enthusiast. Follow on Twitter - https://twitter.com/@praveenweb

Read More