This tutorial aims to give you an introduction to GraphQL Federation, a set of tools developed by Apollo that extends the power of GraphQL. GraphQL Federation allows you to create a unified, single schema from multiple, separate GraphQL services.
By the end of this tutorial, you will understand what GraphQL Federation is, why it's beneficial, and how to set up a federated architecture.
Basic knowledge of GraphQL and Node.js are necessary to follow along with this tutorial. Familiarity with Apollo Server would also be beneficial.
GraphQL Federation allows for a microservices architecture where each team can work on their own services without having to know the entire schema. It enables schema separation, while still allowing queries across multiple services.
The main concepts behind GraphQL Federation are:
Let's look at a basic example of setting up a federated schema. In this scenario, we have two services: one for users and one for reviews.
// Define the schema
const typeDefs = gql`
type User @key(fields: "id") {
id: ID!
username: String
}
`;
// Define the resolvers
const resolvers = {
User: {
__resolveReference(user, { dataSources }) {
return dataSources.usersAPI.getUser(user.id);
},
},
};
// Create the Apollo Server
const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }]),
dataSources: () => ({
usersAPI: new UsersAPI(),
}),
});
server.listen({ port: 4001 }).then(({ url }) => {
console.log(`User service ready at ${url}`);
});
In this example, the User type has a @key
directive which specifies the field that can be used to fetch a User object across services.
// Define the schema
const typeDefs = gql`
type Review @key(fields: "id") {
id: ID!
user: User
}
extend type User @key(fields: "id") {
id: ID! @external
reviews: [Review]
}
`;
// Define the resolvers
const resolvers = {
Review: {
user(review) {
return { __typename: "User", id: review.userId };
},
},
User: {
reviews(user) {
return reviewsAPI.getReviewsForUser(user.id);
},
},
};
// Create the Apollo Server
const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }]),
dataSources: () => ({
reviewsAPI: new ReviewsAPI(),
}),
});
server.listen({ port: 4002 }).then(({ url }) => {
console.log(`Review service ready at ${url}`);
});
In this example, the Review type extends the User type, meaning it can use the User's fields in its resolvers.
In this tutorial, we have covered the basics of GraphQL Federation, how to set it up and the benefits it provides in a microservices architecture. To continue your learning, consider exploring how to handle errors and set up authorization and authentication in a federated architecture.
Exercise 1: Set up a federated schema with three services: Users, Reviews, and Products.
Exercise 2: Extend the User type in the Reviews service to include a reviewsCount
field that returns the number of reviews for a user.
Exercise 3: Extend the User type in the Products service to include a productsCount
field that returns the number of products a user has listed.
Remember, practice is critical in mastering any new subject. Keep exploring GraphQL Federation and experimenting with different schemas and services.