Skip to content
Merged
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
@@ -1,6 +1,5 @@
using System.Buffers;
using System.Collections.Immutable;
using System.Runtime.InteropServices;
using HotChocolate.Fusion.Execution.Clients;
using HotChocolate.Language;

Expand Down Expand Up @@ -81,10 +80,6 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
RequiresFileUpload = operation.RequiresFileUpload
};

var index = 0;
var bufferLength = 0;
SourceSchemaResult[]? buffer = null;
SourceSchemaResult? singleResult = null;
var hasSomeErrors = false;

try
Expand All @@ -93,85 +88,32 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
var response = await client.ExecuteAsync(context, request, cancellationToken).ConfigureAwait(false);
context.TrackSourceSchemaClientResponse(this, response);

var totalPathCount = variables.Length;

for (var i = 0; i < variables.Length; i++)
{
totalPathCount += variables[i].AdditionalPaths.Length;
}

var initialBufferLength = Math.Max(totalPathCount, 4);

await foreach (var result in response.ReadAsResultStreamAsync(cancellationToken).ConfigureAwait(false))
{
if (index == 0)
var hasErrors = result.Errors is not null;
if (hasErrors)
{
singleResult = result;
index = 1;
hasSomeErrors = true;
}
else

try
{
if (buffer is null)
{
bufferLength = initialBufferLength;
buffer = ArrayPool<SourceSchemaResult>.Shared.Rent(bufferLength);
buffer[0] = singleResult!;
}

buffer[index++] = result;
context.AddPartialResult(
operation.Source,
result,
operation.ResultSelectionSet,
hasErrors);
}

if (result.Errors is not null)
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
hasSomeErrors = true;
return ExecutionStatus.Failed;
}
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
return ExecutionStatus.Failed;
}
catch (Exception exception)
{
diagnosticEvents.SourceSchemaTransportError(context, this, schemaName, exception);

if (buffer is not null && bufferLength > 0)
{
foreach (var result in buffer.AsSpan(0, index))
catch (Exception exception)
{
result?.Dispose();
diagnosticEvents.SourceSchemaStoreError(context, this, schemaName, exception);
context.AddErrors(exception, variables, operation.ResultSelectionSet);
return ExecutionStatus.Failed;
}

buffer.AsSpan(0, index).Clear();
ArrayPool<SourceSchemaResult>.Shared.Return(buffer);
}
else if (singleResult is not null)
{
singleResult.Dispose();
}

context.AddErrors(exception, variables, operation.ResultSelectionSet);
return ExecutionStatus.Failed;
}

try
{
if (buffer is not null)
{
context.AddPartialResults(
operation.Source,
buffer.AsSpan(0, index),
operation.ResultSelectionSet,
hasSomeErrors);
}
else if (singleResult is not null)
{
var firstResult = singleResult;
context.AddPartialResults(
operation.Source,
MemoryMarshal.CreateReadOnlySpan(ref firstResult, 1),
operation.ResultSelectionSet,
hasSomeErrors);
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
Expand All @@ -180,18 +122,10 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
}
catch (Exception exception)
{
diagnosticEvents.SourceSchemaStoreError(context, this, schemaName, exception);
diagnosticEvents.SourceSchemaTransportError(context, this, schemaName, exception);
context.AddErrors(exception, variables, operation.ResultSelectionSet);
return ExecutionStatus.Failed;
}
finally
{
if (buffer is not null)
{
buffer.AsSpan(0, index).Clear();
ArrayPool<SourceSchemaResult>.Shared.Return(buffer);
}
}

return hasSomeErrors ? ExecutionStatus.PartialSuccess : ExecutionStatus.Success;
}
Expand Down
Loading