在为Apollo Federation的网关构建supergraph时,您将创建一个.yaml配置文件,其中包含指向子图的路由urls。例如: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服务器和只包含本地模式的情况下构建超图?
发布于 2021-07-28 05:14:11
你可以的。遵循本教程: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,
});
}
},
});https://stackoverflow.com/questions/68548103
复制相似问题