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
Build a GraphQL Gateway with Schema Composition
📖 Scenario: You are building a GraphQL gateway that combines two separate services: Users and Products. Each service has its own schema. Your gateway will compose these schemas into one unified schema so clients can query both users and products from a single endpoint.
🎯 Goal: Create a GraphQL gateway schema by composing two service schemas: User and Product. You will define the initial schemas, add configuration for schema stitching, compose the schemas, and finalize the gateway schema.
📋 What You'll Learn
Create two GraphQL schemas as strings: userSchema and productSchema with specified types
Add a configuration variable gatewayConfig to hold schema stitching options
Compose the two schemas into one unified schema using makeExecutableSchema and mergeSchemas
Export the final composed schema as gatewaySchema
💡 Why This Matters
🌍 Real World
GraphQL gateway composition is used in microservice architectures to combine multiple GraphQL APIs into a single endpoint for clients.
💼 Career
Understanding schema composition is essential for backend developers working with GraphQL in distributed systems and API gateways.
Progress0 / 4 steps
1
Define User and Product schemas
Create two GraphQL schema strings called userSchema and productSchema. userSchema should define a User type with fields id: ID! and name: String!. productSchema should define a Product type with fields id: ID! and title: String!.
GraphQL
Hint
Define userSchema and productSchema as template strings with the exact type definitions.
2
Add gateway configuration for schema stitching
Create a configuration object called gatewayConfig with a property schemas that is an array containing userSchema and productSchema.
GraphQL
Hint
Define gatewayConfig as an object with a schemas property that holds both schema strings.
3
Compose schemas into one executable schema
Use makeExecutableSchema from @graphql-tools/schema to create executable schemas from userSchema and productSchema. Then use mergeSchemas from @graphql-tools/merge to combine them into one schema. Assign the result to a variable called composedSchema.
GraphQL
Hint
Create executable schemas from the schema strings, then merge them into one schema.
4
Export the final gateway schema
Export the composed schema by assigning composedSchema to a constant called gatewaySchema and export it as the default export.
GraphQL
Hint
Assign composedSchema to gatewaySchema and export it as default.
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
Step 1: Understand gateway composition
Gateway composition merges several GraphQL services into one unified API.
Step 2: Identify the main benefit
This allows clients to send requests to a single endpoint instead of multiple services.
Final Answer:
To combine multiple GraphQL services into a single API endpoint -> Option A
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'] })
ApolloGateway expects an object with a 'services' array containing objects with 'name' and 'url'.
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.
Final Answer:
new ApolloGateway({ services: [{ name: 'users', url: 'http://users' }, { name: 'products', url: 'http://products' }] }) -> Option C
Quick Check:
ApolloGateway needs services array with name and url [OK]
Hint: Use services array with name and url objects in ApolloGateway [OK]