API 参考文档:@apollo/gateway
本 API 参考文档介绍了从@apollo/gateway包的导出。此包使您能够将Apollo Server用作联邦超级图网关。有关更多信息,请参阅使用 Apollo Server 实现 gateway。
我们建议所有 supergraphs 都使用 GraphOS Router而不是这个基于 Node.js 的网关。请参阅选择路由库。
class ApolloGateway
Apollo Server 联邦网关实现的核心类。有关使用 ApolloGateway 的示例,请参阅使用 Apollo Server 实现 gateway。
方法
构造函数
返回一个初始化的ApolloGateway实例,您可以将它作为gateway配置选项传递给ApolloServer构造函数,如以下示例所示:
const server = new ApolloServer({gateway: new ApolloGateway({serviceList: [// ...],}),});
const server = new ApolloServer({gateway: new ApolloGateway({serviceList: [// ...],}),});
接受一个options对象作为参数。以下描述了该对象支持的字段。
示例
提供buildService函数以配置子图检索器
const gateway = new ApolloGateway({serviceList: [{ name: 'products', url: 'https://products-service.dev/graphql' },{ name: 'reviews', url: 'https://reviews-service.dev/graphql' },],introspectionHeaders: {Authorization: 'Bearer abc123',},});
const gateway = new ApolloGateway({serviceList: [{ name: 'products', url: 'https://products-service.dev/graphql' },{ name: 'reviews', url: 'https://reviews-service.dev/graphql' },],introspectionHeaders: {Authorization: 'Bearer abc123',},});
配置管理的联盟.fetcher
如果您向buildService函数提供给ApolloGateway构造函数,则此函数会为您的每个图的子图调用一次。此函数可以返回一个具有自定义fetcher的RemoteGraphQLDataSource,该检索器随后用于所有与子图的通信:
const gateway = new ApolloGateway({buildService({ url, name }) {return new (class extends RemoteGraphQLDataSource {fetcher = require('make-fetch-happen').defaults({onRetry() {console.log('We will retry!');},});})({url,name,});},});
const gateway = new ApolloGateway({buildService({ url, name }) {return new (class extends RemoteGraphQLDataSource {fetcher = require('make-fetch-happen').defaults({onRetry() {console.log('We will retry!');},});})({url,name,});},});
配置管理的联盟.fetcher
此managed federation配置只配置了您的网关用于从Apollo获取
配置子图检索器
const gateway = new ApolloGateway({fetcher: require('make-fetch-happen').defaults({onRetry() {console.log('We will retry!');},}),});
const gateway = new ApolloGateway({fetcher: require('make-fetch-happen').defaults({onRetry() {console.log('We will retry!');},}),});
选项
| 名称 类型 | 描述 |
|---|---|
| 您可以使用此选项将您的supergraph模式提供给网关。您可以选择将它作为一个 当 当
当 如果您正在使用托管联邦,请不要提供此字段。 如果您没有使用托管联邦,则此字段或 |
| 此选项已被弃用, favore of the drop-in replacement, 一个对象数组,每个对象都指定了您的联邦图中一个子图的 您可以为此 如果您正在使用托管联邦,请不要提供此字段。 如果您没有使用托管联邦,则此字段或 |
| 此选项已被弃用, favore of the drop-in replacement, 一个对象(或一个返回对象的异步函数返回包含网关在向子图发送 introspection 请求时包括的 HTTP 头部的名称和值。仅当发送 introspection 请求时包括。 如果您正在使用托管联邦,请不要提供此字段。 如果您定义了 |
| 如果为 默认值是 |
| 用于替代 如果提供了此值,网关将自动记录所有严重级别的消息(从 debug 到 此日志记录器自动添加到所有传递给 Apollo Server 插件函数的 |
| 指定在从 Apollo 获取配置时用于与 Fetch API 实现使用的 托管联邦。 默认情况下,网关使用 如上例所示,如果您想覆盖库的默认配置,也可以提供 |
| 如果 启动时,如果这些请求中的任何一个失败,网关将抛出一个错误。 更新模式时,如果这些请求中的任何一个失败,网关将不会切换到新的模式或 图 配置。网关将在每次轮询间隔重试这些请求,直到成功为止。 默认值是 |
| 网关使用这些 Apollo Uplink URL 来轮询其托管的配置的列表。有关详情和默认值,请参阅 Apollo Uplink。 仅在您使用 托管联邦并且您的特定用例确实需要时,才提供此字段(这很少见)。 |
| 网关尝试失败轮询请求到达 Apollo Uplink 的最大次数,遍历其列表中的 默认情况下,将尝试每个 uplinkEndpoints 三次(即默认列表中的两个端点,总共 5 次重试)。 仅在您使用 托管联邦 时才提供此字段。 |
| 指定此选项作为 Uplink 无法提供轮询间隔时的后备方案。如果 |
| 定义此功能以自定义网关的数据传输到您的某些或全部子图。此自定义可以包括使用除了HTTP以外的协议。有关详细信息,请参阅The |
buildService函数
如果您提供了此函数,网关会为每个子图调用一次。此函数必须返回一个实现GraphQLDataSource接口的对象,例如RemoteGraphQLDataSource或其派生类。
以下示例演示了向网关发送到子图的所有请求中添加x-user-id HTTP头来:
class AuthenticatedDataSource extends RemoteGraphQLDataSource {willSendRequest({ request, context }) {request.http.headers.set('x-user-id', context.userId);}}const gateway = new ApolloGateway({// ...other options...buildService({ name, url }) {return new AuthenticatedDataSource({ url });},});
class AuthenticatedDataSource extends RemoteGraphQLDataSource {willSendRequest({ request, context }) {request.http.headers.set('x-user-id', context.userId);}}const gateway = new ApolloGateway({// ...other options...buildService({ name, url }) {return new AuthenticatedDataSource({ url });},});
实验选项
这些选项是实验性的。它们可能会在任何时候被删除或更改,即使在补丁版本中。
| 名称 类型 | 描述 |
|---|---|
| 设置网关的查询计划存储的近似大小(以 MiB 为单位)。此缓存用于保存查询计划以供后续查询重复使用,这些查询解析到之前观察到的新 默认值是 |
服务健康检查
当被调用时,网关会向每个子图发送一个小的查询({ __typename })以验证其响应性。此函数在失败时抛出异常,并返回一个需要await的 Promise。
参数
| 名称 类型 | 描述 |
|---|---|
| 如果提供,网关将只向地图中包含的数据源发送健康检查请求。 默认情况下,网关使用 |
class RemoteGraphQLDataSource
表示您的联邦网关与您的子图之一的连接。
您可以通过扩展此类并重写其willSendRequest和/或didReceiveResponse方法来自定义此连接:
- 覆盖
willSendRequest以在发送之前修改您的网关对 子图 的请求。 - 覆盖
didReceiveResponse来修改子图 的响应,在网关将其传递给请求客户端之前。
以下详细描述了这些方法。
方法
构造函数
返回一个初始化的 RemoteGraphQLDataSource 实例:
const productsDataSource = new RemoteGraphQLDataSource({url: 'https://products-service.dev/graphql',});
const productsDataSource = new RemoteGraphQLDataSource({url: 'https://products-service.dev/graphql',});
接受一个 options 对象作为参数。以下是该对象的受支持字段:
选项
| 名称 类型 | 描述 |
|---|---|
| 必需。子图的 URL,用于通过 HTTP 发送获取请求。 |
| 如果 此外,子图必须启用对 APQ 的支持,网关才能使用此功能(Apollo Server 默认启用 APQ)。 |
willSendRequest
在子类中覆盖此方法,以在发送到子图的每个获取请求之前对其进行修改:
// Adds an `x-user-id` header to each outgoing fetch requestclass AuthenticatedDataSource extends RemoteGraphQLDataSource {willSendRequest({ request, context }) {request.http.headers.set('x-user-id', context.userId);}}
// Adds an `x-user-id` header to each outgoing fetch requestclass AuthenticatedDataSource extends RemoteGraphQLDataSource {willSendRequest({ request, context }) {request.http.headers.set('x-user-id', context.userId);}}
此方法接收一个 requestContext 对象,该对象包含原始未修改的 request 以及当前的 context 对象(即 contextValue 对象)。
didReceiveResponse
在子类中覆盖此方法,以自定义网关完成对子图的获取后的行为,但在发送响应到请求客户端之前:
class CookieDataSource extends RemoteGraphQLDataSource {didReceiveResponse({ response, request, context }) {const cookie = request.http.headers.get('Cookie');if (cookie) {context.responseCookies.push(cookie);}// Return the response, even when unchanged.return response;}}
class CookieDataSource extends RemoteGraphQLDataSource {didReceiveResponse({ response, request, context }) {const cookie = request.http.headers.get('Cookie');if (cookie) {context.responseCookies.push(cookie);}// Return the response, even when unchanged.return response;}}
此方法接收一个 requestContext 对象,其中包含:
- 子图的
response - 网关对子图的
request - 当前操作 的
context
这使得您能够修改操作 的 contextValue 和获取响应的任何组合。
请求和响应对象的 http 属性包含额外的 HTTP 特定属性,例如 headers。
此方法必须返回一个与GraphQLResponseGraphQLResponse结构匹配的对象
didEncounterError
在子类中重写此方法以自定义网关在与其子图或解析其响应(例如,如果响应不是良好的JSON格式)通信时遇到错误后的行为
如果不重写此方法,则默认实现throw抛出错误,如下所示:
class MyDataSource extends RemoteGraphQLDataSource {didEncounterError(error, fetchRequest, fetchResponse, context) {throw error;}}
class MyDataSource extends RemoteGraphQLDataSource {didEncounterError(error, fetchRequest, fetchResponse, context) {throw error;}}
注意,如果您在这个方法中不抛出error(或者不同的Error)error将在此方法返回后立即抛出。
此方法接受以下位置参数
| 名称 类型 | 描述 |
|---|---|
| 通信过程中发生的错误。 |
| 发送到子图的 |
| 由子图发送的 |
| The |
class IntrospectAndCompose
⚠️ 我们强烈建议不要在生产环境中使用IntrospectAndCompose。有关详情,请参阅《IntrospectAndCompose限制》。
IntrospectAndCompose是用于从子图SDL检索并组合成supergraph的开发工具,用于您的网关。IntrospectAndCompose将根据其子图及其URL列表发出查询,IntrospectAndCompose将它们组合成supergraph,并为此supergraph提供网关。IntrospectAndCompose还可以配置为通过轮询进行更新并执行子图健康检查,以确保supergraph安全更新。IntrospectAndCompose实现SupergraphManager接口,并将其传递给ApolloGateway构造函数的supergraphSdl选项。
IntrospectAndCompose是serviceList的替代品。
方法
构造函数
返回一个初始化的IntrospectAndCompose实例,然后您可以将该实例传递给ApolloGateway构造函数的supergraphSdl配置选项,如下所示:
const server = new ApolloServer({gateway: new ApolloGateway({supergraphSdl: new IntrospectAndCompose({subgraphs: [// ...],}),}),});
const server = new ApolloServer({gateway: new ApolloGateway({supergraphSdl: new IntrospectAndCompose({subgraphs: [// ...],}),}),});
接受一个 options 对象作为参数。此对象的受支持属性描述如下。
示例
提供 subgraphs 列表和头信息以授权检查。
const gateway = new ApolloGateway({supergraphSdl: new IntrospectAndCompose({subgraphs: [{ name: 'products', url: 'https://products-service.dev/graphql' },{ name: 'reviews', url: 'https://reviews-service.dev/graphql' },],introspectionHeaders: {Authorization: 'Bearer abc123',},}),});
const gateway = new ApolloGateway({supergraphSdl: new IntrospectAndCompose({subgraphs: [{ name: 'products', url: 'https://products-service.dev/graphql' },{ name: 'reviews', url: 'https://reviews-service.dev/graphql' },],introspectionHeaders: {Authorization: 'Bearer abc123',},}),});
配置管理的联盟.fetcher
IntrospectAndCompose 使用由 ApolloGateway 构造的数据源。要自定义网关的数据源,您可以将 buildService 函数传递给 ApolloGateway 构造函数。在下面的示例中,IntrospectAndCompose 通过网关的 buildService 函数中构造的 AuthenticatedDataSource 进行受认证的请求,来访问子图。
const gateway = new ApolloGateway({buildService({ name, url }) {return new AuthenticatedDataSource({ url });},supergraphSdl: new IntrospectAndCompose({subgraphs: [{ name: 'products', url: 'https://products-service.dev/graphql' },{ name: 'reviews', url: 'https://reviews-service.dev/graphql' },],}),});
const gateway = new ApolloGateway({buildService({ name, url }) {return new AuthenticatedDataSource({ url });},supergraphSdl: new IntrospectAndCompose({subgraphs: [{ name: 'products', url: 'https://products-service.dev/graphql' },{ name: 'reviews', url: 'https://reviews-service.dev/graphql' },],}),});
选项
| 名称 类型 | 描述 |
|---|---|
| 一个对象数组,每个对象指定了在您的联邦图中一个子图
对于 Studio 用户,子图名称 必须:
|
| 一个对象(或者一个返回对象的可选异步函数),其中包含网关在向您的子图发出检查请求时包含的 HTTP 头的名称和值。 如果您在 |
| 指定此选项以启用通过子图轮询的 superschema 更新。 |
| 此选项仅适用于通过 此选项是 |
| 用于替代
|