@@ -15,7 +15,9 @@ namespace GraphQL.Client.Http
1515{
1616 public class GraphQLHttpClient : IGraphQLClient
1717 {
18- private GraphQLHttpWebSocket _graphQlHttpWebSocket = null ;
18+ private readonly Lazy < GraphQLHttpWebSocket > _lazyHttpWebSocket ;
19+ private GraphQLHttpWebSocket _graphQlHttpWebSocket => _lazyHttpWebSocket . Value ;
20+
1921 private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource ( ) ;
2022 private readonly ConcurrentDictionary < Tuple < GraphQLRequest , Type > , object > _subscriptionStreams = new ConcurrentDictionary < Tuple < GraphQLRequest , Type > , object > ( ) ;
2123
@@ -37,12 +39,12 @@ public class GraphQLHttpClient : IGraphQLClient
3739 /// <summary>
3840 /// Publishes all exceptions which occur inside the websocket receive stream (i.e. for logging purposes)
3941 /// </summary>
40- public IObservable < Exception > WebSocketReceiveErrors => GetGraphQLHttpWebSocket ( ) . ReceiveErrors ;
42+ public IObservable < Exception > WebSocketReceiveErrors => _graphQlHttpWebSocket . ReceiveErrors ;
4143
4244 /// <summary>
4345 /// the websocket connection state
4446 /// </summary>
45- public IObservable < GraphQLWebsocketConnectionState > WebsocketConnectionState => GetGraphQLHttpWebSocket ( ) . ConnectionState ;
47+ public IObservable < GraphQLWebsocketConnectionState > WebsocketConnectionState => _graphQlHttpWebSocket . ConnectionState ;
4648
4749 #region Constructors
4850
@@ -63,8 +65,7 @@ public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJson
6365 if ( ! HttpClient . DefaultRequestHeaders . UserAgent . Any ( ) )
6466 HttpClient . DefaultRequestHeaders . UserAgent . Add ( new ProductInfoHeaderValue ( GetType ( ) . Assembly . GetName ( ) . Name , GetType ( ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ;
6567
66- if ( options . UseWebSocketForQueriesAndMutations )
67- _graphQlHttpWebSocket = new GraphQLHttpWebSocket ( GetWebSocketUri ( ) , this ) ;
68+ _lazyHttpWebSocket = new Lazy < GraphQLHttpWebSocket > ( ( ) => new GraphQLHttpWebSocket ( GetWebSocketUri ( ) , this ) ) ;
6869 }
6970
7071 #endregion
@@ -75,7 +76,7 @@ public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJson
7576 public async Task < GraphQLResponse < TResponse > > SendQueryAsync < TResponse > ( GraphQLRequest request , CancellationToken cancellationToken = default )
7677 {
7778 if ( Options . UseWebSocketForQueriesAndMutations )
78- return await GetGraphQLHttpWebSocket ( ) . SendRequest < TResponse > ( request , cancellationToken ) ;
79+ return await _graphQlHttpWebSocket . SendRequest < TResponse > ( request , cancellationToken ) ;
7980
8081 return await SendHttpRequestAsync < TResponse > ( request , cancellationToken ) ;
8182 }
@@ -96,7 +97,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
9697 if ( _subscriptionStreams . ContainsKey ( key ) )
9798 return ( IObservable < GraphQLResponse < TResponse > > ) _subscriptionStreams [ key ] ;
9899
99- var observable = GetGraphQLHttpWebSocket ( ) . CreateSubscriptionStream < TResponse > ( request ) ;
100+ var observable = _graphQlHttpWebSocket . CreateSubscriptionStream < TResponse > ( request ) ;
100101
101102 _subscriptionStreams . TryAdd ( key , observable ) ;
102103 return observable ;
@@ -113,7 +114,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
113114 if ( _subscriptionStreams . ContainsKey ( key ) )
114115 return ( IObservable < GraphQLResponse < TResponse > > ) _subscriptionStreams [ key ] ;
115116
116- var observable = GetGraphQLHttpWebSocket ( ) . CreateSubscriptionStream < TResponse > ( request , exceptionHandler ) ;
117+ var observable = _graphQlHttpWebSocket . CreateSubscriptionStream < TResponse > ( request , exceptionHandler ) ;
117118 _subscriptionStreams . TryAdd ( key , observable ) ;
118119 return observable ;
119120 }
@@ -124,7 +125,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
124125 /// explicitly opens the websocket connection. Will be closed again on disposing the last subscription
125126 /// </summary>
126127 /// <returns></returns>
127- public Task InitializeWebsocketConnection ( ) => GetGraphQLHttpWebSocket ( ) . InitializeWebSocket ( ) ;
128+ public Task InitializeWebsocketConnection ( ) => _graphQlHttpWebSocket . InitializeWebSocket ( ) ;
128129
129130 #region Private Methods
130131
@@ -158,15 +159,6 @@ private Uri GetWebSocketUri()
158159 return new Uri ( $ "{ webSocketSchema } ://{ Options . EndPoint . Host } :{ Options . EndPoint . Port } { Options . EndPoint . AbsolutePath } ") ;
159160 }
160161
161- private GraphQLHttpWebSocket GetGraphQLHttpWebSocket ( )
162- {
163- //no instance
164- if ( _graphQlHttpWebSocket == null )
165- _graphQlHttpWebSocket = new GraphQLHttpWebSocket ( GetWebSocketUri ( ) , this ) ;
166-
167- return _graphQlHttpWebSocket ;
168- }
169-
170162 #endregion
171163
172164 #region IDisposable
@@ -196,7 +188,7 @@ protected virtual void Dispose(bool disposing)
196188 Debug . WriteLine ( $ "Disposing GraphQLHttpClient on endpoint { Options . EndPoint } ") ;
197189 _cancellationTokenSource . Cancel ( ) ;
198190 HttpClient . Dispose ( ) ;
199- _graphQlHttpWebSocket ? . Dispose ( ) ;
191+ _lazyHttpWebSocket ? . Value . Dispose ( ) ;
200192 _cancellationTokenSource . Dispose ( ) ;
201193 }
202194 }
0 commit comments