Skip to content

Aligned to GraphQL Java Field Merging Rule #8231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions src/All.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Project Path="HotChocolate/Core/test/Subscriptions.Redis.Tests/HotChocolate.Subscriptions.Redis.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Subscriptions.Tests/HotChocolate.Subscriptions.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Types.Analyzers.Tests/HotChocolate.Types.Analyzers.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Types.Analyzer.Integration.Tests/HotChocolate.Types.Analyzer.Integration.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Types.CursorPagination.Tests/HotChocolate.Types.CursorPagination.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Types.Json.Tests/HotChocolate.Types.Json.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Types.Mutations.Tests/HotChocolate.Types.Mutations.Tests.csproj" />
Expand All @@ -130,9 +131,7 @@
<Project Path="HotChocolate/Core/test/Types.Tests/HotChocolate.Types.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Utilities/HotChocolate.Tests.Utilities.csproj" />
<Project Path="HotChocolate/Core/test/Validation.Tests/HotChocolate.Validation.Tests.csproj" />
</Folder>
<Folder Name="/HotChocolate/Core/test/Types.Analyzer.Integration.Tests/">
<Project Path="HotChocolate/Core/test/Types.Analyzer.Integration.Tests/HotChocolate.Types.Analyzers.Tests.csproj" />
<Project Path="HotChocolate/Core/test/Validation.Benchmarks/HotChocolate.Validation.Benchmarks.csproj" />
</Folder>
<Folder Name="/HotChocolate/CostAnalysis/" />
<Folder Name="/HotChocolate/CostAnalysis/src/">
Expand Down
3 changes: 2 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageVersion Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.7.8" />
<PackageVersion Include="Basic.Reference.Assemblies.Net90" Version="1.7.8" />
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="ChilliCream.Nitro.App" Version="$(NitroVersion)" />
<PackageVersion Include="ChilliCream.Testing.Utilities" Version="0.2.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
Expand Down Expand Up @@ -132,4 +133,4 @@
<PackageVersion Include="Roslynator.CodeAnalysis.Analyzers" Version="4.12.4" />
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.12.4" />
</ItemGroup>
</Project>
</Project>
7 changes: 4 additions & 3 deletions src/HotChocolate/Core/HotChocolate.Core.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@
<Project Path="test/Types.Tests/HotChocolate.Types.Tests.csproj" />
<Project Path="test/Utilities/HotChocolate.Tests.Utilities.csproj" />
<Project Path="test/Validation.Tests/HotChocolate.Validation.Tests.csproj" />
<Project Path="test/Types.Analyzer.Integration.Tests/HotChocolate.Types.Analyzer.Integration.Tests.csproj" />
</Folder>
<Folder Name="/test/Types.Analyzer.Integration.Tests/">
<Project Path="test/Types.Analyzer.Integration.Tests/HotChocolate.Types.Analyzers.Tests.csproj" />
<Folder Name="/test/Validation.Benchmarks/">
<Project Path="test/Validation.Benchmarks/HotChocolate.Validation.Benchmarks.csproj" />
</Folder>
</Solution>
</Solution>
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public static IValidationBuilder AddDocumentRules(
/// </summary>
public static IValidationBuilder AddFieldRules(
this IValidationBuilder builder)
{
return builder
.TryAddValidationRule<LeafFieldSelectionsRule>()
.TryAddValidationRule<OverlappingFieldsCanBeMergedRule>();
}

public static IValidationBuilder AddFieldRulesLegacy(
this IValidationBuilder builder)
{
return builder.TryAddValidationVisitor<FieldVisitor>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<PackageId>HotChocolate.Validation</PackageId>
<AssemblyName>HotChocolate.Validation</AssemblyName>
<RootNamespace>HotChocolate.Validation</RootNamespace>
<Description></Description>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,6 +12,7 @@

<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.Validation.Tests" />
<InternalsVisibleTo Include="HotChocolate.Validation.Benchmarks" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using HotChocolate.Language;
using HotChocolate.Types;

namespace HotChocolate.Validation.Rules;

internal sealed class LeafFieldSelectionsRule : IDocumentValidatorRule
{
public ushort Priority => ushort.MaxValue;

public bool IsCacheable => true;

public void Validate(IDocumentValidatorContext context, DocumentNode document)
{
foreach (var operation in document.Definitions)
{
if(operation is not OperationDefinitionNode operationDef)
{
continue;
}

if (context.Schema.GetOperationType(operationDef.Operation) is { } rootType)
{
ValidateSelectionSet(context, operationDef.SelectionSet, rootType);
}
}
}

private void ValidateSelectionSet(IDocumentValidatorContext context, SelectionSetNode selectionSet, IType type)
{
foreach (var selection in selectionSet.Selections)
{
if (selection is FieldNode field)
{
ValidateField(context, field, type);
}
else if (selection is InlineFragmentNode inlineFrag)
{
var typeCondition = inlineFrag.TypeCondition is null
? type
: context.Schema.GetType<INamedType>(inlineFrag.TypeCondition.Name.Value);
ValidateSelectionSet(context, inlineFrag.SelectionSet, typeCondition);
}
else if (selection is FragmentSpreadNode spread
&& context.Fragments.TryGetValue(spread.Name.Value, out var frag))
{
if (context.VisitedFragments.Add(spread.Name.Value))
{
var typeCondition = context.Schema.GetType<INamedType>(frag.TypeCondition.Name.Value);
ValidateSelectionSet(context, frag.SelectionSet, typeCondition);
context.VisitedFragments.Remove(spread.Name.Value);
}
}
}
}

private void ValidateField(IDocumentValidatorContext context, FieldNode field, IType parentType)
{
if (parentType is not IComplexOutputType complex ||
!complex.Fields.TryGetField(field.Name.Value, out var fieldDef))
{
// handled by other rules like KnownFieldNames
return;
}

var namedType = fieldDef.Type.NamedType();

if (namedType.IsLeafType())
{
if (field.SelectionSet is not null)
{
context.ReportError(
ErrorBuilder.New()
.SetMessage($"Field \"{field.Name.Value}\" must not have a selection since type \"{namedType.Name}\" has no subfields.")
.AddLocation(field)
.Build());
}
}
else
{
if (field.SelectionSet is null)
{
context.ReportError(
ErrorBuilder.New()
.SetMessage($"Field \"{field.Name.Value}\" of type \"{namedType.Name}\" must have a selection of subfields. Did you mean \"{field.Name.Value} {{ ... }}\"?")
.AddLocation(field)
.Build());
}
else
{
ValidateSelectionSet(context, field.SelectionSet, fieldDef.Type);
}
}
}
}
Loading
Loading