DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Pagination in GraphQL: Efficiently Retrieve and Manipulate Data
  • Boosting Self-Hosted GraphQL API Performance With Apollo Router and Federation
  • Why and When to Use GraphQL
  • Optimizing Natural Language Queries for Multi-Service Information Retrieval

Trending

  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Testing SingleStore's MCP Server
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  1. DZone
  2. Data Engineering
  3. Databases
  4. Building a Unified API Using GraphQL Joins

Building a Unified API Using GraphQL Joins

The following is a simple use case for creating a unified API with Hasura GraphQL Joins in under five minutes.

By 
Shruti Kapoor user avatar
Shruti Kapoor
·
Mar. 03, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.0K Views

Join the DZone community and get the full member experience.

Join For Free

GraphQL Joins lets developers join data from two different GraphQL services to create a unified GraphQL API without needing to write custom code or modify any underlying APIs. It enables you to quickly fetch data from across databases and other GraphQL services as if they were one schema to easily mix and match your data sources. 

To create a join, you need to define the relationship between the two data sets you want to unify. Joins are typically based on a shared key between the two objects, such as an ID or name. Once the relationship is established, the query returns both data sets, allowing us to ask complex questions and receive a wealth of information in one response.

The following is a simple use case for creating a unified API with Hasura GraphQL Joins in under five minutes.

This example uses two remote GraphQL APIs, a country API and a weather API. Our goal is to find a list of countries and then connect the weather for the capitals. Both APIs are different remote GraphQL endpoints that we need to connect. Let’s get started!

In the Hasura API dashboard, we already queried the countries in the Explorer section on the left. (To learn how to add a remote schema, go here.) 

Remote Schemas Dash

Go to the Remote Schemas dash to connect the countries and weather APIs that are GraphQL points. We start off by adding an existing GraphQL API as a remote schema. For our use case, we are adding a third-party GraphQL API containing country information. 

We will call this API "Countries."

Countries

Next, we will add a weather endpoint. You can name it anything you want, but we’ll choose “Weather” for our use case. Enter the GraphQL server URL and hit “Add.” If you need authorization headers to pass, add them in the “Additional Headers” field. 

New GraphQL Schema

The Weather remote schema will appear below the Countries schema.

In order to avoid conflicting definitions for “Language” in the Country and Weather fields, you need to add customization. Click on “Countries” and then go to “Modify.” Click the add button under “GraphQL Customizations.” In the Root Field Namespace, enter “countries_graph” and add “Countries” in the Prefix field, then hit Save.

In the Root Field Namespace, enter “countries_graph” and add “Countries” in the Prefix field, then hit Save.

Now go back to the API dash, and you will see “countries_ graph,”  “getCityby,” and “getCityByName” namespaces in the Explorer box. Click on “countries_ graph” from the dropdown menu, select “continent,” select “code,” and enter “NA” for North America. Then, under “countries,” select “capital” to fetch them.

Click on “countries_ graph” from the dropdown menu, select “continent,” select “code,” and enter “NA” for North America. Then, under “countries,” select “capital” to fetch them.

To fetch the weather for each capital, go back to the Remote Schemas dash and select “Relationships” for the Countries endpoint. GraphQL joins relationships enable you to make nested object queries if the tables/views in your database are connected or related in the remote APIs. 

Here we will create an API that lets us fetch weather for all the country capitals of a continent that will map a relationship between a country and the weather schema.

Here we will create an API that lets us fetch weather for all the country capitals of a continent that will map a relationship between a country and the weather schema.

Next, click on “Add a new relationship” and give it a name. We’ll use “GetCapitalWeather.” Then under Source Remote Schema, select “Country,” and for the Reference Remote Schema, select “Weather.”

Next, click on “Add a new relationship” and give it a name. We’ll use “GetCapitalWeather.” Then under Source Remote Schema, select “Country,” and for the Reference Remote Schema, select “Weather.”

Next, click on “Query” and “getCityByName” in the Mapping section of the dropdown menu. Select “name,” “Source Field,” and then “Capital” in the From Field dropdown menu. This will map the name parameter to the capital that's coming from the “Country” endpoint. 

Next, click on “Query” and “getCityByName” in the Mapping section of the dropdown menu. Select “name,” “Source Field,” and then “Capital” in the From Field dropdown menu.

Now, go back to the API dash again and click on “countries_graph” in the Explorer box. In the dropdown menu, click on “code” and type in “NA” to fetch the code of North America.

Now, go back to the API dash again and click on “countries_graph” in the Explorer box. In the dropdown menu, click on “code” and type in “NA” to fetch the code of North America.

Next, scroll further down until you reach the “GetCapitalWeather” relationship we just added. Select the weather parameters you want, such as actual, minimum, or maximum temperature.

Next, scroll further down until you reach the “GetCapitalWeather” relationship we just added. Select the weather parameters you want, such as actual, minimum, or maximum temperature.

Then hit the play button to run the query to get the capital and weather of the country.

Then hit the play button to run the query to get the capital and weather of the country.

That's it. Now you can create your own unified APIs via Hasura GraphQL Joins and easily connect different remote GraphQL endpoints with just a UI and without writing any time-consuming code. 

For information about GraphQL Joins with Hasura, check out the resource links below.

  • Weather API 
  • Countries API 
  • Adding a remote schema
API GraphQL Use case remote Schema

Opinions expressed by DZone contributors are their own.

Related

  • Pagination in GraphQL: Efficiently Retrieve and Manipulate Data
  • Boosting Self-Hosted GraphQL API Performance With Apollo Router and Federation
  • Why and When to Use GraphQL
  • Optimizing Natural Language Queries for Multi-Service Information Retrieval

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!