Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
# https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options

# .NET coding convention settings for EditorConfig
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2019
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference

# Language conventions
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions

# Formatting conventions
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-formatting-conventions?view=vs-2019
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-formatting-conventions

# .NET naming conventions for EditorConfig
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions?view=vs-2019
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions

# Top-most EditorConfig file
root = true
Expand All @@ -26,6 +26,9 @@ indent_style = space
indent_size = 2
trim_trailing_whitespace = true

[*.json]
insert_final_newline = false

[*.cs]
indent_size = 4

Expand Down Expand Up @@ -91,8 +94,8 @@ csharp_style_var_elsewhere = true:suggestion

# C# code style settings - Expression-bodied members
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#expression-bodied-members
csharp_style_expression_bodied_methods = when_on_single_line:warning
csharp_style_expression_bodied_constructors = false:suggestion
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
csharp_style_expression_bodied_constructors = false:warning
csharp_style_expression_bodied_operators = when_on_single_line:warning
csharp_style_expression_bodied_properties = when_on_single_line:warning
csharp_style_expression_bodied_indexers = when_on_single_line:warning
Expand Down Expand Up @@ -120,7 +123,7 @@ csharp_style_conditional_delegate_call = true:warning

# C# code style settings - Code block preferences
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#code-block-preferences
csharp_prefer_braces = false:suggestion
csharp_prefer_braces = when_multiline:suggestion

# C# code style - Unused value preferences
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#unused-value-preferences
Expand All @@ -129,17 +132,17 @@ csharp_style_unused_value_assignment_preference = discard_variable:suggestion

# C# code style - Index and range preferences
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#index-and-range-preferences
csharp_style_prefer_index_operator = true:warning
csharp_style_prefer_range_operator = true:warning
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion

# C# code style - Miscellaneous preferences
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#miscellaneous-preferences
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_using_directive_placement = outside_namespace:warning
csharp_prefer_static_local_function = true:suggestion
csharp_prefer_simple_using_statement = false:suggestion
csharp_style_prefer_switch_expression = true:suggestion
csharp_prefer_simple_using_statement = true:warning
csharp_style_prefer_switch_expression = true:warning

# .NET formatting settings - Organize using directives
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-formatting-conventions?view=vs-2019#organize-using-directives
Expand Down Expand Up @@ -260,3 +263,27 @@ dotnet_naming_rule.async_methods_end_in_async.severity = warning

# ReSharper: Configure await
configure_await_analysis_mode = library

# Remove unnecessary import https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0005
dotnet_diagnostic.IDE0005.severity = error

# Enforce formatting https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#rule-id-ide0055-fix-formatting
dotnet_diagnostic.IDE0055.severity = error

# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS0060.md
dotnet_diagnostic.RCS0060.severity = warning
roslynator_blank_line_after_file_scoped_namespace_declaration = true

# https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1080.md
dotnet_diagnostic.RCS1080.severity = warning

# ConfigureAwait https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1090.md
dotnet_diagnostic.RCS1090.severity = warning
roslynator_configure_await = true

# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1102.md
# TODO: NullabilityInfo issue in Patching.cs in internal class SR
dotnet_diagnostic.RCS1102.severity = suggestion

# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1194.md
dotnet_diagnostic.RCS1194.severity = suggestion
2 changes: 0 additions & 2 deletions examples/GraphQL.Client.Example/PersonAndFilmsResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace GraphQL.Client.Example;

public class PersonAndFilmsResponse
Expand Down
3 changes: 0 additions & 3 deletions examples/GraphQL.Client.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GraphQL.Client.Abstractions.Utilities;
namespace GraphQL.Client.Abstractions.Utilities;

/// <summary>
/// Copied from https://github.com/jquense/StringUtils
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.Client.Abstractions/Utilities/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;

namespace GraphQL.Client.Abstractions.Utilities;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GraphQL.Client.Serializer.Newtonsoft;

