loading...
Hasura

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

hasurahq_staff profile image Hasura Originally published at blog.hasura.io on ・3 min read

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 (available in preview) series. Remote Joins in Hasura allows you to join data across tables and remote data sources. You can sign up here if you'd like to be notified when we launch. Head to the PR to check out more details, read the preview docs, and to try out a test Hasura image that you can spin up. Jump on our discord or comment on github 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:

Posted on by:

hasurahq_staff profile

Hasura

@hasurahq_staff

⚡️ Instant GraphQL APIs to build realtime apps & APIs. Connect Hasura to your database & other data sources (REST & GraphQL servers, 3rd party APIs) and get a unified data access layer instantly.

Hasura

Hasura GraphQL Engine is an opensource service that gives you instant realtime GraphQL APIs on Postgres with fine grained access control.

Discussion

pic
Editor guide