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

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.



Transcription


Hi everyone. So glad to be here. I'm Tanmay and I'm going to 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. 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, you know, the customer data from the database and maybe the Stripe data from the Stripe api. Right. 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, and users have ID and email, 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, you know, last login or, you know, 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 users 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. So this data is coming in from Auth0, this data is coming in from our database. Let's take another look at, 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 pg-remote. And as soon as I add this here, let's go and add a relationship as well. So every user is also an artist. So let's go and say artist and link it to pg-remote 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 column. So as soon as I do this, what I can now do is I can start making a request for artists as well. There we go. We can see artists now. And I can say artist and artist ID, and 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 SQL here and it's pushing down the SQL that is joining across artists and albums, right? The last quick example that I want to show you is an example of joining this with a 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 get user info endpoint. It returns some extra information about the user, right? It's coming in from an external REST api. So let's say I have type query and then I say get user info ID int, right? And this returns a user info object, right? 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. This is also running on a Heroku app. 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 verified, right? And now 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 to our database. 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. I'm going to say user table where the ID maps to the ID. 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 the 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 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 joins. 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. Thanks, folks.
9 min
18 Jun, 2021

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

Workshops on related topic