【问题标题】:Unexpected end of input: ApolloClient error in getting response from AWS API Gateway输入意外结束:从 AWS API Gateway 获取响应时出现 ApolloClient 错误
【发布时间】: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 库却不行。

我能做什么?

【问题讨论】:

    标签: javascript amazon-web-services angular graphql apollo


    【解决方案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" },
                 }
            };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-16
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 2017-05-26
      相关资源
      最近更新 更多