Skip to content

Commit 10c2236

Browse files
authored
Merge pull request #370 from graphql-dotnet/fix-configure-await
Fix configure await
2 parents 8cbe46e + 4a2a963 commit 10c2236

File tree

23 files changed

+95
-735
lines changed

23 files changed

+95
-735
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,6 @@ dotnet_naming_style.end_in_async_style.required_suffix = Async
254254

255255
# dotnet_naming_rule.<namingRuleTitle>.severity = <value>
256256
dotnet_naming_rule.async_methods_end_in_async.severity = warning
257+
258+
# ReSharper: Configure await
259+
configure_await_analysis_mode = library

GraphQL.Client.sln

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ VisualStudioVersion = 16.0.28407.52
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{47C98B55-08F1-4428-863E-2C5C876DEEFE}"
77
ProjectSection(SolutionItems) = preProject
8+
src\.editorconfig = src\.editorconfig
89
src\src.props = src\src.props
910
EndProjectSection
1011
EndProject
@@ -14,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1415
.gitignore = .gitignore
1516
dotnet-tools.json = dotnet-tools.json
1617
LICENSE.txt = LICENSE.txt
18+
examples\GraphQL.Client.Example\Program.cs = examples\GraphQL.Client.Example\Program.cs
1719
README.md = README.md
1820
root.props = root.props
1921
EndProjectSection
@@ -65,6 +67,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{89
6567
EndProject
6668
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GraphQL.Client.Example", "examples\GraphQL.Client.Example\GraphQL.Client.Example.csproj", "{6B13B87D-1EF4-485F-BC5D-891E2F4DA6CD}"
6769
EndProject
70+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{98D4DDDD-DE15-4997-B888-9BC806C7416C}"
71+
EndProject
6872
Global
6973
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7074
Debug|Any CPU = Debug|Any CPU
@@ -133,6 +137,7 @@ Global
133137
EndGlobalSection
134138
GlobalSection(NestedProjects) = preSolution
135139
{E95A1258-F666-4D4E-9101-E0C46F6A3CB3} = {0B0EDB0F-FF67-4B78-A8DB-B5C23E1FEE8C}
140+
{05CAF9B2-981E-40C0-AE31-5FA56E351F12} = {98D4DDDD-DE15-4997-B888-9BC806C7416C}
136141
{87FC440E-6A4D-47D8-9EB2-416FC31CC4A6} = {47C98B55-08F1-4428-863E-2C5C876DEEFE}
137142
{C212983F-67DB-44EB-BFB0-5DA75A86DF55} = {0B0EDB0F-FF67-4B78-A8DB-B5C23E1FEE8C}
138143
{92107DF5-73DF-4371-8EB1-6734FED704AD} = {0B0EDB0F-FF67-4B78-A8DB-B5C23E1FEE8C}
@@ -146,6 +151,7 @@ Global
146151
{0D307BAD-27AE-4A5D-8764-4AA2620B01E9} = {0B0EDB0F-FF67-4B78-A8DB-B5C23E1FEE8C}
147152
{7FFFEC00-D751-4FFC-9FD4-E91858F9A1C5} = {47C98B55-08F1-4428-863E-2C5C876DEEFE}
148153
{6B13B87D-1EF4-485F-BC5D-891E2F4DA6CD} = {89AD33AB-64F6-4F82-822F-21DF7A10CEC0}
154+
{98D4DDDD-DE15-4997-B888-9BC806C7416C} = {63F75859-4698-4EDE-8B70-4ACBB8BC425A}
149155
EndGlobalSection
150156
GlobalSection(ExtensibilityGlobals) = postSolution
151157
SolutionGuid = {387AC1AC-F90C-4EF8-955A-04D495C75AF4}

examples/.editorconfig

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Configure await
2+
configure_await_analysis_mode = disabled

src/GraphQL.Client.LocalExecution/GraphQLLocalExecutionClient.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
7676

7777
private async Task<GraphQLResponse<TResponse>> ExecuteQueryAsync<TResponse>(GraphQLRequest request, CancellationToken cancellationToken)
7878
{
79-
var executionResult = await ExecuteAsync(request, cancellationToken);
80-
return await ExecutionResultToGraphQLResponseAsync<TResponse>(executionResult, cancellationToken);
79+
var executionResult = await ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
80+
return await ExecutionResultToGraphQLResponseAsync<TResponse>(executionResult, cancellationToken).ConfigureAwait(false);
8181
}
8282
private async Task<IObservable<GraphQLResponse<TResponse>>> ExecuteSubscriptionAsync<TResponse>(GraphQLRequest request, CancellationToken cancellationToken = default)
8383
{
84-
var result = await ExecuteAsync(request, cancellationToken);
84+
var result = await ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
8585
var stream = ((SubscriptionExecutionResult)result).Streams?.Values.SingleOrDefault();
8686

8787
return stream == null
@@ -106,17 +106,17 @@ private async Task<ExecutionResult> ExecuteAsync(GraphQLRequest request, Cancell
106106
options.Query = deserializedRequest?.Query;
107107
options.Inputs = inputs;
108108
options.CancellationToken = cancellationToken;
109-
});
109+
}).ConfigureAwait(false);
110110

111111
return result;
112112
}
113113

