Apollo Federation helps combine many small GraphQL services into one big service. It makes it easy to work together on data from different places.
0
0
Apollo Federation concepts in GraphQL
Introduction
When you have multiple teams working on different parts of your data and want to combine their work.
When you want to split a big GraphQL API into smaller, manageable parts.
When you want to share data between different GraphQL services without merging them manually.
When you want to add new features or data sources without changing the whole API.
When you want to keep your GraphQL services independent but still provide a single API to users.
Syntax
GraphQL
type Product @key(fields: "id") { id: ID! name: String price: Float } extend type Query { product(id: ID!): Product }
@key marks the primary field to identify an entity across services.
extend type is used to add fields to types defined in other services.
Examples
This example shows a
User entity with an @key and extending another type Review to link to User.GraphQL
type User @key(fields: "id") { id: ID! username: String } extend type Review { author: User }
Defines a
Product entity identified by upc and extends the Query type to add a new field.GraphQL
type Product @key(fields: "upc") { upc: String! name: String } extend type Query { topProducts(first: Int): [Product] }
Sample Program
This schema shows two entities, Book and Author, each with a unique key. It also extends the Query type to fetch books and authors by their keys.
GraphQL
type Book @key(fields: "isbn") { isbn: String! title: String author: Author } type Author @key(fields: "id") { id: ID! name: String } extend type Query { book(isbn: String!): Book author(id: ID!): Author }
OutputSuccess
Important Notes
Each service defines its own part of the schema and uses @key to tell Apollo Federation how to identify entities.
Federation allows services to share and extend types without conflicts.
Use extend type to add fields to types owned by other services.
Summary
Apollo Federation helps combine multiple GraphQL services into one API.
Use @key to mark unique identifiers for entities.
Use extend type to add fields to types from other services.