这对每个人来说可能都很陌生,但希望最好的人能找到解决方案。
我一直在尝试按照官方文档通过 ApolloGateway 设置 apollo 管理的联合来联合我的服务。 https://www.apollographql.com/docs/graph-manager/managed-federation/setup/#4-deploy-the-modified-gateway
.env
NODE_ENV=development
APOLLO_KEY=service:service_name:hash
Apollo 网关
import 'reflect-metadata';
import express from 'express';
import {ApolloServer} from 'apollo-server-express';
import {ApolloGateway} from '@apollo/gateway';
import {config} from 'dotenv';
config();
const port = process.env.NODE_PORT || 7000;
const nodeEnv = process.env.NODE_ENV || 'localhost';
const nodeHost = process.env.NODE_HOST || 'http://localhost';
const apolloGatewayConfig: any = {
__exposeQueryPlanExperimental: false,
};
if (nodeEnv === 'localhost' || true) {
apolloGatewayConfig.serviceList = [
{
name: 'vendors',
url: `${process.env.GMS_VENDORS_NODE_HOST}/graphql`,
}
];
}
const gateway = new ApolloGateway(apolloGatewayConfig);
(async () => {
const app = express();
app.get('/health', (_, res: any): void => {
res.send({gateway: true});
});
const {schema, executor} = await gateway.load(); // breaking point
const server = new ApolloServer({
schema,
executor,
engine: true,
subscriptions: false,
});
server.applyMiddleware({app, path: '/graphql'});
app.listen({port}, () =>
console.log(`API Gateway is ready at ${nodeHost}:${port}`)
);
})();
在行const {schema, executor} = wait gateway.load();
它抛出一个错误
UnhandledPromiseRejectionWarning:错误:未设置
serviceList时,必须提供 Apollo 引擎配置。
我一直在关注官方文档,但不确定我在这里错过了什么?
最佳答案
不确定这是否能解决您的问题,但我遇到了类似的问题,我的内省(introspection)查询将通过本地网关而不是 Apollo Studio;
首先,我必须通过 CLI 在联合配置中部署各个服务(因为直接报告架构尚不可用)
npx apollo service:push --graph=<graph> --key=<my-key> --localSchemaFile=src/schema.graphql --serviceName=<serviceName> --serviceURL=<serviceUrl> --variant=dev
然后,在网关代码中,我必须按照 Apollo Studio setup docs 的指示从 ApolloGateway
构造函数中删除 serviceList
:
This option specifies the name and URL for each of your graph's implementing services. With managed federation, this information is no longer hardcoded in the gateway's constructor! Instead, the gateway regularly polls Apollo for this information. This enables you to add and remove implementing services from your graph without needing to restart your gateway.
Remove the
serviceList
argument from yourApolloGateway
constructor entirely:
const gateway = new ApolloGateway({
serviceList: [
{ name: 'test', url: 'http://localhost:4000/graphql'},
]
});
...
const gateway = new ApolloGateway();
就您而言,这应该可以解决您的问题,但您还应该更新 Apollo Server 的使用。您可以直接嵌入网关
,而不是使用{ schema, executor }
:
const server = new ApolloServer({
gateway,
subscriptions: false,
});
关于node.js - Apollo 网关未获取托管联合配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61753200/