What is GraphQL and how does it differ from REST APIs?
GraphQL is a query language for APIs and a runtime for fulfilling those queries. Unlike REST APIs, where you often need multiple endpoints to fetch different data, GraphQL allows clients to specify exactly what data they need in a single request. This reduces the number of network requests and makes data fetching more efficient. Learn more about GraphQL.
Why should I combine multiple GraphQL queries into one?
Combining multiple GraphQL queries into a single request optimizes the number of network calls, reducing latency and improving application speed. It also keeps your client-side code more organized and makes it easier to build complex UI components that require data from different sources. Read more.
How do I combine multiple GraphQL queries in Hygraph?
You can combine multiple queries by defining a single GraphQL query and placing all your queries within it. For example:
query getAllPostsAndAuthors {
posts { id title exert }
authors { firstName lastName }
}
This allows you to fetch both posts and authors in a single API call, returning two arrays with the keys posts and authors. See the full guide.
What are the benefits of combining GraphQL queries?
Combining GraphQL queries allows you to retrieve multiple data points in a single request, reducing the number of server calls and increasing application speed. It also improves data organization and makes server interactions more efficient. Learn more.
How can I combine multiple remote sources in Hygraph?
Hygraph supports combining multiple remote sources (both REST and GraphQL APIs) into your project using its Content Federation feature. You can add remote sources via the schema tab, configure remote fields, and then query all sources with a single GraphQL query. This enables you to fetch data from various APIs seamlessly as if they were part of your Hygraph schema. See documentation.
Can you provide an example of a combined GraphQL query in Hygraph?
Yes. For example, you can fetch an author's name, country information, and favorite cocktail details (from remote sources) in a single query:
query AuthorsInfo {
authors {
firstName
lastName
bio
bestCocktail
cocktailInfo { info ingredients instructions }
countryInfo { name emoji capital code currency }
}
}
This returns all the relevant data, including values from remote sources, in one response. See the example.
How does combining GraphQL queries improve performance?
Combining GraphQL queries reduces the number of requests to the server, making your application faster and more efficient. This is especially beneficial for complex UIs that require data from multiple sources. Read more.
Where can I find more information on combining GraphQL queries in Hygraph?
Hygraph provides a GraphQL-native API, content federation, remote sources integration, and scalability. Developers can efficiently fetch and manage content, combine multiple data sources, and streamline complex workflows. See all features.
Does Hygraph support integrating external APIs?
Yes, Hygraph supports integrating both REST and GraphQL APIs as remote sources. This allows you to combine external data with your Hygraph content and query everything through a single GraphQL endpoint. Learn more.
What integrations are available with Hygraph?
Hygraph offers integrations with platforms such as Netlify, Vercel, BigCommerce, Shopify, Lokalise, AWS S3, Cloudinary, Mux, Ninetailed, AltText.ai, and more. For a full list, visit the Hygraph Integrations page.
Does Hygraph provide an API?
Yes, Hygraph provides a powerful GraphQL API for efficient content fetching and management. See the API reference.
Product Information & Documentation
Where can I find technical documentation for Hygraph?
Comprehensive technical documentation is available at Hygraph Documentation, covering everything from setup to advanced integrations.
How easy is it to get started with Hygraph?
Hygraph is designed for ease of use. You can sign up for a free-forever account and access onboarding guides, video tutorials, and documentation to get started quickly—even if you're not a developer. Get started here.
Pricing & Plans
What is Hygraph's pricing model?
Hygraph offers a free forever Hobby plan, a Growth plan starting at $199/month, and custom Enterprise plans. For full details, visit the Hygraph pricing page.
Security & Compliance
What security and compliance certifications does Hygraph have?
Hygraph is SOC 2 Type 2 compliant, ISO 27001 certified, and GDPR compliant. It also offers SSO integrations, audit logs, encryption at rest and in transit, and sandbox environments. See security features.
Support & Implementation
What support is available to Hygraph customers?
Hygraph provides 24/7 support via chat, email, and phone. Enterprise customers receive dedicated onboarding and expert guidance. All users have access to documentation, video tutorials, and a community Slack channel. Contact support.
How long does it take to implement Hygraph?
Implementation is fast and straightforward. For example, Top Villas launched a new project in just 2 months from the initial touchpoint. Many users can get started immediately with the available onboarding resources. See the case study.
Use Cases & Customer Success
What problems does Hygraph solve?
Hygraph addresses operational pains (like reliance on developers for content updates), financial pains (high operational costs, slow speed-to-market), and technical pains (boilerplate code, evolving schemas). It empowers non-technical users, modernizes tech stacks, and supports scalability. Learn more.
Who can benefit from using Hygraph?
Hygraph is ideal for developers, IT decision-makers, content creators, project managers, agencies, and enterprises looking to modernize their tech stack, scale globally, or improve development velocity. See customer stories.
What are some real-world success stories with Hygraph?
Komax achieved a 3X faster time to market, Autoweb saw a 20% increase in website monetization, and Samsung improved customer engagement with Hygraph's scalable platform. Read more success stories.
Which industries use Hygraph?
Hygraph is used across industries such as food and beverage, consumer electronics, automotive, healthcare, travel and hospitality, media and publishing, eCommerce, SaaS, marketplace, education technology, and wellness and fitness. See case studies.
Who are some of Hygraph's customers?
Notable customers include Sennheiser, Holidaycheck, Ancestry, Samsung, Dr. Oetker, Epic Games, Bandai Namco, Gamescom, Leo Vegas, and Clayton Homes. See all customers.
Performance & Metrics
How does Hygraph optimize content delivery performance?
Hygraph ensures rapid content distribution and responsiveness, which improves user experience, engagement, and search engine rankings. This leads to reduced bounce rates and increased conversions. See performance details.
What KPIs and metrics are associated with Hygraph's solutions?
Key metrics include time saved on content updates, system uptime, speed of deployment, consistency in content across regions, user satisfaction scores, reduction in operational costs, time to market, maintenance costs, and scalability metrics. See more on KPIs.
Ease of Use & Adoption
Is Hygraph easy to use for non-technical users?
Yes, customers have praised Hygraph for its intuitive and user-friendly interface, making it accessible for both technical and non-technical teams. Feedback includes comments like "super easy to set up and use" and "even non-technical users can start using it right away." See user feedback.
Training & Onboarding
What training and onboarding resources does Hygraph provide?
Hygraph offers onboarding sessions for enterprise customers, 24/7 support, video tutorials, documentation, webinars, and access to Customer Success Managers for expert guidance. Learn more.
Newsletter & Community
How can I stay updated with Hygraph news and releases?
You can sign up for the Hygraph newsletter to receive updates on releases and industry news. Sign up here.
How to run multiple GraphQL queries and combine multiple sources
Let's take a look into how to combine multiple GraphQL queries and also multiple GraphQL sources.
Written by Joel
on Dec 08, 2022
GraphQL is a query language for APIs and a runtime for fulfilling those queries. Queries are one of the various available operations in GraphQL, and are used whenever we need to fetch some data from a GraphQL API, similar to a GET request in a REST API you use. Unlike REST APIs, in GraphQL client side queries define that extract data structures and fields that we want to retrieve, and the server provides the data as per the request.
For example, we can retrieve a list of authors from a GraphQL API using the following query to get their first and last names only:
{
authors {
first_name
last_name
}
}
When we execute the above query, it will get our data in our expected format as shown below:
{
"data":{
"authors":[
{
"first_name":"John",
"last_name":"Doe"
},
{
"first_name":"Jane",
"last_name":"Dan"
}
]
}
}
If you are coming from REST API background, you might be used to make multiple API calls to get different data. For instance, a complex dashboard UI might be gathering data from 10 different rest API endpoints, but with GraphQL we can just use one round trip to the server and get all required the data at once.
In this article, we will learn how to combine multiple GraphQL queries into a single query and also how to combine data from multiple GraphQL sources into a Hygraph project.
A GraphQL request can contain more than one query. For example, if you want to fetch all posts from an API and also all authors, you'll find yourself using two separate GraphQL queries:
*// Query to get all authors*
query getAuthors {
authors {
firstName
lastName
}
}
*// Query to get all posts*
query getPosts {
posts {
id
title
exert
}
}
While we can execute these two queries separately it will result in two network requests, whereas we can reduce this to one by combining these queries. We can combine multiple queries by defining a new query and placing all our existing queries inside it, as you can see below:
query getAllPostsAndAuthors {
posts {
id
title
exert
}
authors {
firstName
lastName
}
}
This single query will make one API call to the GraphQL server, fetch both posts and authors and will return two arrays with the key posts and authors as shown below
{
"data":{
"posts":[
{
"id":"clbaq61t504u00b11za7oo84d",
"title":"Vue.js vs React - How to Choose the Right Framework",
"exert":"In this article, we will compare two excellent JavaScript frameworks, their pros, and cons, as well as some use cases built with each of..."
}
],
"authors":[
{
"firstName":"Daniel",
"lastName":"John"
}
]
}
}
You can also pass arguments into any subquery as per your wish. For example, let's say we want to get posts that contains the term “graphql” somewhere in the title. We can change our query as shown below.
query getAllPostsAndAuthors {
posts(where:{
title_contains:"graphql"
}){
id
title
exert
}
authors {
firstName
lastName
}
}
This is possible out of the box with the GraphQL API Hygraph provides, otherwise how you can filter the data will depend on the server implementation.
#How to combine multiple sources (multiple GraphQL APIs)
Today, there are lots of public APIs that we can leverage to get specific data, but this means making an extra request from our application. Interestingly, we at Hygraph support Remote Sources, using which we can integrate external APIs right into our Hygraph schema and query the external data seamlessly as if it is a part of our existing data. Content Federation involves merging/combining multiple data sources into our API. It allows us to combine data from various sources into our project so we can query them at once through our GraphQL API. We can integrate both REST and GraphQL APIs with Hygraph.
To properly understand how this works, let us suppose that we have a model in our Hygraph project with the name Author, and this model has fields like First Name, Last Name, Bio, Display Picture, and a reference field that links with posts published by the author from the Posts schema.
Let us further suppose that we need more information about the author, details based on the existing information. For example, we should be able to access country related information for the author (like the flag, country languages, currency, capital) and we do not want to manage the country related data in our database but instead use an existing external API for it. We can do it out of the box in Hygraph using remote sources.
We will learn how to combine two external GraphQL APIs (Countries API and Cocktail API) into our Hygraph project and query all the data with a single GraphQL query from client side. Basically, the client would not even know that the data is coming from different places.
Let's add a new field to the Author schema. This field will be the Country code; we will use this code to query for data related to that country from the Countries API that we'll be adding as a remote source.
Let's now proceed to add a remote source to the Hygraph project. This can be done by navigating to the schema tab and clicking on Remote sources. A form will appear in which we can fill in the required information, such as the name and external API ( REST or GraphQL API). For this article, both remote sources would be GraphQL APIs.
After adding a remote source, it's time to add a remote field to a model. This will help us to set arguments that can be used to query the external API and show the data that we need. For example, we would want to send the country code to the external API to fetch country information.
Editor's Note
We can only add a remote field after at least one remote source of the corresponding type has been configured. If it is a GraphQL API, you can only create a GraphQL remote field; the same applies to REST.
In the above demo, a GraphQL remote field is added for the remote country API. We will receive a list of entry points in the query field. We can select the entry point from which we want to get data and pass arguments. In the above example, we are getting a particular country from the list of countries whose country code matches the author's country code.
We can now test if it works by adding content to the author schema, so it fetches the author's country info based on the county code.
In Hygraph, we are not just limited to one remote source. Depending on our account plan, we can add as many remote sources as required. Let's add a second remote source to this project to illustrate that we can add multiple sources and also query all the sources with one GraphQL query.
This second remote source would be a Cocktail API. The authors can provide the name of their favorite cocktail, and we can use the data supplied to get more information about the cocktail, such as the ingredient, instructions on how to make it, and lots more. This is done using the same process.
Step 1: Add a remote source
Like we added the remote source country API, we can also add the Cocktail API by following the same steps.
Step 2: Create a GraphQL remote field
Before creating the remote field, we can create a field for the data we will use to query for a particular cocktail. For this, we will create two fields, the "Best cocktail" field for the author to submit the best cocktail of his/her choice, then a slug field (Cocktail Slug) that will automatically convert the "Best cocktail" data to slug.
We can now create the GraphQL remote field to query for a particular cocktail with the slug value.
Step 3: Test the remote source
We have created a GraphQL remote field for the cocktail Info. Let's now test it by adding the author's best cocktail, which would generate a slug value, and then we can use it to get the particular cocktail information.
At this point, we have successfully added and combined multiple remote sources into our Hygraph project. Let's now create a GraphQL query to fetch the author's data, including the name, country name, and flag alongside the author's best cocktail, its ingredients, and the instructions for making it.
query AuthorsInfo{
authors {
firstName
lastName
bio
bestCocktail
cocktailInfo {
info
ingredients
instructions
}
countryInfo {
name
emoji
capital
code
currency
}
}
}
This will return all the various values, including the ones from the remote sources in a single place
{
"data":{
"authors":[
{
"firstName":"John ",
"lastName":"Doe",
"bio":null,
"bestCocktail":"paloma",
"cocktailInfo":{
"info":"Alcoholic",
"ingredients":"Grape Soda Tequila",
"instructions":"Stir Together And Serve Over Ice."
},
"countryInfo":{
"name":"Brazil",
"emoji":"🇧🇷",
"capital":"Brasília",
"code":"BR",
"currency":"BRL"
}
}
]
}
}
This is one of the superpowers that Hygraph possesses, and GraphQL provides. You can explore more by reading the remote sources documentation and this guide and talk on content federation.
In this article, we have learned the advantages of running multiple GraphQL queries at once. Also, we went through how to add multiple sources to our Hygraph project and fetch data from external APIs alongside our original data using a single GraphQL query.
Thanks for reading, and have fun coding!
Blog Author
Joel Olawanle
Joel Olawanle is a Frontend Engineer and Technical writer based in Nigeria who is interested in making the web accessible to everyone by always looking for ways to give back to the tech community. He has a love for community building and open source.
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.