Bird
Raised Fist0
GraphQLquery~20 mins

Gateway composition 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
🎖️
Gateway Composition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this federated query?

Given two GraphQL services composed via a gateway, the Product type is extended in the Reviews service. Consider the following query executed against the gateway:

{ product(id: "1") { id name reviews { body author { name } } } }

Assuming the Product service returns { id: "1", name: "Table" } and the Reviews service returns two reviews with authors, what is the expected shape of the response?

A{"data":{"product":{"id":"1","name":"Table"}}}
B{"data":{"product":{"id":"1","name":"Table","reviews":[{"body":"Great!","author":{"name":"Alice"}},{"body":"Good quality","author":{"name":"Bob"}}]}}}
C{"data":{"product":{"id":"1","reviews":[{"body":"Great!","author":{"name":"Alice"}},{"body":"Good quality","author":{"name":"Bob"}}]}}}
D{"errors":[{"message":"Field 'reviews' not found on type 'Product'"}]}
Attempts:
2 left
💡 Hint

Remember that the gateway merges data from both services, so extended fields appear in the final response.

🧠 Conceptual
intermediate
1:30remaining
Which directive is essential for gateway composition in Apollo Federation?

In Apollo Federation, to indicate that a type or field is extended from another service, which directive must be used?

A@external
B@key
C@extends
D@provides
Attempts:
2 left
💡 Hint

Think about how a service declares it is adding fields to a type defined elsewhere.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this federated schema snippet

Consider this schema snippet from a federated service:

extend type User @key(fields: "id") { id: ID! name: String! @external posts: [Post] }

What is the syntax error here?

AMissing closing brace '}' for the type definition
BMissing @external directive on 'posts' field
CThe @key directive must be on the base type, not on an extension
DThe 'posts' field must have a non-nullable type
Attempts:
2 left
💡 Hint

Check the braces carefully in the type definition.

optimization
advanced
1:30remaining
How to reduce query latency in gateway composition?

When a gateway composes multiple services, queries can become slow due to multiple network calls. Which approach best reduces latency?

AIncrease the timeout for each subservice request
BRemove @key directives to simplify schema
CDisable schema stitching and use separate endpoints
DBatch requests to subservices and cache results at the gateway
Attempts:
2 left
💡 Hint

Think about how to minimize the number of calls and reuse data.

🔧 Debug
expert
2:30remaining
Why does this federated query return a null for an extended field?

A federated gateway query requests an extended field reviews on Product. The base service returns the product data correctly, but reviews is always null. The Reviews service schema has:

extend type Product @key(fields: "id") { id: ID! reviews: [Review] }

And the resolver for reviews uses the id from the parent. What is the most likely cause?

AThe <code>id</code> field is not marked with @external in the extension
BThe <code>Product</code> type is not marked with @extends in the Reviews service
CThe <code>reviews</code> field is missing the @requires directive
DThe gateway is not forwarding headers to the Reviews service
Attempts:
2 left
💡 Hint

Check how fields from the base service are referenced in extensions.

Practice

(1/5)
1. What is the main purpose of gateway composition in GraphQL?
easy
A. To combine multiple GraphQL services into a single API endpoint
B. To create multiple endpoints for each service
C. To replace GraphQL with REST APIs
D. To disable client requests

Solution

  1. Step 1: Understand gateway composition

    Gateway composition merges several GraphQL services into one unified API.
  2. Step 2: Identify the main benefit

    This allows clients to send requests to a single endpoint instead of multiple services.
  3. Final Answer:

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

    Gateway composition = single API endpoint [OK]
Hint: Gateway composition means one API endpoint for many services [OK]
Common Mistakes:
  • Thinking it creates multiple endpoints
  • Confusing with REST APIs
  • Believing it disables client requests
2. Which of the following is the correct way to initialize ApolloGateway with two services named 'users' and 'products'?
easy
A. ApolloGateway({ services: ['users', 'products'] })
B. new ApolloGateway(services: ['users', 'products'])
C. new ApolloGateway({ services: [{ name: 'users', url: 'http://users' }, { name: 'products', url: 'http://products' }] })
D. new ApolloGateway({ endpoints: ['users', 'products'] })

