게이트웨이 설정 및 관리
Apollo Gateway를 구성하고 배포해 페더레이션 서브그래프를 하나의 통합 그래프로 구성하고 노출합니다.
게이트웨이 설정 및 관리은(는) CoddyKit의 무료 GraphQL APIs with Spring Boot 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 GraphQL APIs with Spring Boot 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. GraphQL APIs with Spring Boot 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Gateway: The Unified API
In GraphQL Federation, the Apollo Gateway acts as the central entry point for all client applications. Think of it as the brain that understands your entire supergraph.
It takes multiple independent GraphQL services (called subgraphs) and combines their schemas into one unified, client-facing schema.
Why a Gateway is Essential
With a microservices architecture, you might have many backend services, each with its own GraphQL API. Without a gateway, clients would need to know about and query each service individually.
- Unified Endpoint: Clients interact with a single GraphQL API.
- Schema Composition: The gateway automatically stitches together the schemas from all subgraphs.
- Simplified Client Logic: Clients don't need to know which service owns which data.
How the Gateway Works
When a client sends a query to the gateway, here's what happens:
- The gateway receives the query.
- It analyzes the query against the composed supergraph schema.
- It breaks down the query into smaller parts, determining which subgraph owns which piece of data.
- It sends sub-queries to the relevant subgraphs.
- It then combines the results from these subgraphs into a single response for the client.
Gateway Implementations
While your subgraphs can be built with Spring Boot (or any other framework), the gateway itself is often implemented using Apollo's own tools.
The most common and robust implementation is the Apollo Server Gateway, which is a Node.js-based server designed specifically for federation. This lesson will focus on setting up this standard gateway.
Basic Gateway Setup (Node.js)
Let's set up a minimal Apollo Gateway. First, create a new Node.js project and install the necessary packages:
npm init -y
npm install @apollo/gateway apollo-serverThen, create an index.js file:
const { ApolloGateway, RemoteGraphQLDataSource } = require("@apollo/gateway");
const { ApolloServer } = require("apollo-server");
const gateway = new ApolloGateway({
serviceList: [
{ name: "products", url: "http://localhost:4001/graphql" },
],
buildService({ name, url }) {
return new RemoteGraphQLDataSource({ url });
},
});
const server = new ApolloServer({ gateway });
server.listen({
port: process.env.PORT || 4000
}).then(({ url }) => {
console.log(`🚀 Gateway ready at ${url}`);
});Configuring Subgraphs
In the previous code, the serviceList array is crucial. It tells the gateway which subgraphs exist and where to find them. Each object in the array needs two properties:
name: A unique identifier for the subgraph (e.g.,"products").url: The endpoint where the subgraph's GraphQL API is running (e.g.,"http://localhost:4001/graphql").
Adding More Subgraphs
As you develop more microservices with GraphQL APIs, you simply add them to the serviceList. The gateway will automatically compose their schemas into the unified graph.
Here's how you'd add a reviews subgraph running on port 4002:
const { ApolloGateway, RemoteGraphQLDataSource } = require("@apollo/gateway");
const { ApolloServer } = require("apollo-server");
const gateway = new ApolloGateway({
serviceList: [
{ name: "products", url: "http://localhost:4001/graphql" },
{ name: "reviews", url: "http://localhost:4002/graphql" },
],
buildService({ name, url }) {
return new RemoteGraphQLDataSource({ url });
},
});
const server = new ApolloServer({ gateway });
server.listen({
port: process.env.PORT || 4000
}).then(({ url }) => {
console.log(`🚀 Gateway ready at ${url}`);
});Running and Accessing the Gateway
Once your index.js is set up, you can start the gateway:
node index.jsThe console will output the URL where your gateway is running, typically http://localhost:4000/. Open this URL in your browser to access the GraphQL Playground or GraphiQL interface provided by Apollo Server.
Querying the Unified Graph
Clients will send their GraphQL queries directly to the gateway's URL. The gateway handles all the internal routing and data fetching from the subgraphs.
For example, a query combining data from both a products and reviews subgraph might look like this:
query GetProductDetails {
product(id: "prod-1") {
name
price
reviews {
id
rating
comment
}
}
}Gateway Management Tips
Beyond initial setup, managing your gateway involves several best practices:
- Deployment: Deploy the gateway as a standalone service, often alongside your subgraphs.
- Monitoring: Use tools to monitor its performance, error rates, and latency.
- Scaling: The gateway can be scaled horizontally to handle increased client traffic.
- Schema Changes: Ensure your gateway is configured to pick up schema updates from subgraphs reliably (e.g., via Apollo Studio or a local schema registry).
Quick Check: Gateway Role
You've learned about the Apollo Gateway and its role in federation.
Recap: Gateway Essentials
Congratulations! You've learned about the Apollo Gateway, a crucial component in GraphQL Federation. It provides a unified API endpoint for clients, seamlessly composing schemas from various subgraphs and routing queries effectively.
By configuring the serviceList, your gateway becomes the single source of truth for your entire supergraph, simplifying client interactions and enabling a robust microservices architecture.
AI 튜터와 함께 GraphQL APIs with Spring Boot을(를) 배우세요 — 무료
브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.
- 코스
- 12
- 레슨
- 48
자주 묻는 질문
“게이트웨이 설정 및 관리” 강의는 무료인가요?
네 — “게이트웨이 설정 및 관리” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 GraphQL APIs with Spring Boot 강의 전체를 잠금 해제할 수 있습니다. GraphQL APIs with Spring Boot 강의에는 총 4개의 강의가 포함되어 있습니다.
“게이트웨이 설정 및 관리”에서 뭘 배우나요?
Apollo Gateway를 구성하고 배포해 페더레이션 서브그래프를 하나의 통합 그래프로 구성하고 노출합니다. 브라우저에서 직접 실행하는 실습 코드로 GraphQL APIs with Spring Boot을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
GraphQL APIs with Spring Boot을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 GraphQL APIs with Spring Boot은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“게이트웨이 설정 및 관리” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 GraphQL APIs with Spring Boot 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 GraphQL APIs with Spring Boot 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Apollo Federation 소개
- 페더레이션 서브그래프 구축
- 게이트웨이 설정 및 관리
- 엔터티 참조와 @key 지시문