Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Theauxm committed Aug 8, 2024
1 parent ff073d8 commit c2fc818
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 87 deletions.
26 changes: 14 additions & 12 deletions src/GraphQL-Client/Configuration/GraphQLClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace GraphQL;
public class GraphQLClientConfiguration : IGraphQLClientConfiguration
{
public GraphQLClientConfiguration(
Uri baseAddress,
IGraphQLWebsocketJsonSerializer jsonSerializer,
GraphQLHttpClientOptions graphQLHttpClientOptions,
JsonSerializerOptions jsonSerializerOptions,
bool disposeHttpClient,
bool validateAssemblies,
HttpClient httpClient)
Uri baseAddress,
IGraphQLWebsocketJsonSerializer jsonSerializer,
GraphQLHttpClientOptions graphQLHttpClientOptions,
JsonSerializerOptions jsonSerializerOptions,
bool disposeHttpClient,
bool validateAssemblies,
HttpClient httpClient
)
{
HttpClient = httpClient;
HttpClient.BaseAddress = baseAddress;
Expand All @@ -27,20 +28,21 @@ public GraphQLClientConfiguration(
GraphQLHttpClient = new GraphQLHttpClient(
serializer: jsonSerializer,
options: graphQLHttpClientOptions,
httpClient: httpClient);
httpClient: httpClient
);
}

public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; init; }

public JsonSerializerOptions JsonSerializerOptions { get; init; }

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

public bool ValidateAssemblies { get; init; }

public GraphQLHttpClient GraphQLHttpClient { get; }

public HttpClient HttpClient { get; init; }

public bool DisposeHttpClient { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace GraphQL;

public class GraphQLClientConfigurationBuilder(Uri baseAddress)
{
public GraphQLClientConfiguration Build()
=> new(
public GraphQLClientConfiguration Build() =>
new(
baseAddress,
WebsocketJsonSerializer,
GraphQLClientOptions,
Expand All @@ -20,23 +20,25 @@ public GraphQLClientConfiguration Build()
HttpClient
);

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

public JsonSerializerOptions JsonSerializerOptions { get; set; } = new()
{
PropertyNameCaseInsensitive = true,
Converters =
public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; set; } =
new SystemTextJsonSerializer();

public JsonSerializerOptions JsonSerializerOptions { get; set; } =
new()
{
new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper),
new DateOnlyConverter()
}
};
PropertyNameCaseInsensitive = true,
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper),
new DateOnlyConverter()
}
};

public GraphQLHttpClientOptions GraphQLClientOptions { get; set; } = new();

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

public HttpClient HttpClient { get; set; } = new();

public bool DisposeHttpClient { get; set; } = false;
}
}
12 changes: 6 additions & 6 deletions src/GraphQL-Client/Configuration/IGraphQLClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace GraphQL;
public interface IGraphQLClientConfiguration
{
public IGraphQLWebsocketJsonSerializer WebsocketJsonSerializer { get; }

public JsonSerializerOptions JsonSerializerOptions { get; }

public GraphQLHttpClientOptions GraphQLClientOptions { get; }

public bool ValidateAssemblies { get; }

public GraphQLHttpClient GraphQLHttpClient { get; }

public HttpClient HttpClient { get; }

public bool DisposeHttpClient { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,42 @@ namespace GraphQL;

public static class GraphQLClientConfigurationExtensions
{
public static ISchema IntrospectSchema(this IGraphQLClientConfiguration configuration)
=> FetchSchemaJson(configuration)
.AsSchema();
public static ISchema IntrospectSchema(this IGraphQLClientConfiguration configuration) =>
FetchSchemaJson(configuration).AsSchema();

public static JsonElement FetchSchemaJson(IGraphQLClientConfiguration configuration)
{
var request = new GraphQLHttpRequest(IntrospectionQuery.Classic);

var response = Task.Run(async () =>
await configuration.GraphQLHttpClient.SendQueryAsync<JsonElement>(request)
).Result;
var response = Task.Run(
async () => await configuration.GraphQLHttpClient.SendQueryAsync<JsonElement>(request)
).Result;

if (response.Errors is not null)
{
throw new Exception(
$"Could not introspect ({configuration.HttpClient.BaseAddress}). Got the following errors: ({response.Errors})");
$"Could not introspect ({configuration.HttpClient.BaseAddress}). Got the following errors: ({response.Errors})"
);
}

return response.Data;
}

public static ISchema AsSchema(this JsonElement schemaJson)
{
var schemaResponse = schemaJson.Deserialize<GraphQLData>(new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() }
});
var schemaResponse = schemaJson.Deserialize<GraphQLData>(
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() }
}
);

if (schemaResponse?.__Schema is null)
{
throw new Exception(
$"Could not get data from __schema introspection. Data likely came back null. Schema: ({schemaJson.GetRawText()})");
$"Could not get data from __schema introspection. Data likely came back null. Schema: ({schemaJson.GetRawText()})"
);
}

var converter = new ASTConverter();
Expand All @@ -52,4 +55,4 @@ public static ISchema AsSchema(this JsonElement schemaJson)

