graphql-dotnet / graphql-dotnet/graphql-client
GraphQL.Client Subscription Error from Xamarin
- Dominant language
- C#
- Stars
- 648
- Forks
- 136
- PR merge metrics
- No merged PRs in 30d
Description
Hi everyone,
I have a proof of concept on AppSync/GraphQL.Client.
I am successfully querying and creating GraphQL mutations. However, I get an error when trying to subscribe.
**Error**:
`
Unable to connect to the remote server
`
**Stack Trace**:
```
at System.Net.WebSockets.WebSocketHandle.ParseAndValidateConnectResponseAsync (System.IO.Stream stream, System.Net.WebSockets.ClientWebSocketOptions options, System.String expectedSecWebSocketAccept, System.Threading.CancellationToken cancellationToken) [0x00100] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs:337
at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.WebSockets.ClientWebSocketOptions options) [0x00383] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs:148
at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000d1] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/ClientWebSocket.cs:157
at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.ConnectAsync (System.Threading.CancellationToken token) [0x000ff] in <5343ffaac4864176b8f7c0b9cc6d172c>:0
```
I was [reading on AWS App Sync documentation](https://docs.aws.amazon.com/appsync/latest/devguide/real-time-websocket-client.html) that the web socket URL is different from the GraphQL Endpoint.
I am trying to follow the example from the [integration test](https://github.com/graphql-dotnet/graphql-client/blob/master/SubscriptionIntegrationTest.ConsoleClient/Program.cs)
**Code**
```
public static void CreateSubscription()
{
var subscriptions = new CompositeDisposable();
subscriptions.Add(Client.WebSocketReceiveErrors.Subscribe(e =>
{
if (e is WebSocketException we)
Console.WriteLine($"WebSocketException: {we.Message} (WebSocketError {we.WebSocketErrorCode}, ErrorCode {we.ErrorCode}, NativeErrorCode {we.NativeErrorCode}");
else
Console.WriteLine($"Exception in websocket receive stream: {e.ToString()}");
}));
subscriptions.Add(CreateSubscription("1"));
}
private static IDisposable CreateSubscription(string id)
{
var taskCreatedRequest = new GraphQLRequest
{
Query = @"
subscription {
onCreateTasksPocModel{
id
description
date
}
}",
Variables = new
{
id
}
};
IObservable> subscriptionStream
= Client.CreateSubscriptionStream(taskCreatedRequest);
var subscription = subscriptionStream.Subscribe(
response => {
Console.WriteLine($"new message from \"{response}\": {response}");
},
exception => {
Console.WriteLine($"message subscription stream failed: {exception}");
},
() =>
{
Console.WriteLine($"message subscription stream completed");
});
return subscription;
}
static GraphQLHttpClient CreateClient()
{
var graphQLOptions = new GraphQLHttpClientOptions()
{
EndPoint = new Uri(GraphQLConstants.GraphQLApiUrl), //https://
//AppSync real-time endpoint from the AWS AppSync GraphQL endpoint is different for websocket
//as per https://docs.aws.amazon.com/appsync/latest/devguide/real-time-websocket-client.html
WebSocketEndPoint = new Uri(GraphQLConstants.GraphQLSocketUrl),//wss://
HttpMessageHandler = new NativeMessageHandler(),
UseWebSocketForQueriesAndMutations =true
};
var client = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer());
client.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(new ProductHeaderValue(nameof(MobileGraphql))));
client.HttpClient.DefaultRequestHeaders.Add(GraphQLConstants.ApiKeyName, GraphQLConstants.ApiKeyValue);
client.HttpClient.DefaultRequestHeaders.Host = GraphQLConstants.GraphQLHost;
return client;
}
```
[View Full Source Code (Gist](https://gist.github.com/henry-delgado/0cfe837e935b51d77f919263e25fe1ae)
I have read similar issues such #333 but that has not helped and wonder if anyone could help me out.
Thanks
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.