Using Remote Joins to Create a Unified GraphQL API for Your Company

Rate this content
Bookmark

With Hasura Remote Joins, you can join your table data with a Stripe API or Github API or any GraphQL API. Without writing a single line of code. Using GraphQL's amazing type system combined with Hasura's declarative configuration, you can create relationships across tables and other services and get a beautiful unified API. In a single call, get all the data necessary for building your apps from anywhere in the world.

9 min
18 Jun, 2021

Video Summary and Transcription

Hasura's remote joins feature allows you to combine multiple data sources into a unified GraphQL API, including Postgres databases, GraphQL APIs, and REST APIs. It enables joining data across different databases and integrating with external APIs like Stripe and Auth0. Hasura pushes down SQL to join data across databases and can even integrate with REST APIs using actions. With remote joins, you can query and fetch data from multiple sources, enriching your GraphQL schema and creating powerful data relationships.

Available in Español

1. Introduction to Remote Joins in Hasura

Short description:

I am Tanmay and I'm gonna talk to you a little bit about a new feature that we've been working on which is remote joins that allows you to kind of take multiple data sources and combine them into a unified GraphQL API. Hasura is an open source GraphQL engine that connects to your Postgres database but also other sources like a GraphQL source and a REST source and then allows you to kind of merge them together and now even join across them. Hasura runs as a Docker container in your own infrastructure.

Hi everyone. So glad to be here. I am Tanmay and I'm gonna talk to you a little bit about a new feature that we've been working on which is remote joins that allows you to kind of take multiple data sources and combine them into a unified GraphQL API. This is something that we've been doing at Hasura. Hasura is an open source GraphQL engine that connects to your Postgres database but also other sources like a GraphQL source and a REST source and then allows you to kind of merge them together and now even join across them. Hasura runs as a Docker container in your own infrastructure. Do check it out on Github, give it a spin. Let me know what you think.

2. Remote Joins and Integrating APIs

Short description:

You can combine data from a Postgres database and a GraphQL API, such as Stripe, using remote joins in Hasura. By integrating another GraphQL API, like Auth0, you can fetch additional details for each user. Adding the remote schema and creating a relationship allows you to query and fetch data from Auth0. Similarly, you can join across different databases, such as the music database in Hasura Remote Postgres.

So what I'm going to talk to you about is essentially being able to do something like this which is that you have data inside a Postgres database and you have a GraphQL API something like Stripe with OneGraph and what you want to do is make a GraphQL query that fetches the customer data from the database and maybe the Stripe data from the Stripe API as an example.

And so let's see what this looks like. Here I have Hasura running and this has just two tables which has user and events and what Hasura gives me is a GraphQL API on those tables. So what I can see is users and users have ID and e-mail and then I can make this GraphQL query. I can also do authorization and whatnot to make sure that this GraphQL API is actually secure.

And so what I'll be doing is now integrating another GraphQL API and then joining across that. So every user in my system has, for example, some is logged in with Auth0. And so they're going to have some Auth0 details like last login or profile details and stuff that are coming from the Auth0 service. And so I have a GraphQL API that's kind of wrapped over the Auth0 service. And so what I'll do is I'll take this and I'll add it as a remote schema in Hasura. So let's call it Auth0 and let's call it Auth0. And what I'll do is I'll add this as a URL here. And then what I can do is I can go to my user's table and I can create a relationship between my table and this GraphQL schema that I've added. So let's call it Auth0 profile and then let's say Auth0 and then choose to say that we want to use the email as a join condition, right? Because the email value is the same. Let's say I don't have the ID. I'm using email. I could have used ID as well, if the IDs were the same. As soon as I do that, what you'll see is that our GraphQL schema will get augmented and I'll be able to make a query to fetch details from Auth0. So that's a query where now I'm fetching this data from Auth0 as well. This data is coming in from Auth0. This data is coming in from our database.

Let's take another example where I can join across two databases. So I have another Hasura service and this is a different Hasura service. I've called it Hasura Remote Postgres, which has a music database. So there's like artists, albums, music tracks, stuff like that. It's kind of a standard demo app that I use. I'm going to take this GraphQL endpoint and add that to my main GraphQL schema here. Let's call it pgremote. And as soon as I add this here, let's go and add a relationship as well. So every user is also an artist.

