Skip to content

Commit 5dd4bfc

Browse files
committed
Subscriptions always use websocket connection
1 parent 87dbf3e commit 5dd4bfc

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/GraphQL.Client/GraphQLHttpClient.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace GraphQL.Client.Http
1515
{
1616
public class GraphQLHttpClient : IGraphQLClient
1717
{
18-
private readonly GraphQLHttpWebSocket _graphQlHttpWebSocket = null;
18+
private GraphQLHttpWebSocket _graphQlHttpWebSocket = null;
1919
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
2020
private readonly ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object> _subscriptionStreams = new ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object>();
2121

@@ -37,12 +37,12 @@ public class GraphQLHttpClient : IGraphQLClient
3737
/// <summary>
3838
/// Publishes all exceptions which occur inside the websocket receive stream (i.e. for logging purposes)
3939
/// </summary>
40-
public IObservable<Exception> WebSocketReceiveErrors => _graphQlHttpWebSocket.ReceiveErrors;
40+
public IObservable<Exception> WebSocketReceiveErrors => GetGraphQLHttpWebSocket().ReceiveErrors;
4141

4242
/// <summary>
4343
/// the websocket connection state
4444
/// </summary>
45-
public IObservable<GraphQLWebsocketConnectionState> WebsocketConnectionState => _graphQlHttpWebSocket.ConnectionState;
45+
public IObservable<GraphQLWebsocketConnectionState> WebsocketConnectionState => GetGraphQLHttpWebSocket().ConnectionState;
4646

4747
#region Constructors
4848

@@ -75,7 +75,7 @@ public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJson
7575
public async Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(GraphQLRequest request, CancellationToken cancellationToken = default)
7676
{
7777
if (Options.UseWebSocketForQueriesAndMutations)
78-
return await _graphQlHttpWebSocket.SendRequest<TResponse>(request, cancellationToken);
78+
return await GetGraphQLHttpWebSocket().SendRequest<TResponse>(request, cancellationToken);
7979

8080
return await SendHttpRequestAsync<TResponse>(request, cancellationToken);
8181
}
@@ -96,7 +96,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
9696
if (_subscriptionStreams.ContainsKey(key))
9797
return (IObservable<GraphQLResponse<TResponse>>)_subscriptionStreams[key];
9898

99-
var observable = _graphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request);
99+
var observable = GetGraphQLHttpWebSocket().CreateSubscriptionStream<TResponse>(request);
100100

101101
_subscriptionStreams.TryAdd(key, observable);
102102
return observable;
@@ -113,7 +113,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
113113
if (_subscriptionStreams.ContainsKey(key))
114114
return (IObservable<GraphQLResponse<TResponse>>)_subscriptionStreams[key];
115115

116-
var observable = _graphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request, exceptionHandler);
116+
var observable = GetGraphQLHttpWebSocket().CreateSubscriptionStream<TResponse>(request, exceptionHandler);
117117
_subscriptionStreams.TryAdd(key, observable);
118118
return observable;
119119
}
@@ -124,7 +124,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
124124
/// explicitly opens the websocket connection. Will be closed again on disposing the last subscription
125125
/// </summary>
126126
/// <returns></returns>
127-
public Task InitializeWebsocketConnection() => _graphQlHttpWebSocket.InitializeWebSocket();
127+
public Task InitializeWebsocketConnection() => GetGraphQLHttpWebSocket().InitializeWebSocket();
128128

129129
#region Private Methods
130130

@@ -158,6 +158,15 @@ private Uri GetWebSocketUri()
158158
return new Uri($"{webSocketSchema}://{Options.EndPoint.Host}:{Options.EndPoint.Port}{Options.EndPoint.AbsolutePath}");
159159
}
160160

161+
private GraphQLHttpWebSocket GetGraphQLHttpWebSocket()
162+
{
163+
//no instance
164+
if (_graphQlHttpWebSocket == null)
165+
_graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), this);
166+
167+
return _graphQlHttpWebSocket;
168+
}
169+
161170
#endregion
162171

163172
#region IDisposable

0 commit comments

Comments
 (0)