Join any OpenAPI or Swagger Rest API with Postgres using Hasura Remote Joins
Tags: openapi, remotejoins, graphql, swagger
Published: 2019-12-23 06:26:07

---
title: Join any OpenAPI or Swagger Rest API with Postgres using Hasura Remote Joins
published: true
date: 2019-08-02 07:12:39 UTC
tags: openapi, remote joins, graphql, swagger
canonical_url: https://blog.hasura.io/join-openapi-swagger-spec-data-to-graphql-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](https://github.com/praveenweb/openapi-swagger-remote-schema)

This post is a part of our [Remote Joins](https://blog.hasura.io/remote-joins-a-graphql-api-to-join-database-and-other-data-sources/) (available in preview) series. Remote Joins in Hasura allows you to join data across tables and remote data sources. You can sign up [here](https://hasura.io/remote-joins) if you'd like to be notified when we launch. Head to [the PR](https://github.com/hasura/graphql-engine/pull/2395) 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](https://discord.gg/hasura) or comment on [github](https://github.com/hasura/graphql-engine) and let us know what you think!

![](https://blog.hasura.io/content/images/2019/08/openapi-banner.png)

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](https://api.apis.guru/v2/specs/exchangerate-api.com/4/openapi.json); 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](https://github.com/IBM/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.

[![](https://raw.githubusercontent.com/hasura/graphql-engine/master/community/boilerplates/auth-webhooks/nodejs-express/assets/deploy-glitch.png)](http://glitch.com/edit/#!/import/github/praveenweb/openapi-swagger-remote-schema)

Change the OpenAPI schema in the `openapi.json` file, as necessary for modifications. It currently uses this [exchange-rate API](https://www.exchangerate-api.com).

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

![](https://blog.hasura.io/content/images/2019/08/add-remote-schema-openapi.png)

### Add Remote Relationship to Join

Now let's add the Remote Relationship called `currency_rates`

![](https://blog.hasura.io/content/images/2019/08/remote-relationship-open-api.png)

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:

- [Building a Music Playlist app with Gatsby, Contentful](https://blog.hasura.io/building-a-music-playlist-app-with-gatsby-contentful-auth0-hasura/)
- [Using GraphQL to join data across Google Places API and Postgres](https://blog.hasura.io/graphql-join-data-google-places-api-postgres-postgis-location/)
- [Build apps with rich content using Contentful and Postgres](https://blog.hasura.io/build-apps-with-rich-content-contentful-hasura-remote-joins/)
- [Join data from Google Sheets API and Postgres](https://blog.hasura.io/join-data-from-google-sheets-api-and-postgres-using-hasura-remote-joins/)