API Aggregation / Composition Pattern for GraphQL APIs
The API Aggregation / Composition pattern allows you to combine multiple GraphQL APIs into a single GraphQL API.
Problem
You have multiple GraphQL APIs that you'd like to expose as a single unified API. This is useful so that clients don't have to interact with multiple APIs, but instead can interact with a single API.
Solution
To implement this pattern, you'll first have to merge the schemas of all APIs you'd like to compose. This doesn't come with it's own challenges, as you'll have to make sure that there are no naming conflicts between the APIs. We'll further discuss this topic in the API Namespacing Pattern pattern.
Once we've got a merged and conflict-free schema, we need to implement a runtime that's aware of the merged schema, all origin APIs, and how to delegate requests to the origin APIs.
The whole second part of this website is dedicated to building such a runtime from scratch.
Example
Let's say we have two GraphQL APIs:
# API 1
type Query {
hello: String!
}
# API 2
type Query {
world: String!
}
We can merge these two APIs into a single API:
# Merged API
type Query {
hello: String!
world: String!
}