You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I successfully created a subscription to a GraphQL API using ClientWebSocket, but I would like to use GraphQL.Client to realize the subscriptions.
Here is a simple example of a subscription that works using ClientWebSocket.:
client.Options.ClientCertificates.Add(pCertificate);
client.Options.SetRequestHeader("Sec-WebSocket-Extensions", "permessage-deflate;client_max_window_bits");
client.Options.SetRequestHeader("Sec-WebSocket-Protocol", "graphql-transport-ws");
await client.ConnectAsync(pEndpoint, CancellationToken.None);
var initMessage = new
{
type = "connection_init",
payload = new
{
Authorization = pToken
}
};
await SendMessageAsync(client, initMessage);
var subscription = new
{
id = "1",
type = "subscribe",
payload = new
{
httpMultipartParams = new
{
includeCookies = false,
requestQueryPlan = false,
showTraces = false,
abortSignalRef = new { }
},
operationName = "dataFeed",
query = @"
subscription dataFeed {
dataFeed {
name
city
}
}",
variables = new { }
}
};
await SendMessageAsync(client, subscription);
await ReceiveMessageAsync(client);
I tried to do the same with GraphQL.Client but I still get the error: “Connection closed by the server.”
at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<>c__DisplayClass36_0`1.b__3(Exception e) in /_/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs:line 215
After many try, here is my last example code using GraphQL.Client that still give me the same error (the code is running inside a thread that is still alive):
GraphQLHttpClientOptions options = new GraphQLHttpClientOptions
{
UseWebSocketForQueriesAndMutations = false,
WebSocketProtocol = WebSocketProtocols.GRAPHQL_TRANSPORT_WS,
EndPoint = pEndpoint,
ConfigureWebsocketOptions = (opt) =>
{
opt.ClientCertificates.Add(pCertificate);
opt.SetRequestHeader("Sec-WebSocket-Extensions", "permessage-deflate;client_max_window_bits");
},
ConfigureWebSocketConnectionInitPayload = (graphQLHttpOptions) =>
{
return new
{
Authorization = pToken
};
},
};
GraphQLHttpClient graphQLClient = new GraphQLHttpClient(
options,
new NewtonsoftJsonSerializer()
);
GraphQLRequest grapQLSubscription = new GraphQLRequest
{
Query = @"subscription dataFeed {
dataFeed {
name
city
}
}",
OperationName = " dataFeed "
};
var subscriptionResponse = graphQLClient.CreateSubscriptionStream<dynamic>(grapQLSubscription);
var subscription = subscriptionResponse.Subscribe(
onNext: res =>
{
Console.WriteLine(res.Data.rid);
},
onError: error =>
{
Console.WriteLine(error.Message);
},
onCompleted: () =>
{
Console.WriteLine("End subscription");
}
);
I am using .Net Framework 4.8
Did I miss something?
Do you have any idea what I'm doing wrong?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I successfully created a subscription to a GraphQL API using ClientWebSocket, but I would like to use GraphQL.Client to realize the subscriptions.
Here is a simple example of a subscription that works using ClientWebSocket.:
I tried to do the same with GraphQL.Client but I still get the error: “Connection closed by the server.”
at GraphQL.Client.Http.Websocket.GraphQLHttpWebSocket.<>c__DisplayClass36_0`1.b__3(Exception e) in /_/src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs:line 215
After many try, here is my last example code using GraphQL.Client that still give me the same error (the code is running inside a thread that is still alive):
I am using .Net Framework 4.8
Did I miss something?
Do you have any idea what I'm doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions