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 @@ -3667,15 +3667,13 @@ private bool IsRuntimeTypeAssignableTo(string typeCondition, string? runtimeType
return conditionType.IsAssignableFrom(runtimeType);
}

private static ErrorTrie? GetErrorTrie(SelectionPath sourcePath, ErrorTrie? errors)
private static ErrorTrie? GetErrorTrie(SelectionPath sourcePath, ErrorTrie? errorTrie)
{
if (errors is null || sourcePath.IsRoot)
if (errorTrie is null || sourcePath.IsRoot)
{
return errors;
return errorTrie;
}

var current = errors;

for (var i = 0; i < sourcePath.Length; i++)
{
var segment = sourcePath[i];
Expand All @@ -3687,13 +3685,20 @@ private bool IsRuntimeTypeAssignableTo(string typeCondition, string? runtimeType
continue;
}

if (!current.TryGetValue(segment.Name, out current))
if (!errorTrie.TryGetValue(segment.Name, out var childErrorTrie))
{
return null;
if (errorTrie.Error is not { } error)
{
return null;
}

return errorTrie.Count == 0 ? errorTrie : new ErrorTrie { Error = error };
}

errorTrie = childErrorTrie;
}

return current;
return errorTrie;
}

private CompositeResultElement GetStartObjectResultUnsafe(Path path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ public bool BuildResult(

if (sourceSnapshot.ValueKind is not JsonValueKind.Object)
{
var error = errorTrie?.FindFirstError();
var canExecutionContinue =
BuildResultForInvalidSource(
sourceSnapshot,
target,
resultSelectionSet,
error);
errorTrie);

if (!canExecutionContinue)
{
Expand Down Expand Up @@ -632,36 +631,45 @@ private bool BuildResultForInvalidSource(
SourceResultElementSnapshot source,
CompositeResultElement target,
ResultSelectionSet resultSelectionSet,
IError? error)
ErrorTrie? errorTrie)
{
var sourceValueKind = source.ValueKind;

if (sourceValueKind is JsonValueKind.Null && target.Type?.NamedType().IsSharedValueType() == true)
{
if (error is not null)
if (errorTrie?.FindFirstError() is { } sharedError)
{
PocketErrors(target.Path, resultSelectionSet, error);
PocketErrors(target.Path, resultSelectionSet, sharedError);
}

return true;
}

// A clean null source without an associated error is a legitimate
// "not found" state (e.g. a lookup that resolved to null), so each
// selected field is completed with its own nullability semantics
// instead of surfacing an unexpected execution error.
if (sourceValueKind is JsonValueKind.Null && error is null)
if (sourceValueKind is JsonValueKind.Null && errorTrie is null or { Count: 0, Error: null })
{
return CompleteNullSource(source, target, resultSelectionSet);
}

var fallbackError = error ??
var targetKind = target.ValueKind;
if (targetKind is JsonValueKind.Undefined && TryInitializeTargetObject(target))
{
targetKind = JsonValueKind.Object;
}

if (targetKind is JsonValueKind.Object
&& sourceValueKind is JsonValueKind.Null or JsonValueKind.Undefined
&& errorTrie is { Count: > 0 } or { Error: not null }
&& target.GetObjectContext() is { SelectionSet.DeclaringSelection: not null } objectContext)
{
return CompleteFailedLookup(errorTrie, target, objectContext, resultSelectionSet);
}

var fallbackError = errorTrie?.FindFirstError() ??
ErrorBuilder.New()
.SetMessage("Unexpected Execution Error")
.Build();

if (target.ValueKind is JsonValueKind.Undefined
&& !TryInitializeTargetObject(target))
if (targetKind is JsonValueKind.Undefined)
{
return BuildErrorResultForUndefinedTarget(
target,
Expand All @@ -673,6 +681,76 @@ private bool BuildResultForInvalidSource(
return BuildErrorResult(target, resultSelectionSet, fallbackError, target.CompactPath);
}

private bool CompleteFailedLookup(
ErrorTrie errorTrie,
CompositeResultElement target,
CompositeObjectContext objectContext,
ResultSelectionSet resultSelectionSet)
{
var path = target.Path;
var lookupError = errorTrie.Error;
if (errorTrie.Count > 0)
{
foreach (var responseName in resultSelectionSet.ResponseNames)
{
if (!TryGetFieldErrorTrie(responseName, out var fieldErrorTrie)
|| !objectContext.TryGetVisibleProperty(responseName, out _, out var selection)
|| selection.Field.IsIntrospectionField)
{
continue;
}

var fieldPath = path.Append(responseName);
ForwardError(fieldErrorTrie.Error, fieldPath);
if (fieldErrorTrie.Count > 0)
{
ForwardErrors(fieldErrorTrie, fieldPath, selection.Type, selection);
}

if (_errorHandlingMode is ErrorHandlingMode.Propagate && selection.IsNonNull)
{
return !PropagateNullValues(target);
}

if (errorTrie.Count == 1)
{
lookupError ??= fieldErrorTrie.Error;
}
}
}

foreach (var responseName in resultSelectionSet.ResponseNames)
{
if (!objectContext.TryGetVisibleProperty(responseName, out var field, out var selection)
|| selection.Field.IsIntrospectionField
|| field.ValueKind is not JsonValueKind.Undefined
|| (errorTrie.Count > 0 && TryGetFieldErrorTrie(responseName, out _)))
{
continue;
}

lookupError ??= errorTrie.FindFirstError();
ForwardError(lookupError, path.Append(responseName));
if (_errorHandlingMode is ErrorHandlingMode.Propagate && selection.IsNonNull)
{
return !PropagateNullValues(target);
}
}

return true;

bool TryGetFieldErrorTrie(string responseName, [NotNullWhen(true)] out ErrorTrie? fieldErrorTrie)
{
if (resultSelectionSet.HasSourceResponseNameMappings
&& resultSelectionSet.TryMapResponseName(responseName, out var mapping))
{
responseName = mapping.SourceResponseName;
}

return errorTrie.TryGetValue(responseName, out fieldErrorTrie);
}
}

/// <summary>
/// Completes the selection set of <paramref name="target"/> for a <c>null</c>
/// source, applying per-field nullability so that non-null fields produce a
Expand Down Expand Up @@ -1338,7 +1416,7 @@ private void ForwardErrors(
var (childPath, childType, childSelection) =
MapErrorPathSegment(segment, path, type, selection);

ForwardError(childErrorTrie, childPath);
ForwardError(childErrorTrie.Error, childPath);

if (childErrorTrie.Count > 0)
{
Expand All @@ -1365,7 +1443,7 @@ private void ForwardErrors(Stack<ErrorTrieFrame> stack)
var (childPath, childType, childSelection) =
MapErrorPathSegment(segment, frame.Path, frame.Type, frame.Selection);

ForwardError(childErrorTrie, childPath);
ForwardError(childErrorTrie.Error, childPath);

if (childErrorTrie.Count > 0)
{
Expand All @@ -1374,16 +1452,11 @@ private void ForwardErrors(Stack<ErrorTrieFrame> stack)
}
}

private void ForwardError(ErrorTrie errorTrie, Path path)
private void ForwardError(IError? error, Path path)
{
if (errorTrie.Error is { } error)
if (error is not null)
{
var errorWithPath = ErrorBuilder.FromError(error)
.SetPath(path)
.Build();
errorWithPath = _errorHandler.Handle(errorWithPath);

_store.AddError(errorWithPath);
_store.AddError(_errorHandler.Handle(error.WithPath(path)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using HotChocolate.Fusion.Execution.Nodes;
using ElementFlags = HotChocolate.Fusion.Text.Json.CompositeResultDocument.ElementFlags;

namespace HotChocolate.Fusion.Text.Json;

Expand Down Expand Up @@ -61,6 +63,27 @@ public bool TryGetProperty(
return TryGetRawObjectProperty(propertyName, out value, out selection);
}

public bool TryGetVisibleProperty(
string responseName,
out CompositeResultElement value,
[NotNullWhen(true)] out Selection? selection)
{
if (_selectionSet is not null && _selectionSet.TryGetSelection(responseName, out selection))
{
var cursor = _objectStartCursor + CompositeResultDocument.GetValueRowOffset(_selectionSet, selection);
var flags = _document._metaDb.GetFlags(cursor - 1);
if ((flags & (ElementFlags.IsInternal | ElementFlags.IsExcluded)) == ElementFlags.None)
{
value = new CompositeResultElement(_document, cursor);
return true;
}
}

value = default;
selection = null;
return false;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private bool TryGetRawObjectProperty(
ReadOnlySpan<byte> propertyName,
Expand Down
Loading
Loading