graphql-dotnet / graphql-client

A GraphQL Client for .NET Standard
MIT License
623 stars 134 forks source link

AWS Appsync supscription (x-amzn-ErrorType=UnauthorizedException) #221

Closed jayderyu closed 4 years ago

jayderyu commented 4 years ago

Hello, I'm having difficulties successfully establishing a subscription with AWS Appsync.

Thanks to Neil Bostrom for the Appsync sample I have normal query and mutations working. However, whenever I try to subscribe I end up with a 401 error. I think the problem is that I don't have a way to set the headers for a websocket connection.

Below is what AWS cloud watch reports

{ "logType": "RequestSummary", "requestId": "5dc2bf32-08e7-4bd5-a1ee-915df97ccfc7", "graphQLAPIId": "SNIPPED", "statusCode": 401, "latency": 8000000 }

Request Headers: {sec-websocket-protocol=[graphql-ws], cloudfront-viewer-country=[CA], x-forwarded-proto=[https], upgrade=[websocket], x-forwarded-port=[443], x-forwarded-for=[IP_SNIPPED, IP_SNIPPED], sec-websocket-version=[13], via=[1.1 d8792dbd3191bbe722eba5b536b979c8.cloudfront.net (CloudFront)], cloudfront-is-smarttv-viewer=[false], x-amzn-trace-id=[Root=1-5e9db6b3-c540f36310048ece21831f18], cloudfront-is-desktop-viewer=[true], cloudfront-is-tablet-viewer=[false], host=[SNIPPED.appsync-api.us-west-2.amazonaws.com], connection=[upgrade], sec-websocket-key=[p8r36XgiBUaUxJHlSznBeA==], cloudfront-forwarded-proto=[https], x-amz-cf-id=[UXdyNw4mmji7eYFM6wyp2r_AUA_JE5QD68LS9bscbO7BNyQ4m5Km-Q==], cloudfront-is-mobile-viewer=[false]}

Response Headers: {x-amzn-ErrorType=UnauthorizedException}

With the non socket there is a handler I can apply the header with and this let's me set the access information. However, with the socket connection I'm unaware of any way to set the x-api-key for the socket connection to get permission.

This is in the http handler that Neil kindly provided. request.Headers.TryAddWithoutValidation("x-api-key", _awsApiKey);

rose-a commented 4 years ago

Related to #179

jayderyu commented 4 years ago

Thank you very much. I did a build on the branch and was able to re-direct the dll. I'm not getting error 400 instead of 401. And from the error it looks like it's something I can work on resolving. Response Headers: {x-amzn-ErrorType=MalformedHttpRequestException}

It looks like #222 is still pending to be moved into the master branch. Is there a apx eta when this will happen?

jayderyu commented 4 years ago

hmm. not sure why. but there is another problem with the subscription. It seems when creating the subscription string something is happening to the request. image

The Query, Variables and Operation are all null.

This is my request line and this is the same style I use for the query and subscriptions. var request = new GraphQLRequest { Query = query, OperationName = String.IsNullOrEmpty(operationName) ? null : operationName, Variables = variables == null ? new { } : variables }; Console.WriteLine("request " + request.Query); var stream = _graphQLHttpClient.CreateSubscriptionStream(request, (e) => { Console.WriteLine($"Sub exception [{e}]"); });

rose-a commented 4 years ago

222 will get released in the next days...

From my short reading into the AWS AppSync docs im fairly sure I saw someone else having that exact problem too... so maybe Google can already provide you with a solution.

Seems subscriptions (or "Real-Time-Data" as they call it) use a different endpoint, too. Note that you would have to create a separate GraphQLHttpClient for that, since its fixed onto a single endpoint.

jayderyu commented 4 years ago

Thanks for the reply. I'll look into a different end point and whom ever was having the problem. I'll post what was happening when a solution is found.