Technical Concepts: Schema Stitching, GraphQL Federation, and Content Federation
What is schema stitching in GraphQL?
Schema stitching is a technique that allows you to combine multiple GraphQL schemas from different microservices into a single unified API endpoint. A gateway service imports individual schemas, stitches them together, and exposes all types, queries, and mutations to clients. This approach is suitable for integrating independently built services under one endpoint with minimal shared types. Learn more.
What is GraphQL Federation and how does it differ from schema stitching?
GraphQL Federation enables a distributed architecture for large-scale GraphQL backends by allowing multiple GraphQL services (subgraphs) to work together as a single data graph. Unlike schema stitching, federation enforces the separation of concerns principle, where each subgraph is responsible for its own types and fields. The router automatically merges federated schemas, making it ideal for scalable, maintainable systems with many shared types. Federation has a higher learning curve but is better suited for complex, large-scale projects. Read more.
What is content federation and how does Hygraph support it?
Content federation is the process of integrating data from multiple remote sources into a CMS platform without migrating or duplicating the data. Hygraph supports content federation through its Remote Sources feature, allowing you to configure external RESTful or GraphQL APIs and query them alongside your CMS content. This is especially useful for aggregating and managing content from various systems in a unified API. Learn more.
What are the key differences between schema stitching, GraphQL federation, and content federation?
The main differences are:
Schema Stitching: Combines multiple GraphQL schemas into one endpoint, requires manual gateway maintenance, and is best for integrating independently built services with minimal shared types. Low learning curve.
GraphQL Federation: Enables distributed, scalable architecture with automatic schema merging via a router, enforces separation of concerns, and is ideal for large-scale systems. High learning curve.
Content Federation: Integrates data from multiple remote sources into a CMS (like Hygraph) using remote configuration, without requiring data migration. Low learning curve, focused on content aggregation in CMS contexts.
What is the learning curve for schema stitching, GraphQL federation, and content federation?
Schema stitching and content federation both have a low learning curve, making them accessible for teams looking to quickly integrate services or content sources. GraphQL federation, on the other hand, has a high learning curve due to its complexity and the need to understand federation concepts and schema design principles. Learn more.
Features & Capabilities
What features does Hygraph offer for content federation?
Hygraph provides content federation through its Remote Sources feature, allowing seamless integration of external RESTful and GraphQL APIs into your CMS. This enables you to unify and manage content from multiple systems without data migration, supporting both composable architectures and scalable digital experiences. Learn more about Hygraph Content Federation.
Does Hygraph support both REST and GraphQL APIs for content federation?
Yes, Hygraph supports integration with both RESTful and GraphQL APIs as remote sources, making it flexible for a wide range of content aggregation scenarios. Read more.
How does Hygraph's approach to content federation differ from schema stitching and GraphQL federation?
Hygraph's content federation is focused on aggregating and managing content from multiple sources within a CMS context, using remote configuration for external APIs. In contrast, schema stitching and GraphQL federation are backend server techniques for combining or federating GraphQL schemas. Content federation does not require manual gateway maintenance or strict schema design principles, making it easier to implement for content-centric use cases. See detailed comparison.
Use Cases & Benefits
When should I use schema stitching, GraphQL federation, or content federation?
Schema Stitching: Use when you have independently built GraphQL services with minimal shared types and want to quickly unify them under a single endpoint.
GraphQL Federation: Use for large-scale, distributed systems where multiple teams manage different services with many shared types, and you need scalability and maintainability.
Content Federation (Hygraph): Use when you have a CMS and want to aggregate content from various external systems without migrating data, enabling a unified content API for digital experiences.
What are the benefits of using Hygraph for content federation?
Hygraph enables you to unify content from multiple sources, reduce integration complexity, and deliver consistent digital experiences without data migration. Its low-code interface and support for both REST and GraphQL APIs make it accessible for technical and non-technical users. This approach streamlines content management and accelerates time-to-market for digital products. Learn more.
Getting Started & Documentation
Where can I find more technical documentation about Hygraph and content federation?
You can access comprehensive technical documentation for Hygraph, including guides on content federation, API integration, and schema design, at Hygraph Documentation.
Does Hygraph provide an API for content management and federation?
Yes, Hygraph offers a powerful GraphQL API for efficient content management and federation. You can learn more about the API and its capabilities at the Hygraph API Reference.
Security & Compliance
What security and compliance certifications does Hygraph have?
Hygraph is SOC 2 Type 2 compliant, ISO 27001 certified, and GDPR compliant. These certifications ensure enterprise-grade security and data protection for all users. For more details, visit the Hygraph Security Features page.
Support & Implementation
How easy is it to get started with Hygraph's content federation features?
Hygraph is designed for ease of use, with a low-code interface for configuring remote sources and integrating external APIs. Users can sign up for a free account and access onboarding guides, documentation, and video tutorials to get started quickly. Get started with Hygraph Documentation.
Schema Stitching vs. GraphQL Federation vs. Content Federation
We will discuss Schema Stitching, GraphQL Federation, and Content Federating. Describe their definitions and the differences between them.
Written by Aagam
on Aug 08, 2023
This article assumes you have a decent knowledge of building basic GraphQL servers and fundamental GraphQL concepts. If you are new to GraphQL, we recommend starting with this article.
Schema stitching is one of the ways in which we can combine multiple GraphQL services and expose them collectively via a single endpoint to the clients. In schema stitching, there are many GraphQL microservices, and we have a Gateway in front of these microservices that interacts with the client. The way this technique works is by importing individual schemas from different services on the gateway. The schemas are then stitched by the gateway, wherein the gateway combines all types, queries, and mutations from different services into a new executable schema which is then exposed to the clients.
To understand Schema Stitching better, let us take an example, suppose we have a User Microservice and a Contact Microservice. The relationship is one user can have many contacts.
This is our User service schema
typeUser{
id:ID!
name:String!
email:String!
}
typeQuery{
user(id:ID!):User
users:[User!]!
}
The GraphQL schema defines User type and exposes two queries - user to retrieve a single user and users to fetch a list of users.
This is our Contact service schema
typeContact{
id:ID!
name:String!
phoneNumber:String!
userId:ID!
}
typeQuery{
contact(id:ID!):Contact
contacts:[Contact!]!
}
This GraphQL schema defines a Contact type and exposes two queries - contact to retrieve a single contact and contacts to fetch a list of contacts.
Let us say these two schemas are served by two different GraphQL endpoints, now in order to query everything under a single endpoint, we need to set up a gateway service that will combine these schemas and stitch them like this.
The provided gateway code sets up an Apollo server that acts as a gateway to multiple GraphQL services. The getGatewaySchema function stitches the schemas of the user service and contacts service together using the stitchSchemas function from @graphql-tools. This code enables the gateway to handle incoming GraphQL queries and distribute them to the respective services, aggregating and returning the results to the client.
We can also query types that have relations and configure how to resolve data across our microservices. For instance, in the above example, you may want to query all contacts for a user, or while querying contacts, you may want to see what contact a user belongs to.
In schema stitching, to resolve these situations, there are three approaches
Schema Extensions
Type Merging
Directive-based Type Merging
Schema Extensions and Type Merging are techniques wherein we write some code at the gateway level to accommodate type merging. The Gateway will have a bird’s-eye view of its all microservices and their respective types, it will extend these types and delegate resolving fields of different types to respective microservices. We have to manually provide and maintain this configuration in the Gateway code. Directive-based type merging is a technique that is similar to Federation, wherein the microservices will have the configuration on how to resolve fields and not the Gateway.
Schema stitching is a quick solution to provide you with a unified GraphQL API. It is appropriate for use cases where there are not many shared types among GraphQL services, and we just want to combine different GraphQL services under a single endpoint. For example, if we have a software product with a backend GraphQL API and we have another separate PDF GraphQL microservice. Now we want the frontend systems to access these two backend services via a unified interface then, schema stitching will be a good approach as it requires less effort. Also, the Product and PDF microservice will not have so many shared types, so maintenance on the gateway is likely to be low. Schema stitching provides a quick and straightforward way to combine schemas.
Given that we can manage shared types in schema stitching and resolve fields via the Gateway, it is likely that gateway code can quickly get messy if numerous teams are managing a large number of microservices with many common types. Stitching might not be the most optimal choice for building very large-scale systems. If you have many microservices that use shared types and different microservices are responsible for different fields of a shared type, then it's probably better to take a look at GraphQL Federation.
Similar to Schema Stitching, GraphQL Federation also allows you to bring multiple GraphQL services under a single endpoint. Federation is a long-term scalable solution that allows you to build large systems and scale them as per your needs with low maintenance efforts. It is harder to implement a federated graph compared to stitching two existing GraphQL microservices and has a steeper learning curve as developers need to understand all federation ecosystem concepts and implement all microservices accordingly, whereas schema stitching is more like integrating a library over your existing services.
In federation, particularly talking about Apollo federation, we call individual GraphQL services with their individual schemas and resolvers as SubGraphs. There is also a Router that builds a SuperGraph from existing subgraphs and is exposed to the client. The high-level architecture might look similar to schema stitching, but the code-level implementation and, most importantly the GraphQL schema design of subgraphs differ greatly compared to schema stitching.
Federation highly promotes the separation of concerns principle. Designing schemas that are separated by concerns is essential for implementing federated graphs. For example, in the schema stitching example above, we saw User and Contact types individually existing in User and Contact services, respectively. The Gateway can have the connection logic implemented and type merging if needed. On the other side, the federation promotes building schemas that are separated by concerns. This means each subgraph should define types and fields that it is responsible for populating from its backend data store. To build a federated graph for the same use case, this is how our schemas would look like.
User service schema
typeUser@key(fields:"id"){
id:ID!
name:String!
email:String!
}
typeQuery{
user(id:ID!):User
users:[User!]!
}
Contact service schema
typeUser@key(fields:"id"){
id:ID!
userContacts:[Contact!]!
}
typeContact{
id:ID!
name:String
phoneNumber:String
user:User
}
typeQuery{
contact(id:ID!):Contact
contacts:[Contact!]!
}
Carefully check the User type. It is defined in both the User and Contact subgraph. This is the separation of concerns principle in effect. Even though userContacts is actually a field of User type, it is not defined in the user service but in the contact service because it has more to do with getting all contacts, so we shared the User type between contact and user subgraphs. These shared types are known as entities in federation, and different subgraphs might be responsible for populating different fields of an entity. The @key directive is used to define the primary key (in this case - id) among all subgraphs, which denotes how to uniquely identify a User type across subgraphs. From the perspective of the User type, when you query from the unified gateway, the fields name and email will be resolved by the User subgraph, and the field userContacts will be resolved by the Contact subgraph.
In Federation, the router will build the supergraph automatically and also handle the type merging for you, all you need to do is provide subgraph URLs to the router, and it will build the supergraph for you by a process known as composition. Once your router is up, it will expose all fields under a unified User type, and you can directly make a nested query below, and it will be handled by respective subgraphs.
query{
users{
name
email
userContacts{
name
phoneNumber
}
}
}
GraphQL Federation is the ideal choice when there are multiple teams maintaining different services with their own data and functionality, there are many shared types. You must carefully design your schemas in your subgraphs based on the design principle of separation by concerns. It is good to use federation when designing your services from scratch that can scale well.
Same / Similar Graphic from this section can be put here
Content federation is again the ability to bring data together from multiple sources and backends via API instead of actually migrating the data itself. It sounds a lot like what we have seen so far in this article, but it is directed more towards a CMS context, whereas schema stitching and GraphQL federation are directed more towards building a backend GraphQL server.
If you are using a CMS service or have your own CMS set up, and you want to integrate some additional data that is provided by some other external API, then you can use content federation if it is supported by your CMS. HyGraph supports content federation by Remote Sources. With Remote Sources, you can seamlessly configure any external data source and query it from the Playground. A Remote Source refers to a system or product that contains content that needs to be integrated with the content in Hygraph, creating a unified API. Setting up a Remote Source is a straightforward process through our user-friendly low-code interface. We provide support for both RESTful and GraphQL APIs. Take a deeper look at Content Federation in this article if you want to.
#Key Differences between Schema Stitching vs. GraphQL Federation vs. Content Federation
Schema Stitching
GraphQL Federation
Content Federation
Enables you to combine multiple GraphQL schemas into one.
Enables a distributed architecture for a large-scale GraphQL backend.
Integrates data from multiple remote sources into a CMS platform.
Two techniques of schema require manual update and maintenance of the gateway.
The router automatically merges federated schema from subgraphs.
Integrates data sources using remote configuration.
Separation of concerns is good to have but is not a strict requirement to be followed.
The separation of concerns principle is mandatory and very important while designing the schema of subgraphs.
Content Federation does not directly involve the separation of concerns principle.
Good for use cases when the services are already built independently, and we just need a way to bring them under a single endpoint.
Good for use cases where we need to design large-scale GraphQL systems from scratch. Federation facilitates scalability, maintainability, and independent development of subgraphs.
Good for use cases when there is already a CMS in our architecture, and we want to aggregate and manage content from various systems without migrating or duplicating the data.
The learning curve is low
The learning curve is high
The learning curve is low
The GraphQL Report 2024
Statistics and best practices from prominent GraphQL users.
As a Software Engineer, my daily routine revolves around writing scalable applications with clean code & maintaining them. In my spare time, I love to explore software architecture patterns, write tech articles & watch thrillers!
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.