【发布时间】: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