Skip to content

Commit

Permalink
Merge pull request #8 from Theauxm/feature/configuration_simplification
Browse files Browse the repository at this point in the history
1.2.2 -- Simplified Configuration to build in correct order after Bas…
  • Loading branch information
Theauxm authored Jun 18, 2024
2 parents 6cd90bd + 8cee7ca commit fb38797
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 65 deletions.
53 changes: 15 additions & 38 deletions src/GraphQL-Client/Configuration/GraphQLClientConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using GraphQL.Client.Abstractions.Websocket;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using GraphQL.Utils.Converters;

namespace GraphQL;

public class GraphQLClientConfiguration : IGraphQLClientConfiguration
{
public GraphQLClientConfiguration(
Uri baseAddress,
IGraphQLWebsocketJsonSerializer? jsonSerializer = null,
GraphQLHttpClientOptions? graphQLHttpClientOptions = null,
JsonSerializerOptions? jsonSerializerOptions = null,
bool disposeHttpClient = false,
bool validateAssemblies = false,
HttpClient? httpClient = null)
IGraphQLWebsocketJsonSerializer jsonSerializer,
GraphQLHttpClientOptions graphQLHttpClientOptions,
JsonSerializerOptions jsonSerializerOptions,
bool disposeHttpClient,
bool validateAssemblies,
HttpClient httpClient)
{
httpClient ??= new HttpClient();
httpClient.BaseAddress = baseAddress;

jsonSerializer ??= new SystemTextJsonSerializer();

jsonSerializerOptions ??= new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper),
new DateOnlyConverter()
}
};

graphQLHttpClientOptions ??= new GraphQLHttpClientOptions();

BaseAddress = baseAddress;
HttpClient = httpClient;
HttpClient.BaseAddress = baseAddress;

JsonSerializerOptions = jsonSerializerOptions;
GraphQLClientOptions = graphQLHttpClientOptions;
WebsocketJsonSerializer = jsonSerializer;
Expand All @@ -49,21 +30,17 @@ public GraphQLClientConfiguration(
httpClient: httpClient);
}

public Uri BaseAddress { get; set; }

public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; set; }
public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; init; }

public JsonSerializerOptions JsonSerializerOptions { get; set; }

public GraphQLHttpClientOptions GraphQLClientOptions { get; set; }
public JsonSerializerOptions JsonSerializerOptions { get; init; }

public IGraphQLClientExecutor? ClientExecutor { get; set; }
public GraphQLHttpClientOptions GraphQLClientOptions { get; init; }

public bool ValidateAssemblies { get; set; }
public bool ValidateAssemblies { get; init; }

public GraphQLHttpClient GraphQLHttpClient { get; internal set; }
public GraphQLHttpClient GraphQLHttpClient { get; }

public HttpClient HttpClient { get; set; }
public HttpClient HttpClient { get; init; }

public bool DisposeHttpClient { get; set; }
public bool DisposeHttpClient { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

namespace GraphQL;

public class GraphQLClientConfigurationBuilder
public class GraphQLClientConfigurationBuilder(Uri baseAddress)
{
internal readonly GraphQLClientConfiguration Configuration;

public GraphQLClientConfigurationBuilder(Uri baseAddress)
{
Configuration = new GraphQLClientConfiguration(
public GraphQLClientConfiguration Build()
=> new(
baseAddress,
WebsocketJsonSerializer,
GraphQLClientOptions,
Expand All @@ -22,7 +19,6 @@ public GraphQLClientConfigurationBuilder(Uri baseAddress)
ValidateAssemblies,
HttpClient
);
}

public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; set; } = new SystemTextJsonSerializer();

Expand Down
16 changes: 6 additions & 10 deletions src/GraphQL-Client/Configuration/IGraphQLClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ namespace GraphQL;

public interface IGraphQLClientConfiguration
{
public Uri BaseAddress { get; set; }

public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; set; }
public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; }

public JsonSerializerOptions JsonSerializerOptions { get; set; }
public JsonSerializerOptions JsonSerializerOptions { get; }

public GraphQLHttpClientOptions GraphQLClientOptions { get; set; }

public IGraphQLClientExecutor? ClientExecutor { get; set; }
public GraphQLHttpClientOptions GraphQLClientOptions { get; }

public bool ValidateAssemblies { get; set; }
public bool ValidateAssemblies { get; }

public GraphQLHttpClient GraphQLHttpClient { get; }

public HttpClient HttpClient { get; set; }
public HttpClient HttpClient { get; }

public bool DisposeHttpClient { get; set; }
public bool DisposeHttpClient { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ await configuration.GraphQLHttpClient.SendQueryAsync<JsonElement>(request)
if (response.Errors is not null)
{
throw new Exception(
$"Could not introspect ({configuration.BaseAddress}). Got the following errors: ({response.Errors})");
$"Could not introspect ({configuration.HttpClient.BaseAddress}). Got the following errors: ({response.Errors})");
}

return response.Data;
Expand Down
23 changes: 15 additions & 8 deletions src/GraphQL-Client/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ public static IServiceCollection AddGraphQLClientServices(
Action<GraphQLClientConfigurationBuilder>? options = null,
params Assembly[] assemblies)
{
// Create Builder to be used after Options are invoked
var builder = new GraphQLClientConfigurationBuilder(baseAddress);

// Options able to be null since all values have defaults
options?.Invoke(builder);

var configuration = builder.Configuration;
configuration.HttpClient.BaseAddress = baseAddress;
var configuration = BuildConfiguration(baseAddress, options);

var validator = new GraphQLClientValidator(configuration);

Expand All @@ -32,4 +25,18 @@ public static IServiceCollection AddGraphQLClientServices(
.AddSingleton<IGraphQLClientValidator>(validator)
.AddSingleton<IGraphQLClientExecutor>(executor);
}

private static IGraphQLClientConfiguration BuildConfiguration(
Uri baseAddress,
Action<GraphQLClientConfigurationBuilder>? options
)
{
// Create Builder to be used after Options are invoked
var builder = new GraphQLClientConfigurationBuilder(baseAddress);

// Options able to be null since all values have defaults
options?.Invoke(builder);

return builder.Build();
}
}
2 changes: 1 addition & 1 deletion src/GraphQL-Client/GraphQL-Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<AssemblyName>Theauxm.GraphQL.Client</AssemblyName>
</PropertyGroup>

Expand Down

0 comments on commit fb38797

Please sign in to comment.