Skip to content

Cost assuming two levels of recursion for circular references #9548

@N-Olbert

Description

@N-Olbert

Product

Hot Chocolate

Version

16.0.rc.1.30

Link to minimal reproduction

See sample below

Steps to reproduce

See this minimal repo:

public static class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.AddGraphQL()
            .AddMutationType<Mutations>()
            .ModifyOptions(x => x.StrictValidation = false);

        var app = builder.Build();

        app.MapGraphQL();
        app.RunWithGraphQLCommands(args);
    }

    public class Mutations
    {
        public string SetField(Foo? input) => "Test";
    }

    public class  Foo
    {
        [Cost(1001)] // High cost to raise error
        public string Bar { get; set; } = "Test";

        [Cost(1002)] // High cost to raise error
        public Foo? Next { get; set; }
    }
}

For the mutation

mutation m($input: FooInput) {
  setField(input: $input)
}

with arguments

{ 
  "input": {
    "bar": "test"
   } 
}

this results in

{
  "errors": [
    {
      "message": "The maximum allowed field cost was exceeded.",
      "extensions": {
        "code": "HC0047",
        "fieldCost": 4007,
        "maxFieldCost": 1000
      }
    }
  ]
}

What is expected?

A cost of 0 (field) + 1001 for bar + 1002 for next + 1 for the arg = 2004.
The reported cost of 4007 suggests that two full recursion levels are being evaluated.
One might argue that this is not incorrect (and I could agree with that), since recursion is potentially infinite. However, I think assuming a single level of recursion is a better default than assuming two. Perhaps this should be configurable at all.

What is actually happening?

A cost of 4007 is emitted

Relevant log output

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions