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 @@ -55,15 +55,14 @@ public bool BuildResult(
ErrorTrie? errorTrie,
ResultSelectionSet resultSelectionSet)
{
var sourceValueKind = source.ValueKind;
var sourceSnapshot = source.CreateSnapshot();

if (sourceValueKind is not JsonValueKind.Object)
if (sourceSnapshot.ValueKind is not JsonValueKind.Object)
{
var error = errorTrie?.FindFirstError();
var canExecutionContinue =
BuildResultForInvalidSource(
source,
sourceValueKind,
sourceSnapshot,
target,
resultSelectionSet,
error);
Expand Down Expand Up @@ -125,19 +124,18 @@ public bool BuildResult(
sourceResponseName = selection.ResponseName;
}

var propertyValue = property.Value;
var propertyValueRow = propertyValue.GetValueRow();
var propertyValueKind = propertyValueRow.TokenType.ToValueKind();
var propertyValueSnapshot = property.Value.CreateSnapshot();
var propertyValueKind = propertyValueSnapshot.ValueKind;

if (errorTrie is null && propertyValueKind.IsScalarValue())
{
if (propertyValueKind is JsonValueKind.String && selection.IsEnumValue)
{
CompleteEnumValue(propertyValue, resultField, selection);
CompleteEnumValue(propertyValueSnapshot, resultField, selection);
continue;
}

resultField.SetLeafValue(propertyValue, propertyValueRow);
resultField.SetLeafValue(propertyValueSnapshot);
continue;
}

Expand All @@ -146,8 +144,7 @@ public bool BuildResult(

var childSet = resultSelectionSet.TryGetChild(selection.ResponseName);
if (!TryCompleteValue(
propertyValue,
propertyValueKind,
propertyValueSnapshot,
resultField,
errorTrieForResponseName,
selection,
Expand Down Expand Up @@ -175,9 +172,8 @@ public bool BuildResult(
continue;
}

var propertyValue = property.Value;
var propertyValueRow = propertyValue.GetValueRow();
var propertyValueKind = propertyValueRow.TokenType.ToValueKind();
var propertyValueSnapshot = property.Value.CreateSnapshot();
var propertyValueKind = propertyValueSnapshot.ValueKind;

// Fast path: when there are no errors and the source value is a
// scalar (string, number, bool) we can set it directly without
Expand All @@ -186,11 +182,11 @@ public bool BuildResult(
{
if (propertyValueKind is JsonValueKind.String && selection.IsEnumValue)
{
CompleteEnumValue(propertyValue, resultField, selection);
CompleteEnumValue(propertyValueSnapshot, resultField, selection);
continue;
}

resultField.SetLeafValue(propertyValue, propertyValueRow);
resultField.SetLeafValue(propertyValueSnapshot);
continue;
}

Expand All @@ -199,8 +195,7 @@ public bool BuildResult(

var childSet = resultSelectionSet.TryGetChild(selection.ResponseName);
if (!TryCompleteValue(
propertyValue,
propertyValueKind,
propertyValueSnapshot,
resultField,
errorTrieForResponseName,
selection,
Expand Down Expand Up @@ -502,12 +497,13 @@ private static bool PropagateNullValues(CompositeResultElement result)
}

private bool BuildResultForInvalidSource(
SourceResultElement source,
JsonValueKind sourceValueKind,
SourceResultElementSnapshot source,
CompositeResultElement target,
ResultSelectionSet resultSelectionSet,
IError? error)
{
var sourceValueKind = source.ValueKind;

if (sourceValueKind is JsonValueKind.Null && IsValueType(target.Type))
{
if (error is not null)
Expand Down Expand Up @@ -555,7 +551,7 @@ private bool BuildResultForInvalidSource(
/// <c>false</c>, if the execution needs to be halted.
/// </returns>
private bool CompleteNullSource(
SourceResultElement source,
SourceResultElementSnapshot source,
CompositeResultElement target,
ResultSelectionSet resultSelectionSet)
{
Expand All @@ -572,7 +568,6 @@ private bool CompleteNullSource(

if (!TryCompleteValue(
source,
JsonValueKind.Null,
fieldResult,
errorTrie: null,
selection,
Expand Down Expand Up @@ -788,15 +783,15 @@ private static bool IsValueType(IType? type)
// we should try to use the path of the original error if it's
// part of what was selected.
private bool TryCompleteValue(
SourceResultElement source,
JsonValueKind sourceValueKind,
SourceResultElementSnapshot source,
CompositeResultElement target,
ErrorTrie? errorTrie,
Selection selection,
IType type,
int depth,
ResultSelectionSet? resultSelectionSet)
{
var sourceValueKind = source.ValueKind;
var isNullOrUndefined = sourceValueKind is JsonValueKind.Null or JsonValueKind.Undefined;

if (type.Kind is TypeKind.NonNull)
Expand Down Expand Up @@ -913,7 +908,7 @@ private bool TryCompleteValue(
}

private bool TryCompleteList(
SourceResultElement source,
SourceResultElementSnapshot source,
CompositeResultElement target,
ErrorTrie? errorTrie,
Selection selection,
Expand Down Expand Up @@ -976,7 +971,8 @@ private bool TryCompleteList(
_store.AddError(errorWithPath);
}

var elementValueKind = element.ValueKind;
var elementSnapshot = element.CreateSnapshot();
var elementValueKind = elementSnapshot.ValueKind;
if (elementValueKind is JsonValueKind.Null or JsonValueKind.Undefined)
{
if (isNonNull && _propagateNullValues)
Expand All @@ -995,7 +991,7 @@ private bool TryCompleteList(
{
case TypeKind.List:
completed = TryCompleteList(
element,
elementSnapshot,
targetElement,
errorTrieForIndex,
selection,
Expand All @@ -1005,18 +1001,18 @@ private bool TryCompleteList(
break;

case TypeKind.Scalar:
targetElement.SetLeafValue(element);
targetElement.SetLeafValue(elementSnapshot);
completed = true;
break;

case TypeKind.Enum:
CompleteEnumValue(element, targetElement, selection);
CompleteEnumValue(elementSnapshot, targetElement, selection);
completed = true;
break;

case TypeKind.Interface or TypeKind.Union:
completed = TryCompleteAbstractValue(
element,
elementSnapshot,
targetElement,
errorTrieForIndex,
selection,
Expand All @@ -1029,7 +1025,7 @@ private bool TryCompleteList(
completed = TryCompleteObjectValue(
selection,
elementType,
element,
elementSnapshot,
errorTrieForIndex,
depth,
targetElement,
Expand All @@ -1056,7 +1052,7 @@ private bool TryCompleteList(
}

private static void CompleteEnumValue(
SourceResultElement source,
SourceResultElementSnapshot source,
CompositeResultElement target,
Selection selection)
{
Expand All @@ -1067,7 +1063,7 @@ private static void CompleteEnumValue(
// escape sequences, but GraphQL enum names are [A-Za-z0-9_] only, so an escaped
// payload cannot match any name and correctly falls through to masking.
if (selection.NamedType is FusionEnumTypeDefinition enumType
&& source.ValueKind is JsonValueKind.String
&& source.TokenType is JsonTokenType.String
&& enumType.Values.ContainsName(source.ValueSpan))
{
target.SetLeafValue(source);
Expand All @@ -1081,7 +1077,7 @@ private static void CompleteEnumValue(
private bool TryCompleteObjectValue(
Selection parentSelection,
IType type,
SourceResultElement source,
SourceResultElementSnapshot source,
ErrorTrie? errorTrie,
int depth,
CompositeResultElement target,
Expand All @@ -1101,7 +1097,7 @@ private bool TryCompleteObjectValue(
}

private bool TryCompleteObjectValue(
SourceResultElement source,
SourceResultElementSnapshot source,
CompositeResultElement target,
ErrorTrie? errorTrie,
Selection parentSelection,
Expand Down Expand Up @@ -1164,19 +1160,18 @@ private bool TryCompleteObjectValue(
sourceResponseName = selection.ResponseName;
}

var propertyValue = property.Value;
var propertyValueRow = propertyValue.GetValueRow();
var propertyValueKind = propertyValueRow.TokenType.ToValueKind();
var propertyValueSnapshot = property.Value.CreateSnapshot();
var propertyValueKind = propertyValueSnapshot.ValueKind;

if (errorTrie is null && propertyValueKind.IsScalarValue())
{
if (propertyValueKind is JsonValueKind.String && selection.IsEnumValue)
{
CompleteEnumValue(propertyValue, targetProperty, selection);
CompleteEnumValue(propertyValueSnapshot, targetProperty, selection);
continue;
}

targetProperty.SetLeafValue(propertyValue, propertyValueRow);
targetProperty.SetLeafValue(propertyValueSnapshot);
continue;
}

Expand All @@ -1185,8 +1180,7 @@ private bool TryCompleteObjectValue(

var childSet = resultSelectionSet.TryGetChild(selection.ResponseName, objectType);
if (!TryCompleteValue(
propertyValue,
propertyValueKind,
propertyValueSnapshot,
targetProperty,
errorTrieForResponseName,
selection,
Expand All @@ -1207,9 +1201,8 @@ private bool TryCompleteObjectValue(
continue;
}

var propertyValue = property.Value;
var propertyValueRow = propertyValue.GetValueRow();
var propertyValueKind = propertyValueRow.TokenType.ToValueKind();
var propertyValueSnapshot = property.Value.CreateSnapshot();
var propertyValueKind = propertyValueSnapshot.ValueKind;

// Fast path: when there are no errors and the source value is a
// scalar (string, number, bool) we can set it directly without
Expand All @@ -1218,11 +1211,11 @@ private bool TryCompleteObjectValue(
{
if (propertyValueKind is JsonValueKind.String && selection.IsEnumValue)
{
CompleteEnumValue(propertyValue, targetProperty, selection);
CompleteEnumValue(propertyValueSnapshot, targetProperty, selection);
continue;
}

targetProperty.SetLeafValue(propertyValue, propertyValueRow);
targetProperty.SetLeafValue(propertyValueSnapshot);
continue;
}

Expand All @@ -1231,8 +1224,7 @@ private bool TryCompleteObjectValue(

var childSet = resultSelectionSet?.TryGetChild(selection.ResponseName, objectType);
if (!TryCompleteValue(
propertyValue,
propertyValueKind,
propertyValueSnapshot,
targetProperty,
errorTrieForResponseName,
selection,
Expand All @@ -1249,7 +1241,7 @@ private bool TryCompleteObjectValue(
}

private bool TryCompleteAbstractValue(
SourceResultElement source,
SourceResultElementSnapshot source,
CompositeResultElement target,
ErrorTrie? errorTrie,
Selection selection,
Expand All @@ -1258,7 +1250,7 @@ private bool TryCompleteAbstractValue(
ResultSelectionSet? resultSelectionSet)
{
var isOpaque = resultSelectionSet?.ProducesOpaqueElements ?? false;
var objectType = GetType(type, source, isOpaque);
var objectType = GetType(type, source.Element, isOpaque);

if (!selection.IsInternal
&& objectType is IInaccessibleProvider { IsInaccessible: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,16 @@ internal void AssignCompositeValue(CompositeResultElement target, CompositeResul

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void AssignSourceValue(CompositeResultElement target, SourceResultElement source)
=> AssignSourceValue(target, source, source.GetValueRow());
=> AssignSourceValue(target, source._parent, source._cursor, source.GetValueRow());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void AssignSourceValue(
CompositeResultElement target,
SourceResultElement source,
SourceResultDocument.DbRow row)
SourceResultDocument source,
SourceResultDocument.Cursor sourceCursor,
SourceResultDocument.DbRow sourceRow)
{
var parent = source._parent;
var parent = source;

if (parent.Id == -1)
{
Expand All @@ -532,12 +533,10 @@ internal void AssignSourceValue(

Debug.Assert(_sources.Contains(parent), "Expected the source document of the source element to be registered.");

var tokenType = row.TokenType.ToElementTokenType();
var tokenType = sourceRow.TokenType.ToElementTokenType();

if (tokenType is ElementTokenType.StartObject or ElementTokenType.StartArray)
{
var sourceCursor = source._cursor;

_metaDb.ReplacePreserveParent(
cursor: target.Cursor,
tokenType: tokenType,
Expand All @@ -551,8 +550,8 @@ internal void AssignSourceValue(
_metaDb.ReplacePreserveParent(
cursor: target.Cursor,
tokenType: tokenType,
location: row.Location,
sizeOrLength: row.SizeOrLength,
location: sourceRow.Location,
sizeOrLength: sourceRow.SizeOrLength,
sourceDocumentId: parent.Id,
flags: ElementFlags.SourceResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,11 @@ internal void SetLeafValue(SourceResultElement source)
_parent.AssignSourceValue(this, source);
}

internal void SetLeafValue(SourceResultElement source, SourceResultDocument.DbRow row)
internal void SetLeafValue(SourceResultElementSnapshot source)
{
CheckValidInstance();

_parent.AssignSourceValue(this, source, row);
_parent.AssignSourceValue(this, source._parent, source._cursor, source._row);
}

internal void SetNullValue()
Expand Down
Loading
Loading