Bird
Raised Fist0
GraphQLquery~20 mins

Apollo Federation concepts in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Apollo Federation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary purpose of Apollo Federation?
Apollo Federation allows multiple GraphQL services to work together as one. What is its main goal?
ATo combine multiple GraphQL services into a single graph for clients to query seamlessly
BTo convert SQL databases into GraphQL schemas
CTo cache GraphQL queries on the client side automatically
DTo replace REST APIs with a single GraphQL service
Attempts:
2 left
💡 Hint
Think about how multiple teams can build parts of a graph independently.
query_result
intermediate
2:00remaining
What is the output of this federated query?
Given two services: Product service with type Product { id, name } and Review service extending Product with { reviews }, what is the result of querying { product(id: "1") { id name reviews { body } } }?
GraphQL
query {
  product(id: "1") {
    id
    name
    reviews {
      body
    }
  }
}
A{ "data": { "product": { "id": "1", "name": null, "reviews": [] } } }
B{ "data": { "product": { "id": "1", "name": "Table", "reviews": [{ "body": "Great!" }] } } }
C{ "errors": [{ "message": "Field 'reviews' not found on type 'Product'" }] }
D{ "data": { "product": null } }
Attempts:
2 left
💡 Hint
Remember that Review service extends Product to add reviews.
📝 Syntax
advanced
2:00remaining
Which SDL snippet correctly defines an entity in Apollo Federation?
Choose the correct way to define a Product entity with a key field 'id' in Apollo Federation SDL.
GraphQL
type Product {
  id: ID!
  name: String
}
A
type Product @key(fields: "id") {
  id: ID!
  name: String
}
B
extend type Product @key(fields: "id") {
  id: ID! @external
  name: String
}
C
type Product @entity(fields: "id") {
  id: ID!
  name: String
}
D
type Product @key(id) {
  id: ID!
  name: String
}
Attempts:
2 left
💡 Hint
The @key directive marks the primary key field for the entity.
optimization
advanced
2:00remaining
How does Apollo Gateway optimize resolving entities across services?
Apollo Gateway receives a query that requires data from multiple services. How does it optimize fetching entities?
AIt caches all data indefinitely to avoid any network calls
BIt sends separate requests for each field to each service
CIt batches entity requests by keys to minimize network calls to subgraphs
DIt merges all schemas into one and queries only one service
Attempts:
2 left
💡 Hint
Think about how to reduce repeated calls for the same entity.
🔧 Debug
expert
3:00remaining
Why does this federated query fail with an error?
Given two services: User service defines type User @key(fields: "id") { id: ID! name: String }, and Order service extends User with extend type User @key(fields: "id") { id: ID! orders: [Order] }, a query requests user { id name orders { id } }. The error says: "Cannot query field 'orders' on type 'User'". What is the cause?
AThe User service must define the 'orders' field, not the Order service
BThe query is missing the @external directive on the 'id' field in the Order service
CThe 'orders' field must be marked as @requires(fields: "id") in the Order service
DThe User type in the gateway schema does not include the 'orders' field because the Order service did not properly extend the User entity
Attempts:
2 left
💡 Hint
Check how the User type is extended and if the gateway schema includes the extension.

Practice

(1/5)
1. What is the primary purpose of Apollo Federation in GraphQL?
easy
A. To replace REST APIs with GraphQL
B. To create a new database schema
C. To combine multiple GraphQL services into a single API
D. To optimize SQL queries automatically

Solution

  1. Step 1: Understand Apollo Federation's role

    Apollo Federation is designed to combine multiple GraphQL services into one unified API.
  2. 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.
  3. Final Answer:

    To combine multiple GraphQL services into a single API -> Option C
  4. Quick Check:

    Apollo Federation = combine services [OK]
Hint: Federation = combining services into one API [OK]
Common Mistakes:
  • Confusing Federation with database schema design
  • Thinking Federation replaces REST APIs directly
  • Assuming Federation optimizes SQL queries
2. Which of the following is the correct syntax to mark a unique identifier for an entity in Apollo Federation?
easy
A. type User @unique(fields: "id") { id: ID! name: String }
B. type User @key(fields: "id") { id: ID! name: String }
C. type User @id(fields: "id") { id: ID! name: String }
D. type User @primary(fields: "id") { id: ID! name: String }

Solution

  1. Step 1: Recall the directive for unique identifiers

    In Apollo Federation, the @key directive marks the unique identifier fields for an entity.
  2. 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.
  3. Final Answer:

    type User @key(fields: "id") { id: ID! name: String } -> Option B
  4. Quick Check:

    @key directive = unique ID marker [OK]
Hint: Use @key to mark unique entity IDs [OK]
Common Mistakes:
  • Using @unique or @id instead of @key
  • Missing quotes around field names
  • Confusing @key with database primary key syntax
3. Given the following schema in a federated service:
extend type Product @key(fields: "upc") { upc: String @external price: Int }

What does the extend type keyword do here?
medium
A. Adds the price field to an existing Product type from another service
B. Removes the upc field from Product type
C. Defines a new Product type with fields upc and price
D. Creates a local copy of Product type without federation

Solution

  1. Step 1: Understand 'extend type' in Apollo Federation

    The extend type keyword adds fields to a type defined in another service.
  2. Step 2: Analyze the example

    The Product type is extended to add the price field, while upc is marked as external, meaning it comes from the original service.
  3. Final Answer:

    Adds the price field to an existing Product type from another service -> Option A
  4. Quick Check:

    extend type = add fields to existing type [OK]
Hint: extend type adds fields to existing types [OK]
Common Mistakes:
  • Thinking extend type creates a new type
  • Confusing @external with local fields
  • Assuming extend type removes fields
4. Identify the error in this federated schema snippet:
type Review @key(fields: "id") { id: ID! body: String author: User }

Assuming User is defined in another service, what is missing?
medium
A. The User type should be imported explicitly
B. The author field should be marked with @external
C. The author field should be marked with @provides or @requires
D. The Review type should use extend keyword

Solution

  1. 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! }.
  2. 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.
  3. Final Answer:

    The User type should be imported explicitly -> Option A
  4. Quick Check:

    Reference entities -> extend type @key [OK]
Hint: Extend referenced entities with @key [OK]
Common Mistakes:
  • Marking author field with @provides or @requires
  • Marking author field as @external
  • Using extend keyword for Review type
5. You have two services: 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?
hard
A. It requires manual merging of User data in the gateway
B. It duplicates User types separately in each service
C. It ignores the extend type and uses only the Accounts User type
D. It merges User types by matching the @key field 'id' across services

Solution

  1. Step 1: Understand @key usage in federation

    The @key directive identifies the unique field used to match entities across services.
  2. Step 2: Analyze entity resolution

    Apollo Federation merges types with the same @key field by matching their unique identifiers, combining fields from both services.
  3. Final Answer:

    It merges User types by matching the @key field 'id' across services -> Option D
  4. Quick Check:

    @key fields unify entities across services [OK]
Hint: Entities merge by matching @key fields [OK]
Common Mistakes:
  • Thinking types are duplicated instead of merged
  • Assuming extend type is ignored
  • Believing manual merging is required