gpt4 book ai didi

node.js - Apollo 网关未选择托管联合配置

转载 作者:行者123 更新时间:2023-12-04 10:02:40 27 4
gpt4 key购买 nike

这对每个人来说可能都是新的,但希望最好的找到解决方案。

我一直在尝试通过 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} = await gateway.load();它抛出一个错误
UnhandledPromiseRejectionWarning: Error: When服务列表 is not set, an Apollo Engine configuration must be provided.
我一直在关注官方文档,但不确定我在这里遗漏了什么?

最佳答案

不确定这是否能解决您的问题,但我有一个类似的问题,我的内省(introspection)查询将通过本地网关而不是 Apollo Studio;
首先,我必须通过 CLI 在联合配置中部署各个服务(因为直接报告模式尚不可用)

npx apollo service:push --graph=<graph> --key=<my-key> --localSchemaFile=src/schema.graphql --serviceName=<serviceName> --serviceURL=<serviceUrl> --variant=dev
然后,在网关代码中,我必须删除 serviceList来自 ApolloGateway根据 Apollo Studio setup docs 指示的构造函数:

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 your ApolloGateway constructor entirely:

const gateway = new ApolloGateway({
serviceList: [
{ name: 'test', url: 'http://localhost:4000/graphql'},
]
});
...
const gateway = new ApolloGateway();
在您的情况下,这应该可以解决您的问题,但您也应该更新您对 Apollo Server 的使用。而不是使用 { schema, executor } ,您可以嵌入 gateway直接地:
const server = new ApolloServer({
gateway,
subscriptions: false,
});

关于node.js - Apollo 网关未选择托管联合配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61753200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com