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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public sealed class InMemorySourceSchemaClient : ISourceSchemaClient

private readonly RequestExecutorProxy _executor;
private readonly JsonResultFormatter _formatter;
private readonly ErrorHandlingMode? _onError;
private bool _disposed;

/// <summary>
Expand All @@ -42,15 +43,20 @@ public sealed class InMemorySourceSchemaClient : ISourceSchemaClient
/// <param name="formatter">
/// The JSON result formatter used to serialize execution results.
/// </param>
/// <param name="onError">
/// The error handling mode requested by the source schema.
/// </param>
public InMemorySourceSchemaClient(
RequestExecutorProxy executor,
JsonResultFormatter formatter)
JsonResultFormatter formatter,
ErrorHandlingMode? onError = null)
{
ArgumentNullException.ThrowIfNull(executor);
ArgumentNullException.ThrowIfNull(formatter);

_executor = executor;
_formatter = formatter;
_onError = onError;
}

/// <inheritdoc />
Expand All @@ -68,7 +74,7 @@ public async ValueTask<SourceSchemaClientResponse> ExecuteAsync(
ObjectDisposedException.ThrowIf(_disposed, this);

ChunkedArrayWriter? buffer = null;
var operationRequest = BuildOperationRequest(context, request, ref buffer);
var operationRequest = BuildOperationRequest(context, request, _onError, ref buffer);

try
{
Expand Down Expand Up @@ -103,7 +109,7 @@ public async IAsyncEnumerable<BatchStreamResult> ExecuteBatchStreamAsync(
{
for (var i = 0; i < requests.Length; i++)
{
operationRequests[i] = BuildOperationRequest(context, requests[i], ref buffer);
operationRequests[i] = BuildOperationRequest(context, requests[i], _onError, ref buffer);
}
}
catch
Expand Down Expand Up @@ -221,6 +227,7 @@ public ValueTask DisposeAsync()
private static IOperationRequest BuildOperationRequest(
OperationPlanContext context,
SourceSchemaClientRequest request,
ErrorHandlingMode? onError,
ref ChunkedArrayWriter? buffer)
{
IFeatureCollection? features = null;
Expand All @@ -236,6 +243,7 @@ private static IOperationRequest BuildOperationRequest(
{
return OperationRequest.FromSourceText(
request.OperationSourceText,
errorHandlingMode: onError,
features: features);
}

Expand All @@ -247,13 +255,15 @@ private static IOperationRequest BuildOperationRequest(
{
return OperationRequest.FromSourceText(
request.OperationSourceText,
errorHandlingMode: onError,
variableValues: JsonDocument.Parse(sequence));
}

buffer ??= new ChunkedArrayWriter();
var cleanedJson = StripFileMarkers(buffer, sequence);
return OperationRequest.FromSourceText(
request.OperationSourceText,
errorHandlingMode: onError,
variableValues: JsonDocument.Parse(cleanedJson.AsSequence()),
features: features);
}
Expand All @@ -279,6 +289,7 @@ private static IOperationRequest BuildOperationRequest(
return VariableBatchRequest.FromSourceText(
request.OperationSourceText,
variableValues: JsonDocument.Parse(variableSequence),
errorHandlingMode: onError,
features: features);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using HotChocolate.Language;

namespace HotChocolate.Fusion.Execution.Clients;

/// <summary>
Expand All @@ -11,19 +13,29 @@ public sealed class InMemorySourceSchemaClientConfiguration : ISourceSchemaClien
/// </summary>
/// <param name="name">The name of the source schema.</param>
/// <param name="supportedOperations">The supported operation types.</param>
/// <param name="onError">
/// The error handling mode requested by the source schema.
/// </param>
public InMemorySourceSchemaClientConfiguration(
string name,
SupportedOperationType supportedOperations = SupportedOperationType.All)
SupportedOperationType supportedOperations = SupportedOperationType.All,
ErrorHandlingMode? onError = null)
{
ArgumentException.ThrowIfNullOrEmpty(name);

Name = name;
SupportedOperations = supportedOperations;
OnError = onError;
}

/// <inheritdoc />
public string Name { get; }

/// <inheritdoc />
public SupportedOperationType SupportedOperations { get; }

/// <summary>
/// Gets the error handling mode requested by the source schema.
/// </summary>
public ErrorHandlingMode? OnError { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ protected override ISourceSchemaClient CreateClient(
InMemorySourceSchemaClientConfiguration configuration)
{
var proxy = new RequestExecutorProxy(_executorProvider, _executorEvents, configuration.Name);
return new InMemorySourceSchemaClient(proxy, _formatter);
return new InMemorySourceSchemaClient(proxy, _formatter, configuration.OnError);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
using System.Net.Http.Headers;
using System.Text.Json;
using HotChocolate.Fusion.Execution.Clients;
using HotChocolate.Language;

namespace HotChocolate.Fusion.Configuration.Parsers;

/// <summary>
/// Built-in parser that claims the <c>http</c> transport and produces a
/// <see cref="SourceSchemaHttpClientConfiguration"/>.
/// <see cref="HttpSourceSchemaClientConfiguration"/>.
/// </summary>
internal sealed class HttpSourceSchemaClientConfigurationParser : ISourceSchemaClientConfigurationParser
{
Expand All @@ -27,16 +28,17 @@ public bool TryParse(
return false;
}

private static SourceSchemaHttpClientConfiguration CreateHttpClientConfiguration(
private static HttpSourceSchemaClientConfiguration CreateHttpClientConfiguration(
string schemaName,
JsonElement http)
{
var clientName = SourceSchemaHttpClientConfiguration.DefaultClientName;
var clientName = HttpSourceSchemaClientConfiguration.DefaultClientName;
var capabilities = SourceSchemaClientCapabilities.All;
var supportedOperations = SupportedOperationType.All;
ImmutableArray<MediaTypeWithQualityHeaderValue>? defaultAcceptHeaderValues = null;
ImmutableArray<MediaTypeWithQualityHeaderValue>? batchingAcceptHeaderValues = null;
ImmutableArray<MediaTypeWithQualityHeaderValue>? subscriptionAcceptHeaderValues = null;
ErrorHandlingMode? onError = null;

if (http.TryGetProperty("clientName", out var clientNameProperty)
&& clientNameProperty.ValueKind is JsonValueKind.String
Expand Down Expand Up @@ -90,6 +92,14 @@ private static SourceSchemaHttpClientConfiguration CreateHttpClientConfiguration
}
}

if (capabilitiesElement.TryGetProperty("onError", out var onErrorElement)
&& onErrorElement.ValueKind is JsonValueKind.String
&& onErrorElement.GetString() is { } onErrorValue
&& Enum.TryParse<ErrorHandlingMode>(onErrorValue, ignoreCase: true, out var parsedOnError))
{
onError = parsedOnError;
}

if (capabilitiesElement.TryGetProperty("subscriptions", out var subscriptionsElement))
{
if (subscriptionsElement.TryGetProperty("supported", out var supported)
Expand All @@ -112,12 +122,13 @@ private static SourceSchemaHttpClientConfiguration CreateHttpClientConfiguration
}
}

return new SourceSchemaHttpClientConfiguration(
return new HttpSourceSchemaClientConfiguration(
name: schemaName,
httpClientName: clientName,
baseAddress: new Uri(http.GetProperty("url").GetString()!),
supportedOperations: supportedOperations,
capabilities: capabilities,
onError: onError,
defaultAcceptHeaderValues: defaultAcceptHeaderValues,
batchingAcceptHeaderValues: batchingAcceptHeaderValues,
subscriptionAcceptHeaderValues: subscriptionAcceptHeaderValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using HotChocolate.Fusion.Execution;
using HotChocolate.Fusion.Execution.Clients;
using HotChocolate.Fusion.Execution.Nodes;
using HotChocolate.Language;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -27,6 +28,9 @@ public static partial class CoreFusionGatewayBuilderExtensions
/// <param name="capabilities">
/// The client capabilities.
/// </param>
/// <param name="onError">
/// The error handling mode requested by the source schema.
/// </param>
/// <param name="defaultAcceptHeaderValues">
/// The <c>Accept</c> header values sent in case of a single, non-Subscription GraphQL request.
/// </param>
Expand Down Expand Up @@ -54,6 +58,7 @@ public static IFusionGatewayBuilder AddHttpClientConfiguration(
Uri baseAddress,
SupportedOperationType supportedOperations = SupportedOperationType.All,
SourceSchemaClientCapabilities capabilities = SourceSchemaClientCapabilities.All,
ErrorHandlingMode? onError = null,
ImmutableArray<MediaTypeWithQualityHeaderValue>? defaultAcceptHeaderValues = null,
ImmutableArray<MediaTypeWithQualityHeaderValue>? batchingAcceptHeaderValues = null,
ImmutableArray<MediaTypeWithQualityHeaderValue>? subscriptionAcceptHeaderValues = null,
Expand All @@ -67,6 +72,7 @@ public static IFusionGatewayBuilder AddHttpClientConfiguration(
baseAddress,
supportedOperations,
capabilities,
onError,
defaultAcceptHeaderValues,
batchingAcceptHeaderValues,
subscriptionAcceptHeaderValues,
Expand Down Expand Up @@ -95,6 +101,9 @@ public static IFusionGatewayBuilder AddHttpClientConfiguration(
/// <param name="capabilities">
/// The client capabilities.
/// </param>
/// <param name="onError">
/// The error handling mode requested by the source schema.
/// </param>
/// <param name="defaultAcceptHeaderValues">
/// The <c>Accept</c> header values sent in case of a single, non-Subscription GraphQL request.
/// </param>
Expand Down Expand Up @@ -123,6 +132,7 @@ public static IFusionGatewayBuilder AddHttpClientConfiguration(
Uri baseAddress,
SupportedOperationType supportedOperations = SupportedOperationType.All,
SourceSchemaClientCapabilities capabilities = SourceSchemaClientCapabilities.All,
ErrorHandlingMode? onError = null,
ImmutableArray<MediaTypeWithQualityHeaderValue>? defaultAcceptHeaderValues = null,
ImmutableArray<MediaTypeWithQualityHeaderValue>? batchingAcceptHeaderValues = null,
ImmutableArray<MediaTypeWithQualityHeaderValue>? subscriptionAcceptHeaderValues = null,
Expand All @@ -137,12 +147,13 @@ public static IFusionGatewayBuilder AddHttpClientConfiguration(

return AddHttpClientConfiguration(
builder,
new SourceSchemaHttpClientConfiguration(
new HttpSourceSchemaClientConfiguration(
name,
httpClientName,
baseAddress,
supportedOperations,
capabilities,
onError,
defaultAcceptHeaderValues,
batchingAcceptHeaderValues,
subscriptionAcceptHeaderValues,
Expand All @@ -165,7 +176,7 @@ public static IFusionGatewayBuilder AddHttpClientConfiguration(
/// </returns>
public static IFusionGatewayBuilder AddHttpClientConfiguration(
this IFusionGatewayBuilder builder,
SourceSchemaHttpClientConfiguration configuration)
HttpSourceSchemaClientConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configuration);
Expand All @@ -187,7 +198,7 @@ public static IFusionGatewayBuilder AddHttpClientConfiguration(
/// </returns>
public static IFusionGatewayBuilder AddHttpClientConfiguration(
this IFusionGatewayBuilder builder,
Func<IServiceProvider, SourceSchemaHttpClientConfiguration> create)
Func<IServiceProvider, HttpSourceSchemaClientConfiguration> create)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(create);
Expand Down
Loading
Loading