graphql - Apollo 联合网关:在编写超图时包含本地模式
问题描述
在为 Apollo Federation 的网关编写超图时,您将创建一个.yaml
配置文件,其中包含到子图的路由 url。例如:https ://github.com/apollographql/supergraph-demo/blob/main/subgraphs/inventory/inventory.js
//supergraph.yaml
subgraphs:
inventory:
routing_url: http://inventory:4000/graphql
schema:
file: ./subgraphs/inventory/inventory.graphql
products:
routing_url: http://products:4000/graphql
schema:
file: ./subgraphs/products/products.graphql
users:
routing_url: http://users:4000/graphql
schema:
file: ./subgraphs/users/users.graphql
在上面的示例中,他们为每个子图启动 Apollo 服务器并组成一个超图。是否可以在不启动 Apollo 服务器且仅包含本地模式的情况下编写超图?
解决方案
你可以。按照本教程:https ://www.apollographql.com/blog/backend/using-apollo-federation-with-local-schemas/
不要使用超级图和子图,而是使用 serviceList 并有条件地构建数据源。
const gateway = new ApolloGateway({
serviceList: [
{ name: "products", url: "http://localhost:4002" },
{ name: "countries", url: "http://countries" },
],
buildService: ({ url }) => {
if (url === "http://countries") {
return new LocalGraphQLDataSource(getCountriesSchema());
} else {
return new RemoteGraphQLDataSource({
url,
});
}
},
});
推荐阅读
- reporting-services - 具有固定源表达式的 SSRS LOOKUP
- docker - 无效的初始堆大小:-Xms1024M -Xmx2024M docker 映像
- javascript - JavaScript - 在 URL 中的两个字符之间获取字符串的更简洁的方法?
- flutter - 我想要求用户访问外部存储以删除文件(歌曲)
- android - 通过单击按钮删除 EditText 中的数字
- google-apps-script - 为 iPhone 暗模式设计卡片?
- linux - golang中的cgroups freeze和linux命令“Kill - STOP pid”有什么区别?
- java - 无法使用 PrintRequestAttributeSet 打印多页
- r - 使用 Tidyr 和 Dplyr 进行分组和填充不起作用
- android - 如何使用图像目录中的单个图像表示文件来实现图像库