This tutorial aims to guide you through the process of combining multiple GraphQL services into a single schema using schema stitching and schema federation methods.
By the end of this tutorial, you will be able to:
- Understand schema stitching and schema federation.
- Combine multiple GraphQL services into a single schema.
- Handle potential challenges during the process.
Before starting this tutorial, you should have a basic understanding of:
- JavaScript (ES6)
- GraphQL basics
- Node.js and npm
Schema stitching is the process of creating a single GraphQL schema from multiple underlying GraphQL APIs.
The main idea behind schema stitching is to create a new schema, then extend it for each existing schema and finally merge them all together.
// Import required libraries
const { mergeSchemas } = require('graphql-tools');
const userSchema = require('./user');
const productSchema = require('./product');
// Merge the schemas
const schema = mergeSchemas({
schemas: [
userSchema,
productSchema,
],
});
In the example above, we import two schemas (userSchema
and productSchema
) and then merge them into one schema using the mergeSchemas
function from the graphql-tools
library.
Schema federation is a more advanced method of combining schemas. It allows you to import types from one service into another and to split your business logic into smaller services.
Federation involves creating a gateway and services. The gateway fetches the schema of each service and combines them into one.
// Import required libraries
const { ApolloServer, gql } = require('apollo-server');
const { buildFederatedSchema } = require('@apollo/federation');
// Define a schema
const typeDefs = gql`
type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
name: String
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
me() {
return { id: "1", name: "Alice" };
}
},
User: {
__resolveReference(user, { dataSources }) {
return dataSources.usersAPI.getUser(user.id);
}
}
};
// Build the federated schema
const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }])
});
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
This example shows a simple schema stitching process with two schemas: userSchema
and productSchema
.
const { mergeSchemas } = require('graphql-tools');
const userSchema = require('./user');
const productSchema = require('./product');
const schema = mergeSchemas({
schemas: [
userSchema,
productSchema,
],
});
This example shows a simple schema federation process.
const { ApolloServer, gql } = require('apollo-server');
const { buildFederatedSchema } = require('@apollo/federation');
const typeDefs = gql`
type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
name: String
}
`;
const resolvers = {
Query: {
me() {
return { id: "1", name: "Alice" };
}
},
User: {
__resolveReference(user, { dataSources }) {
return dataSources.usersAPI.getUser(user.id);
}
}
};
const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }])
});
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
In this tutorial, we have learned about two methods of combining multiple GraphQL services into a single schema: schema stitching and schema federation.
The next steps for learning would be to explore each method in deeper detail, and to practice by creating and combining more complex schemas.
For additional resources, check out the official GraphQL documentation and the Apollo Federation documentation.
Solutions: