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 @@ -4,6 +4,10 @@ namespace HotChocolate.Fusion.Types;

internal static class TypeDefinitionExtensions
{
public static bool IsSharedValueType(this ITypeDefinition type)
=> type is FusionComplexTypeDefinition { IsValueType: true }
or FusionUnionTypeDefinition { IsValueType: true };

public static bool ExistsInSchema(this ITypeDefinition type, string schemaName)
{
return type switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ internal Selection(
_flags |= Flags.EnumValue;
}

if (namedType is FusionObjectTypeDefinition { IsValueType: true }
or FusionInterfaceTypeDefinition { IsValueType: true }
or FusionUnionTypeDefinition { IsValueType: true })
if (namedType.IsSharedValueType())
{
_flags |= Flags.ValueTypeNamedType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ private bool BuildResultForInvalidSource(
{
var sourceValueKind = source.ValueKind;

if (sourceValueKind is JsonValueKind.Null && IsValueType(target.Type))
if (sourceValueKind is JsonValueKind.Null && target.Type?.NamedType().IsSharedValueType() == true)
{
if (error is not null)
{
Expand Down Expand Up @@ -892,24 +892,6 @@ private void PocketErrors(Path path, ResultSelectionSet resultSelectionSet, IErr
}
}

private static bool IsValueType(IType? type)
{
if (type is null)
{
return false;
}

var namedType = type.NamedType();

return namedType switch
{
FusionObjectTypeDefinition { IsValueType: true } => true,
FusionInterfaceTypeDefinition { IsValueType: true } => true,
FusionUnionTypeDefinition { IsValueType: true } => true,
_ => false
};
}

// TODO: When extracting an error from a path below the current field,
// we should try to use the path of the original error if it's
// part of what was selected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3190,11 +3190,11 @@ private static SelectionSetNode PruneNonValueTypeChildren(
{
var fieldNamedType = fieldDef.Type.NamedType();

if (fieldNamedType is FusionComplexTypeDefinition { IsValueType: true } valueType)
if (fieldNamedType.IsSharedValueType())
{
var pruned = PruneNonValueTypeChildren(
field.SelectionSet,
valueType,
fieldNamedType,
schema,
sourceSchemaName,
sourceAliases,
Expand All @@ -3221,13 +3221,17 @@ private static SelectionSetNode PruneNonValueTypeChildren(
// reaches an @interfaceObject stand-in, however, the path to that field
// must be kept so the result selection set can carry the opacity marker;
// recurse to preserve it instead of stripping the whole subtree.
// The path is also kept when the subtree reaches a value-type field,
// because a null with an error on such a field can only be pocketed
// for the fields this node selects below it.
if (sourceAliasSelectionSets?.Contains(field.SelectionSet) == true
|| (fieldNamedType is FusionComplexTypeDefinition complexFieldType
&& SubtreeContainsOpaqueStandIn(
field.SelectionSet,
complexFieldType,
schema,
sourceSchemaName)))
sourceSchemaName))
|| SubtreeContainsValueType(field.SelectionSet, fieldNamedType, schema))
{
var pruned = PruneNonValueTypeChildren(
field.SelectionSet,
Expand Down Expand Up @@ -3417,6 +3421,52 @@ private static bool SubtreeContainsOpaqueStandIn(
return false;
}

// Reports whether a selection set reaches a field that returns a value type anywhere in its
// subtree. Used to decide whether a non-value field must keep its child selections so the
// result selection set still describes what this node selects below the nested value type.
private static bool SubtreeContainsValueType(
SelectionSetNode selectionSet,
ITypeDefinition parentType,
FusionSchemaDefinition schema)
{
var complexType = parentType as IComplexTypeDefinition;

foreach (var selection in selectionSet.Selections)
{
if (selection is FieldNode { SelectionSet: not null } field)
{
if (complexType is not null
&& complexType.Fields.TryGetField(field.Name.Value, out var fieldDef))
{
var fieldNamedType = fieldDef.Type.NamedType();

if (fieldNamedType.IsSharedValueType()
|| SubtreeContainsValueType(field.SelectionSet, fieldNamedType, schema))
{
return true;
}
}
}
else if (selection is InlineFragmentNode inlineFragment)
{
var fragmentType = inlineFragment.TypeCondition is not null
&& schema.Types.TryGetType(
inlineFragment.TypeCondition.Name.Value,
allowInaccessibleFields: true,
out var resolvedType)
? resolvedType
: parentType;

if (SubtreeContainsValueType(inlineFragment.SelectionSet, fragmentType, schema))
{
return true;
}
}
}

return false;
}

private static bool DoVariablesContainUploadScalar(
IReadOnlyList<VariableDefinitionNode> variables,
FusionSchemaDefinition schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public OperationPlanner(
}
}

public static Version Version { get; } = new(2, 11, 0);
public static Version Version { get; } = new(2, 12, 0);

private static FrozenSet<string> CollectProtectedFieldNames(FusionSchemaDefinition schema)
{
Expand Down
Loading