Apollo Federation helps combine many small GraphQL services into one big service. It makes it easy to work together on data from different places.
Apollo Federation concepts in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
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.
User entity with an @key and extending another type Review to link to User.type User @key(fields: "id") { id: ID! username: String } extend type Review { author: User }
Product entity identified by upc and extends the Query type to add a new field.type Product @key(fields: "upc") { upc: String! name: String } extend type Query { topProducts(first: Int): [Product] }
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.
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 }
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.
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.
Practice
Solution
Step 1: Understand Apollo Federation's role
Apollo Federation is designed to combine multiple GraphQL services into one unified API.Step 2: Compare options
Options B, C, and D describe unrelated tasks. Only To combine multiple GraphQL services into a single API correctly describes Apollo Federation's purpose.Final Answer:
To combine multiple GraphQL services into a single API -> Option CQuick Check:
Apollo Federation = combine services [OK]
- Confusing Federation with database schema design
- Thinking Federation replaces REST APIs directly
- Assuming Federation optimizes SQL queries
Solution
Step 1: Recall the directive for unique identifiers
In Apollo Federation, the@keydirective marks the unique identifier fields for an entity.Step 2: Check syntax correctness
type User @key(fields: "id") { id: ID! name: String } uses@key(fields: "id"), which is the correct syntax. Other options use incorrect directives.Final Answer:
type User @key(fields: "id") { id: ID! name: String } -> Option BQuick Check:
@key directive = unique ID marker [OK]
- Using @unique or @id instead of @key
- Missing quotes around field names
- Confusing @key with database primary key syntax
extend type Product @key(fields: "upc") { upc: String @external price: Int }What does the
extend type keyword do here?Solution
Step 1: Understand 'extend type' in Apollo Federation
Theextend typekeyword adds fields to a type defined in another service.Step 2: Analyze the example
The Product type is extended to add thepricefield, whileupcis marked as external, meaning it comes from the original service.Final Answer:
Adds the price field to an existing Product type from another service -> Option AQuick Check:
extend type = add fields to existing type [OK]
- Thinking extend type creates a new type
- Confusing @external with local fields
- Assuming extend type removes fields
type Review @key(fields: "id") { id: ID! body: String author: User }Assuming User is defined in another service, what is missing?
Solution
Step 1: Understand referencing external entities
In Apollo Federation, to reference an entity type like User from another service, the schema must explicitly extend that type with its @key directive:extend type User @key(fields: "id") { id: ID! }.Step 2: Analyze the snippet
The Review type references User via author: User but lacks the required extend declaration for User, which is necessary for the gateway to know this service can resolve User.Final Answer:
The User type should be imported explicitly -> Option AQuick Check:
Reference entities -> extend type @key [OK]
- Marking author field with @provides or @requires
- Marking author field as @external
- Using extend keyword for Review type
Accounts defining type User @key(fields: "id") { id: ID! name: String } and Reviews extending User with extend type User @key(fields: "id") { id: ID! reviews: [Review] }. How does Apollo Federation resolve the User entity across these services?Solution
Step 1: Understand @key usage in federation
The@keydirective identifies the unique field used to match entities across services.Step 2: Analyze entity resolution
Apollo Federation merges types with the same @key field by matching their unique identifiers, combining fields from both services.Final Answer:
It merges User types by matching the @key field 'id' across services -> Option DQuick Check:
@key fields unify entities across services [OK]
- Thinking types are duplicated instead of merged
- Assuming extend type is ignored
- Believing manual merging is required
