Apollo Gateway on AWS Lambda 无法读取未定义的 "content-type "属性。

问题描述 投票:0回答:1

我有一个Node Apollo网关服务在AWS Lambda上运行,它委托给两个不同的GraphQL服务,它在我的Mac上本地工作一切正常。

然而,当在 AWS Lambda 上运行时,当我击中终点时,网关会给我这个错误(期望看到游乐场)。

{
"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中工作。

谢谢你

克里斯

node.js graphql apollo
1个回答
1
投票

你不会让它在REST中工作,因为apollo的代码并没有考虑到从AWS API Gateway接收输入。

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

© www.soinside.com 2019 - 2024. All rights reserved.