Solution

  1. Step 1: Recall ApolloGateway initialization syntax

    ApolloGateway expects an object with a 'services' array containing objects with 'name' and 'url'.
  2. Step 2: Match the correct syntax

    new ApolloGateway({ services: [{ name: 'users', url: 'http://users' }, { name: 'products', url: 'http://products' }] }) correctly uses new ApolloGateway with services array of objects including name and url.
  3. Final Answer:

    new ApolloGateway({ services: [{ name: 'users', url: 'http://users' }, { name: 'products', url: 'http://products' }] }) -> Option C
  4. Quick Check:

    ApolloGateway needs services array with name and url [OK]
Hint: Use services array with name and url objects in ApolloGateway [OK]
Common Mistakes:
  • Passing services as simple string array
  • Using 'endpoints' instead of 'services'
  • Omitting 'new' keyword
3. Given this ApolloGateway setup:
const gateway = new ApolloGateway({
  services: [
    { name: 'users', url: 'http://localhost:4001/graphql' },
    { name: 'products', url: 'http://localhost:4002/graphql' }
  ],
  __exposeQueryPlanExperimental: false
});
What will be the result if a client queries for a product's name and its owner's username?
medium
A. The gateway throws an error because __exposeQueryPlanExperimental is false
B. The gateway returns data only from 'products' service ignoring 'users'
C. The gateway returns empty data because services are unreachable
D. The gateway fetches data from both 'products' and 'users' services and returns combined result

Solution

  1. Step 1: Understand gateway data fetching

    The gateway composes schemas and fetches data from all relevant services for the query.
  2. Step 2: Analyze the query involving product and user data

    Since the query asks for product and owner username, gateway calls both 'products' and 'users' services.
  3. Final Answer:

    The gateway fetches data from both 'products' and 'users' services and returns combined result -> Option D
  4. Quick Check:

    Gateway combines data from multiple services [OK]
Hint: Gateway merges data from all services needed by query [OK]
Common Mistakes:
  • Assuming gateway fetches from only one service
  • Confusing __exposeQueryPlanExperimental with data fetching
  • Assuming error without service failure
4. You wrote this ApolloGateway code but get an error:
const gateway = new ApolloGateway({
  services: [
    { name: 'users', url: 'http://localhost:4001/graphql' },
    { name: 'products' }
  ]
});
What is the likely cause of the error?
medium
A. Missing 'url' property for the 'products' service
B. Using 'services' instead of 'endpoints'
C. ApolloGateway requires only one service
D. The 'name' property should be 'serviceName'

Solution

  1. Step 1: Check service definitions

    Each service object must have both 'name' and 'url' properties.
  2. Step 2: Identify missing property

    The 'products' service lacks the 'url' property, causing the error.
  3. Final Answer:

    Missing 'url' property for the 'products' service -> Option A
  4. Quick Check:

    Each service needs name and url [OK]
Hint: Always include 'url' for each service in ApolloGateway [OK]
Common Mistakes:
  • Confusing 'services' with 'endpoints'
  • Thinking ApolloGateway supports single service only
  • Using wrong property names
5. You want to disable subscriptions in your ApolloGateway setup while composing three services. Which is the correct way to do this?
hard
A. Set subscriptions: false inside each service definition in ApolloGateway
B. Pass { subscriptions: false } in ApolloServer constructor, not in ApolloGateway
C. Add subscriptions: false in ApolloGateway constructor options
D. Disable subscriptions by removing the 'subscriptions' field from the schema

Solution

  1. Step 1: Understand where to disable subscriptions

    Subscriptions are disabled in ApolloServer, not ApolloGateway.
  2. Step 2: Apply correct configuration

    Pass { subscriptions: false } as an option to ApolloServer constructor wrapping the gateway.
  3. Final Answer:

    Pass { subscriptions: false } in ApolloServer constructor, not in ApolloGateway -> Option B
  4. Quick Check:

    Disable subscriptions in ApolloServer, not ApolloGateway [OK]
Hint: Disable subscriptions in ApolloServer, not ApolloGateway [OK]
Common Mistakes:
  • Trying to disable subscriptions inside ApolloGateway
  • Removing schema fields instead of config
  • Setting subscriptions false per service