114114
private async Task<GraphQLResponse<TResponse>> ExecutionResultToGraphQLResponseAsync<TResponse>(ExecutionResult executionResult, CancellationToken cancellationToken = default)
115115
{
116116
using var stream = new MemoryStream();
117-
await _documentWriter.WriteAsync(stream, executionResult, cancellationToken);
117+
await _documentWriter.WriteAsync(stream, executionResult, cancellationToken).ConfigureAwait(false);
118118
stream.Seek(0, SeekOrigin.Begin);
119-
return await Serializer.DeserializeFromUtf8StreamAsync<TResponse>(stream, cancellationToken);
119+
return await Serializer.DeserializeFromUtf8StreamAsync<TResponse>(stream, cancellationToken).ConfigureAwait(false);
120120
}
121121

122122
#endregion

src/GraphQL.Client/GraphQLHttpClient.cs

+10-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Linq;
@@ -19,7 +18,6 @@ public class GraphQLHttpClient : IGraphQLClient
1918
private GraphQLHttpWebSocket GraphQlHttpWebSocket => _lazyHttpWebSocket.Value;
2019

2120
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
22-
private readonly ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object> _subscriptionStreams = new ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object>();
2321

2422
private readonly bool _disposeHttpClient = false;
2523

@@ -85,9 +83,9 @@ public async Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(GraphQLR
8583
if (Options.UseWebSocketForQueriesAndMutations ||
8684
!(Options.WebSocketEndPoint is null) && Options.EndPoint is null ||
8785
Options.EndPoint.HasWebSocketScheme())
88-
return await GraphQlHttpWebSocket.SendRequest<TResponse>(request, cancellationToken);
86+
return await GraphQlHttpWebSocket.SendRequest<TResponse>(request, cancellationToken).ConfigureAwait(false);
8987

90-
return await SendHttpRequestAsync<TResponse>(request, cancellationToken);
88+
return await SendHttpRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
9189
}
9290

9391
/// <inheritdoc />
@@ -97,34 +95,15 @@ public Task<GraphQLResponse<TResponse>> SendMutationAsync<TResponse>(GraphQLRequ
9795

9896
/// <inheritdoc />
9997
public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TResponse>(GraphQLRequest request)
100-
{
101-
if (_disposed)
102-
throw new ObjectDisposedException(nameof(GraphQLHttpClient));
103-
104-
var key = new Tuple<GraphQLRequest, Type>(request, typeof(TResponse));
105-
106-
if (_subscriptionStreams.ContainsKey(key))
107-
return (IObservable<GraphQLResponse<TResponse>>)_subscriptionStreams[key];
108-
109-
var observable = GraphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request);
110-
111-
_subscriptionStreams.TryAdd(key, observable);
112-
return observable;
113-
}
98+
=> CreateSubscriptionStream<TResponse>(request, null);
11499

115100
/// <inheritdoc />
116-
public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TResponse>(GraphQLRequest request, Action<Exception> exceptionHandler)
101+
public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TResponse>(GraphQLRequest request, Action<Exception>? exceptionHandler)
117102
{
118103
if (_disposed)
119104
throw new ObjectDisposedException(nameof(GraphQLHttpClient));
120-
121-
var key = new Tuple<GraphQLRequest, Type>(request, typeof(TResponse));
122-
123-
if (_subscriptionStreams.ContainsKey(key))
124-
return (IObservable<GraphQLResponse<TResponse>>)_subscriptionStreams[key];
125-
105+
126106
var observable = GraphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request, exceptionHandler);
127-
_subscriptionStreams.TryAdd(key, observable);
128107
return observable;
129108
}
130109

@@ -140,24 +119,24 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
140119

141120
private async Task<GraphQLHttpResponse<TResponse>> SendHttpRequestAsync<TResponse>(GraphQLRequest request, CancellationToken cancellationToken = default)
142121
{
143-
var preprocessedRequest = await Options.PreprocessRequest(request, this);
122+
var preprocessedRequest = await Options.PreprocessRequest(request, this).ConfigureAwait(false);
144123

145124
using var httpRequestMessage = preprocessedRequest.ToHttpRequestMessage(Options, JsonSerializer);
146-
using var httpResponseMessage = await HttpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
125+
using var httpResponseMessage = await HttpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
147126

148-
var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync();
127+
var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
149128

150129
if (httpResponseMessage.IsSuccessStatusCode)
151130
{
152-
var graphQLResponse = await JsonSerializer.DeserializeFromUtf8StreamAsync<TResponse>(contentStream, cancellationToken);
131+
var graphQLResponse = await JsonSerializer.DeserializeFromUtf8StreamAsync<TResponse>(contentStream, cancellationToken).ConfigureAwait(false);
153132
return graphQLResponse.ToGraphQLHttpResponse(httpResponseMessage.Headers, httpResponseMessage.StatusCode);
154133
}
155134

156135
// error handling
157136
string content = null;
158137
if (contentStream != null)
159138
using (var sr = new StreamReader(contentStream))
160-
content = await sr.ReadToEndAsync();
139+
content = await sr.ReadToEndAsync().ConfigureAwait(false);
161140

162141
throw new GraphQLHttpRequestException(httpResponseMessage.StatusCode, httpResponseMessage.Headers, content);
163142
}

0 commit comments

Comments
 (0)