【问题标题】:ReactJS - NodeJS - Issue Connecting to Graphql On AWS LambdaReactJS - NodeJS - 在 AWS Lambda 上连接到 Graphql 的问题
【发布时间】:2018-10-11 11:58:44
【问题描述】:

我在 aws 上部署了一个 nodejs lambda 函数,它通过 API Gateway 公开一个 lambda 端点。 端点是here,允许您访问graphiql 端点。

我一直试图从我的反应代码中调用它,但我收到以下错误响应

{"message":"Missing Authentication Token"}

以及以下控制台警告

Failed to load https://z8zch5bp3m.execute-api.us-east-1.amazonaws.com/test: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

我在 API 网关中启用了 cors,但仍然出现此错误。

我的简单反应代码如下

import React, { Component } from 'react';
import { gql } from 'apollo-boost';
import { Query } from 'react-apollo';

const ADD_NUMBERS = gql`
  query {
      addNumbers(number1:1, number2:55) {
        add
      }    
  }
`


const App = () => (
  <Query query={ADD_NUMBERS}>
    {({ loading, error, data }) => {
      if (loading) return <div>Loading...</div>;
      if (error) return <div>Error :(</div>;

      return (
        <div>Data: {data}</div>
      )
    }}
  </Query>
)

export default App;

我的 lambda 函数的 nodejs 代码位于 here

如果我需要做任何事情来让这个 lambda 调用正常工作,请告诉我。

【问题讨论】:

    标签: node.js lambda graphql aws-api-gateway apollo-client


    【解决方案1】:

    通过从一开始就启用 cors 重新创建我的 api 网关端点并按预期工作

    【讨论】:

      【解决方案2】:

      查看您的代码并没有告诉我太多信息。我建议你看看这些主题:

      1. 当您使用错误的方法进行 HTTP 调用时也会返回 Missing Authentication Token(假设您想 POST,但您 PUT);
      2. 查看Lambda Proxy Integration。使用 Lambda 代理集成时,您可以在响应中指定标头。在那里,您可以确保允许跨域资源共享。

      希望这会有所帮助。

      【讨论】:

      • 感谢您的反馈@Bilger_Yahov。它在失败的选项调用中。我看到一篇文章提到将选项调用设置为指向模拟以使其通过 - 我没有高兴地尝试过。我启用了代理,但它什么也没做
      • 看看这里可能会很有趣:cloudacademy.com/blog/…
      • 感谢@Bilger_Yahov - 将通过此查看是否有任何帮助
      • 实际上 - 我按照该教程在 Lambda 中创建了我的 graphql 端点。他们所描述的工作正常,但我的应用程序和 API 网关之间的关系现在是
      猜你喜欢
      • 2019-06-04
      • 2019-05-27
      • 2018-10-04
      • 2018-07-23
      • 2020-04-23
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多