Join data from a GraphQL API with Database Table using Remote Schema to Database Joins

The GraphQL Joins feature enables you to create a unified GraphQL API by joining the data from different GraphQL services.

With GraphQL Joins, you can federate your queries and mutations across multiple GraphQL services as if they were a single GraphQL schema. You do not have to write extra code or change the underlying APIs.

There are four types of joins with Hasura:

In this article, you will learn about the Remote Schema to Database Joins.

How do RS-to-DB Joins work?

Remote Schema to Database allows you to join the data coming from the GraphQL API with the existing data from the database.

You achieve that by creating a relationship between the GraphQL API (remote schema) and your Hasura app. That means you are driving a relationship from the Remote Schema to the database.

GraphQL Joins

The above figure illustrates Remote Schema to Database Joins. Let’s see how it works with a real-world example.

E-Commerce with Hasura

In this example, you have a store service, and a Hasura application.

The store service is an example of an e-commerce schema containing items and orders. Each order contains items.

The Hasura application keeps track of users and their orders. When you query the orders, you can retrieve the information of the user associated with the orders.

The image illustrates the structure of the "users" table.

Hasura "users" Table

The next step is to add the "store" service as a remote schema in Hasura.

"store" Remote Schema

If you want to learn more about adding remote schemas in Hasura, the documentation covers the topic in detail here.

At this point, you have the service connected to Hasura as a remote schema, but there is no relationship between them. That means there is no connection between the orders and the users from the application.

The next is to connect the "store" service and the database with a Remote Schema to Database Join. The reason for joining the two is to be able to query user information alongside the orders. For instance, you might want to see all the orders and the person who made the order.

Create an array relationship from the remote schema to the database. The id field from the "store" remote schema maps to the order_id field from the database.

Remote Schema to Database Join

Save the relationship and you are done! You should be able to retrieve the orders and the user associated with each order.

Let’s take as an example the following query:

This query retrieves all the orders including the:

  • order ID
  • items from the orders - name, price, and quantity
  • information about the user who placed the order - id, name, and address

Running the query returns the following response:

Now the orders and the users are linked, meaning they can be queried together.

In this article, you joined two services together and also connected an external API to the database. You did that without writing any code or modifying the external GraphQL APIs. How cool is that?

If you want to read more about GraphQL Joins, you can do it here.
27 Jun, 2022
Subscribe to stay up-to-date on all things Hasura. One newsletter, once a month.
Accelerate development and data access with radically reduced complexity.