0
0
GraphQLquery~30 mins

Gateway composition in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Assign composedSchema to gatewaySchema and export it as default.