0Pricing
GraphQL APIs with Spring Boot · Lekcja

Konfiguracja i zarządzanie Gateway

Skonfiguruj i wdroż Apollo Gateway, aby komponować sfederowane podgrafy i udostępniać je jako jeden ujednolicony graf.

Konfiguracja i zarządzanie Gateway to bezpłatna lekcja GraphQL APIs with Spring Boot na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej GraphQL APIs with Spring Boot, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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:

  1. The gateway receives the query.
  2. It analyzes the query against the composed supergraph schema.
  3. It breaks down the query into smaller parts, determining which subgraph owns which piece of data.
  4. It sends sub-queries to the relevant subgraphs.
  5. 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-server

Then, 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.js

The 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.

Często zadawane pytania

Czy lekcja „Konfiguracja i zarządzanie Gateway” jest bezpłatna?

Tak — pełny tekst „Konfiguracja i zarządzanie Gateway” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu GraphQL APIs with Spring Boot, przejdź na CoddyKit PRO. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.

Co nauczysz się w „Konfiguracja i zarządzanie Gateway”?

Skonfiguruj i wdroż Apollo Gateway, aby komponować sfederowane podgrafy i udostępniać je jako jeden ujednolicony graf. Ćwiczysz GraphQL APIs with Spring Boot z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć GraphQL APIs with Spring Boot?

Nie wymagamy żadnego doświadczenia. GraphQL APIs with Spring Boot w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Konfiguracja i zarządzanie Gateway”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji GraphQL APIs with Spring Boot?

Tak. Każda lekcja GraphQL APIs with Spring Boot zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wprowadzenie do Apollo Federation
  2. Tworzenie sfederowanych podgrafów
  3. Konfiguracja i zarządzanie Gateway
  4. Referencje encji i dyrektywa @key
← Powrót do GraphQL APIs with Spring Boot