Skip to content
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

Omitted requireOneSlicingArgument if not set in listSize directive #7418

Open
wants to merge 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace HotChocolate.CostAnalysis.Types;
public sealed class ListSizeAttribute : ObjectFieldDescriptorAttribute
{
private readonly int? _assumedSize;
private readonly bool? _requireOneSlicingArgument;

/// <summary>
/// The maximum length of the list returned by this field.
Expand Down Expand Up @@ -44,7 +45,11 @@ public int AssumedSize
/// Whether to require a single slicing argument in the query. If that is not the case (i.e., if
/// none or multiple slicing arguments are present), the static analysis will throw an error.
/// </summary>
public bool RequireOneSlicingArgument { get; init; } = true;
public bool RequireOneSlicingArgument
{
get => _requireOneSlicingArgument ?? true;
init => _requireOneSlicingArgument = value;
}

protected override void OnConfigure(
IDescriptorContext context,
Expand All @@ -56,6 +61,6 @@ protected override void OnConfigure(
_assumedSize,
SlicingArguments?.ToImmutableArray(),
SizedFields?.ToImmutableArray(),
RequireOneSlicingArgument));
_requireOneSlicingArgument));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public ListSizeDirective(
SlicingArguments = slicingArguments ?? ImmutableArray<string>.Empty;
SizedFields = sizedFields ?? ImmutableArray<string>.Empty;
SlicingArgumentDefaultValue = slicingArgumentDefaultValue;

// https://ibm.github.io/graphql-specs/cost-spec.html#sec-requireOneSlicingArgument
// Per default, requireOneSlicingArgument is enabled,
// and has to be explicitly disabled if not desired for a field.
RequireOneSlicingArgument = SlicingArguments is { Length: > 0 } && (requireOneSlicingArgument ?? true);
RequireOneSlicingArgument = requireOneSlicingArgument;
}

/// <summary>
Expand Down Expand Up @@ -86,5 +82,5 @@ public ListSizeDirective(
/// <seealso href="https://ibm.github.io/graphql-specs/cost-spec.html#sec-requireOneSlicingArgument">
/// Specification URL
/// </seealso>
public bool RequireOneSlicingArgument { get; }
public bool? RequireOneSlicingArgument { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ private static DirectiveNode FormatValue(object value)
arguments.Add(new ArgumentNode(SizedFields, directive.SizedFields.ToListValueNode()));
}

arguments.Add(new ArgumentNode(RequireOneSlicingArgument, directive.RequireOneSlicingArgument));
if (directive.RequireOneSlicingArgument is not null)
{
arguments.Add(new ArgumentNode(RequireOneSlicingArgument, directive.RequireOneSlicingArgument.Value));
}

return new DirectiveNode(_name, arguments.ToImmutableArray());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CookieCrumble;
using HotChocolate.CostAnalysis.Types;
using HotChocolate.Language.Utilities;
using HotChocolate.Types;

namespace HotChocolate.CostAnalysis;
Expand Down Expand Up @@ -112,6 +114,36 @@ public void ListSize_ObjectFieldAttribute_AppliesDirective()
Assert.False(costDirective.RequireOneSlicingArgument);
}

[Fact]
public void ListSize_ObjectFieldAttribute_AppliesRequireOneSlicingArgumentCorrectly()
{
// arrange & act
var query = CreateSchema().GetType<ObjectType>(OperationTypeNames.Query);

var listSizeDirective1Sdl = query.Fields["examplesAssumedSizeOnly"]
.Directives
.Single(d => d.Type.Name == "listSize")
.AsSyntaxNode()
.Print();

var listSizeDirective2Sdl = query.Fields["examplesRequireOneSlicingArgumentTrue"]
.Directives
.Single(d => d.Type.Name == "listSize")
.AsSyntaxNode()
.Print();

var listSizeDirective3Sdl = query.Fields["examplesRequireOneSlicingArgumentFalse"]
.Directives
.Single(d => d.Type.Name == "listSize")
.AsSyntaxNode()
.Print();

// assert
listSizeDirective1Sdl.MatchInlineSnapshot("@listSize(assumedSize: 10)");
listSizeDirective2Sdl.MatchInlineSnapshot("@listSize(requireOneSlicingArgument: true)");
listSizeDirective3Sdl.MatchInlineSnapshot("@listSize(requireOneSlicingArgument: false)");
}

private static ISchema CreateSchema()
{
return SchemaBuilder.New()
Expand All @@ -128,6 +160,8 @@ private static ISchema CreateSchema()
[QueryType]
private static class Queries
{
private static readonly List<Example> List = [new Example(ExampleEnum.Member)];

[ListSize(
AssumedSize = 10,
SlicingArguments = ["first", "last"],
Expand All @@ -137,7 +171,28 @@ private static class Queries
// ReSharper disable once UnusedMember.Local
public static List<Example> GetExamples([Cost(8.0)] ExampleInput _)
{
return [new Example(ExampleEnum.Member)];
return List;
}

[ListSize(AssumedSize = 10)]
// ReSharper disable once UnusedMember.Local
public static List<Example> GetExamplesAssumedSizeOnly()
{
return List;
}

[ListSize(RequireOneSlicingArgument = true)]
// ReSharper disable once UnusedMember.Local
public static List<Example> GetExamplesRequireOneSlicingArgumentTrue()
{
return List;
}

[ListSize(RequireOneSlicingArgument = false)]
// ReSharper disable once UnusedMember.Local
public static List<Example> GetExamplesRequireOneSlicingArgumentFalse()
{
return List;
}
}

Expand Down
Loading