gpt4 book ai didi

node.js - AWS Lambda 上的 Apollo Gateway 无法读取未定义的属性 'content-type'

转载 作者:行者123 更新时间:2023-12-02 19:29:32 25 4
gpt4 key购买 nike

我有一个在 AWS Lambda 上运行的 Node Apollo Gateway 服务,它委托(delegate)给两个不同的 GraphQL 服务,它在我的 Mac 本地运行良好。

但是,在 AWS Lambda 上运行时,当我到达终点(期望看到 Playground )时,网关会出现此错误

{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'content-type' of undefined",
"trace": [
"TypeError: Cannot read property 'content-type' of undefined",
" at fileUploadHandler (/var/task/node_modules/apollo-server-lambda/dist/ApolloServer.js:143:50)",
" at Runtime.handler (/var/task/node_modules/apollo-server-lambda/dist/ApolloServer.js:166:13)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}

我没有对上传执行任何操作,但是查看 ApolloServer.js,我可以看到创建了一个处理程序。

我的 index.js 看起来像这样:

const isLambda = !!process.env.LAMBDA_TASK_ROOT;

const { ApolloServer, gql } = isLambda ? require('apollo-server-lambda') : require('apollo-server');

const { ApolloGateway, RemoteGraphQLDataSource } = require("@apollo/gateway");

const gateway = new ApolloGateway({
serviceList: [
{ name: 'service1', url: !isLambda ? 'http://127.0.0.1:5090' : 'https://api.blah.com/search' },
{ name: 'service2', url: !isLambda ? 'http://127.0.0.1:5110' : 'https://api.elsewhere.com/other' },
],
});

const server = new ApolloServer({
gateway,
subscriptions: false,
uploads: false,
context: ({ event, context }) => ({
headers: event.headers,
functionName: context.functionName,
event,
context,
}),
playground: true,
introspection: true,
});

if (!isLambda) {
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
} else {
exports.graphql = server.createHandler({
cors: {
origin: '*',
credentials: true,
},
});
}

我使用的 NPM 模块是:

npm install  @apollo/gateway apollo-server-lambda graphql

有任何关于这里可能出现问题的指示吗?

2020 年 5 月 25 日更新这似乎与我在 AWS Gateway 中选择 HTTP 还是 REST 有关,HTTP 让我克服了这个错误,但我不知道我需要做什么才能使其在 REST 中工作

谢谢

克里斯

最佳答案

您无法使其在 REST 中工作,因为 apollo 的代码在编写时并未考虑从 AWS API Gateway 接收输入。

它期望处理整个请求。因此,要么使用 API 网关作为代理,要么必须伪造它期望存在的所有其他属性。

关于node.js - AWS Lambda 上的 Apollo Gateway 无法读取未定义的属性 'content-type',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009883/

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