输入意外结束:从 AWS API Gateway 获取响应时出现 ApolloClient 错误

Posted

技术标签:

【中文标题】输入意外结束:从 AWS API Gateway 获取响应时出现 ApolloClient 错误【英文标题】:Unexpected end of input: ApolloClient error in getting response from AWS API Gateway 【发布时间】:2017-05-29 08:16:18 【问题描述】:

我在我的 Angular 应用程序中使用 Apollo,现在我遇到了这个问题。我在 AWS Lambda 上有一个 GraphQL 端点,通过 API Gateway 我看到响应正确返回,但是当它通过读取它时会引发此错误。 AWS 正在使用 LAMBDA_PROXY

//setup
const client = new ApolloClient(
  networkInterface: createNetworkInterface(
    opts: 
      mode: 'no-cors',
    ,
    uri: "https://xxxxxxx.execute-api.ap-southeast-2.amazonaws.com/dev/graphql"
  ),
);

//make the call
this.apollo.watchQuery(
        forceFetch: true,
        query: QueryToSend
    ).subscribe((data) => 
        ...process...

    , error => 
        console.log(error);
    );

//the error...
Network error: Unexpected end of input
    at new ApolloError (ApolloError.js:31)
    at QueryManager.js:128
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (ng_zone.js:236)
    at t.invoke (polyfills.js:3)
    at e.run (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (ng_zone.js:227)
    at t.invokeTask (polyfills.js:3)

请求是

const QueryToSend= gql`
    query data  
        getSuperDefinitionList
            id,
            name, 
        
    
`;

不知道出了什么问题。我从 Postman 和 SoapUI 调用相同的请求正文。我什至看到提琴手响应有正确的响应......

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: ...
Connection: keep-alive
Date: Fri, 13 Jan 2017 21:10:10 GMT
x-amzn-RequestId: ab0fghjghja-d9d4-11e6-b19a-090dsfvdgcebe
X-Amzn-Trace-Id: Root=1-58794232-c7f7157d28e033330dbbe6f2
X-Cache: Miss from cloudfront
Via: 1.1 a1d4b598e9b2fghjgfhj3991961a666a6.cloudfront.net (CloudFront)
X-Amz-Cf-Id: XHeOfghjfghjUthDPKxVbCrFoLM4iOYpf9X9mWlu2Z374grmuAv5jMjg==

"data":"getSuperDefinitionList":["id":"83d3b09f-a947-4a33-bcb0-f5c9e0446ace","name":"S","__typename":"SuperDefinitionType"]

从 AWS 看来一切正常,但 Apollo 或它使用的某些 javascript 库却不行。

我能做什么?

【问题讨论】:

【参考方案1】:

写完之后,我有一种预感。

我删除了

opts: 
  mode: 'no-cors',
,

并确保我返回了正确的标题(最后 3 个被省略):

        var response = new APIGatewayProxyResponse
        
            StatusCode = result.Result.IsError ? (int)HttpStatusCode.BadRequest : (int)HttpStatusCode.OK,
            Body = result.Result.Body,
            Headers = new Dictionary<string, string> 
                 "Content-Type", "application/json" ,
                 "Access-Control-Allow-Origin", "*" ,
                 "Access-Control-Allow-Methods", "GET,POST,OPTIONS" ,
                 "Access-Control-Allow-Headers", "Connection, Host, Origin, Referer, Access-Control-Request-Method, Access-Control-Request-Headers, User-Agent, Accept, Content-Type, Authorization, Content-Length, X-Requested-With, Accept-Encoding, Accept-Language" ,
             
        ;

【讨论】:

以上是关于输入意外结束:从 AWS API Gateway 获取响应时出现 ApolloClient 错误的主要内容,如果未能解决你的问题,请参考以下文章

fetch() 输入意外结束

从 API Gateway 获取 terraform 中 AWS lambda 的端点

从 AWS API Gateway Web 界面配置 AWS Lambda 时无法选择/查看 Lambda 函数

从 AWS API Gateway 自定义域映射中删除映射路径

如何将参数从 POST 从 Amazon API Gateway 传递到 AWS Lambda

Angular 2/Web Api - json 解析错误语法错误意外结束输入