public class NewtonsoftJsonSerializer : IGraphQLWebsocketJsonSerializer
{
public static JsonSerializerSettings DefaultJsonSerializerSettings => new JsonSerializerSettings
public static JsonSerializerSettings DefaultJsonSerializerSettings => new()
{
ContractResolver = new CamelCasePropertyNamesContractResolver { IgnoreIsSpecifiedMembers = true },
MissingMemberHandling = MissingMemberHandling.Ignore,
Expand All @@ -34,7 +34,7 @@ public NewtonsoftJsonSerializer(JsonSerializerSettings jsonSerializerSettings)

public byte[] SerializeToBytes(GraphQLWebSocketRequest request)
{
var json = JsonConvert.SerializeObject(request, JsonSerializerSettings);
string json = JsonConvert.SerializeObject(request, JsonSerializerSettings);
return Encoding.UTF8.GetBytes(json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GraphQL.Client.Serializer.SystemTextJson;

public class ConstantCaseJsonNamingPolicy: JsonNamingPolicy
public class ConstantCaseJsonNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name) => name.ToConstantCase();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public class ErrorPathConverter : JsonConverter<ErrorPath>
{

public override ErrorPath Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
new ErrorPath(ReadArray(ref reader));
new(ReadArray(ref reader));

public override void Write(Utf8JsonWriter writer, ErrorPath value, JsonSerializerOptions options)
=> throw new NotImplementedException(
"This converter currently is only intended to be used to read a JSON object into a strongly-typed representation.");
private IEnumerable<object?> ReadArray(ref Utf8JsonReader reader)

private static IEnumerable<object?> ReadArray(ref Utf8JsonReader reader)
{
if (reader.TokenType != JsonTokenType.StartArray)
{
Expand All @@ -33,7 +33,7 @@ public override void Write(Utf8JsonWriter writer, ErrorPath value, JsonSerialize
return array;
}

private object? ReadValue(ref Utf8JsonReader reader)
private static object? ReadValue(ref Utf8JsonReader reader)
=> reader.TokenType switch
{
JsonTokenType.None => null,
Expand Down
6 changes: 2 additions & 4 deletions src/GraphQL.Client.Serializer.SystemTextJson/MapConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static TDictionary ReadDictionary<TDictionary>(ref Utf8JsonReader reader
{
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException();

while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
Expand Down Expand Up @@ -60,7 +60,7 @@ private static List<object> ReadArray(ref Utf8JsonReader reader)

return result;
}

private static object? ReadValue(ref Utf8JsonReader reader)
=> reader.TokenType switch
{
Expand All @@ -74,6 +74,4 @@ private static List<object> ReadArray(ref Utf8JsonReader reader)
JsonTokenType.None => null,
_ => throw new InvalidOperationException($"Unexpected value kind: {reader.TokenType}")
};


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SystemTextJsonSerializer : IGraphQLWebsocketJsonSerializer
public static JsonSerializerOptions DefaultJsonSerializerOptions => new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter(new ConstantCaseJsonNamingPolicy(), false)}
Converters = { new JsonStringEnumConverter(new ConstantCaseJsonNamingPolicy(), false) }
}.SetupImmutableConverter();

public JsonSerializerOptions Options { get; }
Expand Down
28 changes: 13 additions & 15 deletions src/GraphQL.Client/GraphQLHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJson
/// <inheritdoc />
public async Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(GraphQLRequest request, CancellationToken cancellationToken = default)
{
if (Options.UseWebSocketForQueriesAndMutations ||
!(Options.WebSocketEndPoint is null) && Options.EndPoint is null ||
Options.EndPoint.HasWebSocketScheme())
return await GraphQlHttpWebSocket.SendRequest<TResponse>(request, cancellationToken).ConfigureAwait(false);

return await SendHttpRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
return Options.UseWebSocketForQueriesAndMutations || Options.WebSocketEndPoint is not null && Options.EndPoint is null || Options.EndPoint.HasWebSocketScheme()
? await GraphQlHttpWebSocket.SendRequest<TResponse>(request, cancellationToken).ConfigureAwait(false)
: await SendHttpRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down Expand Up @@ -132,22 +129,23 @@ private async Task<GraphQLHttpResponse<TResponse>> SendHttpRequestAsync<TRespons
// error handling
string content = null;
if (contentStream != null)
using (var sr = new StreamReader(contentStream))
content = await sr.ReadToEndAsync().ConfigureAwait(false);
{
using var sr = new StreamReader(contentStream);
content = await sr.ReadToEndAsync().ConfigureAwait(false);
}

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

private GraphQLHttpWebSocket CreateGraphQLHttpWebSocket()
{
if(Options.WebSocketEndPoint is null && Options.EndPoint is null)
if (Options.WebSocketEndPoint is null && Options.EndPoint is null)
throw new InvalidOperationException("no endpoint configured");

var webSocketEndpoint = Options.WebSocketEndPoint ?? Options.EndPoint.GetWebSocketUri();
if (!webSocketEndpoint.HasWebSocketScheme())
throw new InvalidOperationException($"uri \"{webSocketEndpoint}\" is not a websocket endpoint");

return new GraphQLHttpWebSocket(webSocketEndpoint, this);
return webSocketEndpoint.HasWebSocketScheme()
? new GraphQLHttpWebSocket(webSocketEndpoint, this)
: throw new InvalidOperationException($"uri \"{webSocketEndpoint}\" is not a websocket endpoint");
}

#endregion
Expand Down Expand Up @@ -178,9 +176,9 @@ protected virtual void Dispose(bool disposing)
{
Debug.WriteLine($"Disposing GraphQLHttpClient on endpoint {Options.EndPoint}");
_cancellationTokenSource.Cancel();
if(_disposeHttpClient)
if (_disposeHttpClient)
HttpClient.Dispose();
if ( _lazyHttpWebSocket.IsValueCreated )
if (_lazyHttpWebSocket.IsValueCreated)
_lazyHttpWebSocket.Value.Dispose();
_cancellationTokenSource.Dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.Client/GraphQLHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public GraphQLHttpRequest(string query, object? variables = null, string? operat
{
}

public GraphQLHttpRequest(GraphQLRequest other): base(other)
public GraphQLHttpRequest(GraphQLRequest other) : base(other)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.Client/GraphQLHttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public GraphQLHttpResponse(GraphQLResponse<T> response, HttpResponseHeaders resp

public static class GraphQLResponseExtensions
{
public static GraphQLHttpResponse<T> ToGraphQLHttpResponse<T>(this GraphQLResponse<T> response, HttpResponseHeaders responseHeaders, HttpStatusCode statusCode) => new GraphQLHttpResponse<T>(response, responseHeaders, statusCode);
public static GraphQLHttpResponse<T> ToGraphQLHttpResponse<T>(this GraphQLResponse<T> response, HttpResponseHeaders responseHeaders, HttpStatusCode statusCode) => new(response, responseHeaders, statusCode);

/// <summary>
/// Casts <paramref name="response"/> to <see cref="GraphQLHttpResponse{T}"/>. Throws if the cast fails.
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQL.Client/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class UriExtensions
/// <param name="uri"></param>
/// <returns></returns>
public static bool HasWebSocketScheme(this Uri? uri) =>
!(uri is null) &&
uri is not null &&
(uri.Scheme.Equals("wss", StringComparison.OrdinalIgnoreCase) || uri.Scheme.Equals("ws", StringComparison.OrdinalIgnoreCase));

/// <summary>
Expand All @@ -33,6 +33,6 @@ public static Uri GetWebSocketUri(this Uri uri)
else
throw new NotSupportedException($"cannot infer websocket uri from uri scheme {uri.Scheme}");

return new UriBuilder(uri){Scheme = webSocketScheme}.Uri;
return new UriBuilder(uri) { Scheme = webSocketScheme }.Uri;
}
}
Loading