3. Joining Data and Integrating REST API

Short description:

Let's link the artist field to pgremote using the ID column as the join condition. Now we can request artists and see their IDs, names, and albums. Hasura pushes down the SQL to join data across databases. Another example is joining with a REST API using actions. We specify the GraphQL spec to map the REST endpoint, which returns additional user information. The contract includes the user's ID, verification status, and address. The REST API endpoint is running on a Heroku app.

And so let's go and say artist and link it to pgremote, and we'll see an artist field. And we'll use the ID column this time as the join condition, right? Instead of the email instead of the email column.

So as soon as I do this, what I can now do is I can start making a request for artists as well. Well, there we go, we can see artists now. And I can say artist and artist ID and you know, the artist has a name and maybe the artist has albums as well. Cool. So the artist has an album.

Right. And so now what is happening is that this data is kind of getting joined across these two databases. So Hasura is actually pushing down the right SQL in both directions. It's pushing down the sequel here and pushing down the sequel that is joining across artists and albums.

The last quick example that I want to show you is an example of joining this with the REST API. So whenever we add rest data sources to Hasura we call them actions. So let's create an action to map to a REST endpoint that I have. And let me just see if I can bring up the REST endpoint here. So my REST endpoint, what it's going to... So what I do with Hasura is I specify the GraphQL spec to map that particular REST endpoint. And so in this case, my REST endpoint is a getUserInfo endpoint. It returns some extra information about the user. It's coming in from an external REST API. So let's say I have type query and then I say getUserInfo Id int. And this returns a user info object. And now what I can do is I can say type user info. So I'm just specifying the contract here. And the contract is id, which is an int. It has a verified, which is a boolean. So this is a REST API that I've already created, which is why I know what the contract is. And the address is string. And the endpoint that I have is here. It's also running on a Heroku app.

4. Remote Joins and REST API Integration

Short description:

I can make an API call to fetch user information from a REST API endpoint. However, I want to return user information from my database as well. To achieve this, I establish a relationship between the REST API and our database. By enriching the schema, I can query user details, such as ID and email, and fetch artist information, including name and albums. This query combines data from the REST endpoint, our database, and another GraphQL endpoint. Check out remote joins and feel free to reach out to me on Twitter with any questions.

And so let me just set that handler here. And let's create this. So as soon as I create this action, what happens is that I can go make this API call where I can say query, get user info, and I can fetch id and address and verify it. Right. And now this is this is backed by a REST API endpoint. Right. So let me just see if I can bring up that GitHub as well, just to show you what that looks like. REST data source. I might be running out of time. So I'm not going to show you the source code. But what this looks like right now is that there's a REST endpoint and the REST endpoint basically returns this data to us. Right. This is not cool because I would like to return the user information as well. So what I would like to do is I would like to have this REST API endpoint return information about the user which is in my database. And so I'm going to add a relationship from the REST API, right, to our database. So I'm going to say artist. And then I'm going to say that this is actually coming in from the sorry, I'm going to say user. This is actually coming in from a user table where the ID maps to the ID. But the ID maps to the ID. And now what I can do is if I go back to our graphical, you'll see that this schema has been enriched with the user entity and then I can add user and I can say ID, maybe email and then I can also fetch the artist information. Right? And the artist has a name and then I can do albums. And then I'll have the album will have a title. Right? So here's a query that I'm making and what this query is now doing is it's what this query is now doing is that it's making a query to the rest endpoint, fetching the rest data, then making a query to do joining that information with our database and then joining that information with another GraphQL endpoint, which happens to be on top of a database. So that's remote joints. Do check it out, do play around with it. Let us know what you think. And as ever, please feel free to reach out to me on Twitter if you have any questions whatsoever.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
Though GraphQL is declarative, resolvers operate field-by-field, layer-by-layer, often resulting in unnecessary work for your business logic even when using techniques such as DataLoader. In this talk, Benjie will introduce his vision for a new general-purpose GraphQL execution strategy whose holistic approach could lead to significant efficiency and scalability gains for all GraphQL APIs.

Workshops on related topic

GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.