return Schema.For(sdl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace GraphQL;

public static class GraphQLClientValidatorExtensions
{
public static GraphQLClientValidator ValidateAssemblies(this GraphQLClientValidator validator, params Assembly[] assemblies)
public static GraphQLClientValidator ValidateAssemblies(
this GraphQLClientValidator validator,
params Assembly[] assemblies
)
{
foreach (var assembly in assemblies)
{
Expand All @@ -26,4 +29,4 @@ public static GraphQLClientValidator ValidateAssemblies(this GraphQLClientValida

return validator;
}
}
}
11 changes: 6 additions & 5 deletions src/GraphQL-Client/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public static IServiceCollection AddGraphQLClientServices(
this IServiceCollection services,
Uri baseAddress,
Action<GraphQLClientConfigurationBuilder>? options = null,
params Assembly[] assemblies)
params Assembly[] assemblies
)
{
var configuration = BuildConfiguration(baseAddress, options);

var validator = new GraphQLClientValidator(configuration);

if (configuration.ValidateAssemblies)
Expand All @@ -33,10 +34,10 @@ private static IGraphQLClientConfiguration BuildConfiguration(
{
// 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();
return builder.Build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public interface IGenericGraphQLClientRequest
public List<string>? NestedLocation { get; }

public bool IsNested => NestedLocation is not null;
}
}
5 changes: 3 additions & 2 deletions src/GraphQL-Client/Interfaces/IGraphQLClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public TOut GetNestedResponse(JsonElement response, JsonSerializerOptions option
.Aggregate(response, (current, property) => current.GetProperty(property))
.GetRawText();

return JsonSerializer.Deserialize<TOut>(resultData, options) ?? throw new InvalidOperationException();
return JsonSerializer.Deserialize<TOut>(resultData, options)
?? throw new InvalidOperationException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ namespace GraphQL;

public class GraphQLClientExecutor(
IGraphQLClientValidator queryValidator,
IGraphQLClientConfiguration graphQlClientConfiguration)
: IGraphQLClientExecutor, IDisposable
IGraphQLClientConfiguration graphQlClientConfiguration
) : IGraphQLClientExecutor, IDisposable
{
private readonly GraphQLHttpClient _graphQLClient = graphQlClientConfiguration.GraphQLHttpClient;
private readonly GraphQLHttpClient _graphQLClient =
graphQlClientConfiguration.GraphQLHttpClient;

public async Task<TReturn> Run<TReturn>(IGraphQLClientRequest<TReturn> request)
{
Expand All @@ -20,21 +21,24 @@ public async Task<TReturn> Run<TReturn>(IGraphQLClientRequest<TReturn> request)
{
OperationType.Query => await _graphQLClient.SendQueryAsync<dynamic>(httpRequest),
OperationType.Mutation => await _graphQLClient.SendMutationAsync<dynamic>(httpRequest),
OperationType.Subscription => throw new ArgumentException("Subscriptions Not Supported."),
OperationType.Subscription
=> throw new ArgumentException("Subscriptions Not Supported."),
_ => throw new ArgumentOutOfRangeException()
};

return request.IsNested
? request.GetNestedResponse(response.Data, graphQlClientConfiguration.JsonSerializerOptions)
? request.GetNestedResponse(
response.Data,
graphQlClientConfiguration.JsonSerializerOptions
)
: response.Data;
}

public void Dispose()
{
_graphQLClient.Dispose();

if (graphQlClientConfiguration.DisposeHttpClient)
graphQlClientConfiguration.HttpClient?.Dispose();

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ namespace GraphQL;
public interface IGraphQLClientExecutor
{
public Task<TReturn> Run<TReturn>(IGraphQLClientRequest<TReturn> request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace GraphQL;

public class GraphQLClientValidator(IGraphQLClientConfiguration graphQLClientConfiguration) : IGraphQLClientValidator
public class GraphQLClientValidator(IGraphQLClientConfiguration graphQLClientConfiguration)
: IGraphQLClientValidator
{
public ISchema Schema { get; } = graphQLClientConfiguration.IntrospectSchema();

Expand All @@ -27,22 +28,19 @@ public async Task<OperationType> Validate(string query)
if (document.Definitions.FirstOrDefault() is not GraphQLOperationDefinition queryType)
throw new Exception($"Could not find any query definitions in: ({query})");

var validationOptions = new ValidationOptions
{
Schema = Schema,
Document = document
};
var validationOptions = new ValidationOptions { Schema = Schema, Document = document };

var (validationResult, _) = await _validator.ValidateAsync(validationOptions);

if (!validationResult.IsValid)
{
throw new Exception(
$"GraphQL Query ({query}) is not valid with the given schema. Got the following errors: ({validationResult.Errors}");
$"GraphQL Query ({query}) is not valid with the given schema. Got the following errors: ({validationResult.Errors}"
);
}

CachedQueries[query] = queryType.Operation;

return queryType.Operation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ namespace GraphQL;
public interface IGraphQLClientValidator
{
public Task<OperationType> Validate(string query);
}
}
14 changes: 6 additions & 8 deletions src/GraphQL-Client/Utils/Converters/DateOnlyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ public class DateOnlyConverter : JsonConverter<DateOnly>
public override DateOnly Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
=> reader.TokenType is JsonTokenType.Null or JsonTokenType.None
? new DateOnly()
JsonSerializerOptions options
) =>
reader.TokenType is JsonTokenType.Null or JsonTokenType.None
? new DateOnly()
: DateOnly.FromDateTime(reader.GetDateTime());

public override void Write(
Utf8JsonWriter writer,
DateOnly value,
JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToDateTime(new TimeOnly(0, 0, 0, 0), DateTimeKind.Utc));
}
}
}
2 changes: 1 addition & 1 deletion tests/GraphQL-Client.Integration/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global using NUnit.Framework;
global using NUnit.Framework;
6 changes: 2 additions & 4 deletions tests/GraphQL-Client.Integration/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ namespace GraphQL_Client.Integration;
public class Tests
{
[SetUp]
public void Setup()
{
}
public void Setup() { }

[Test]
public void Test1()
{
Assert.Pass();
}
}
}

0 comments on commit c2fc818

Please sign in to comment.