What is @apollo/gateway?
@apollo/gateway is a package that allows you to build a federated data graph by composing multiple GraphQL services into a single unified API. It is part of Apollo's Federation architecture, which enables you to manage and scale your GraphQL services independently while providing a seamless experience for clients.
What are @apollo/gateway's main functionalities?
Creating a Gateway
This code sample demonstrates how to create an Apollo Gateway that composes multiple GraphQL services into a single unified API. The `serviceList` array contains the list of services to be federated.
const { ApolloGateway } = require('@apollo/gateway');
const { ApolloServer } = require('apollo-server');
const gateway = new ApolloGateway({
serviceList: [
{ name: 'accounts', url: 'http://localhost:4001' },
{ name: 'products', url: 'http://localhost:4002' },
{ name: 'reviews', url: 'http://localhost:4003' }
]
});
const server = new ApolloServer({ gateway, subscriptions: false });
server.listen().then(({ url }) => {
console.log(`π Server ready at ${url}`);
});
Customizing Service Health Checks
This code sample shows how to enable service health checks in Apollo Gateway. The `serviceHealthCheck` option ensures that the gateway periodically checks the health of the federated services.
const { ApolloGateway } = require('@apollo/gateway');
const { ApolloServer } = require('apollo-server');
const gateway = new ApolloGateway({
serviceList: [
{ name: 'accounts', url: 'http://localhost:4001' },
{ name: 'products', url: 'http://localhost:4002' },
{ name: 'reviews', url: 'http://localhost:4003' }
],
serviceHealthCheck: true
});
const server = new ApolloServer({ gateway, subscriptions: false });
server.listen().then(({ url }) => {
console.log(`π Server ready at ${url}`);
});
Using Managed Federation
This code sample demonstrates how to use managed federation with Apollo Gateway. The `experimental_pollInterval` option allows the gateway to periodically poll for updates to the federated services' schemas.
const { ApolloGateway } = require('@apollo/gateway');
const { ApolloServer } = require('apollo-server');
const gateway = new ApolloGateway({
serviceList: [
{ name: 'accounts', url: 'http://localhost:4001' },
{ name: 'products', url: 'http://localhost:4002' },
{ name: 'reviews', url: 'http://localhost:4003' }
],
experimental_pollInterval: 10000
});
const server = new ApolloServer({ gateway, subscriptions: false });
server.listen().then(({ url }) => {
console.log(`π Server ready at ${url}`);
});
Other packages similar to @apollo/gateway
graphql-tools
graphql-tools is a package that provides a set of utilities to build and manipulate GraphQL schemas in JavaScript. It allows you to create a unified schema from multiple GraphQL services, similar to Apollo Gateway. However, it does not provide the same level of built-in support for federated services and managed federation as Apollo Gateway.
graphql-mesh
graphql-mesh is a package that allows you to create a unified GraphQL schema from multiple sources, including REST APIs, gRPC services, and other GraphQL APIs. It provides a flexible and extensible way to integrate different data sources into a single GraphQL API. Compared to Apollo Gateway, graphql-mesh offers more versatility in terms of the types of data sources it can integrate, but it may require more configuration and setup.
Apollo Gateway
This package provides utilities for combining multiple GraphQL microservices into a single GraphQL endpoint.
Each microservice should implement the federation schema specification. This can be done either through Apollo Federation or a variety of other open source products.
For complete documentation, see the Apollo Gateway API reference.
Usage
const { ApolloServer } = require("apollo-server");
const { ApolloGateway } = require("@apollo/gateway");
const gateway = new ApolloGateway({
serviceList: [
{ name: "accounts", url: "http://localhost:4001/graphql" },
]
});
const server = new ApolloServer({ gateway });
server.listen().then(({ url }) => {
console.log(`π Server ready at ${url}`);
});