1

How do I get MaxExecutionDepth to work in Hot Chocolate GraphQL? Here is my code:

    // Add GraphQL Services
    services.AddGraphQL(
        SchemaBuilder.New()
            // enable for authorization support
            .AddAuthorizeDirectiveType()
            .ModifyOptions(o => o.RemoveUnreachableTypes = true)
            .Create()
            .MakeExecutable(
                builder =>
                    builder
                        .UseDefaultPipeline()
                        .AddErrorFilter<UseExceptionMessageErrorFilter>()
                        .AddOptions(
                            new QueryExecutionOptions()
                            {
                                MaxExecutionDepth = 15
                            }))
            .Schema);

I've tested with this, even changing MaxExecutionDepth to 1, but I can still execute 20+ deep queries.

System.Cats.Lol
  • 1,620
  • 1
  • 26
  • 47

2 Answers2

2

Per the developer in the GitHub issue I created, was able to get it working like this:

            services.AddGraphQL(
                SchemaBuilder.New()
                    // enable for authorization support
                    .AddAuthorizeDirectiveType()
                    .ModifyOptions(o => o.RemoveUnreachableTypes = true)
                    .Create(),
                new QueryExecutionOptions()
                {
                    MaxExecutionDepth = ApiConfigurationConstants.MaxExecutionDepth
                });
            services.AddErrorFilter<UseExceptionMessageErrorFilter>();
System.Cats.Lol
  • 1,620
  • 1
  • 26
  • 47
1

Since the original question and answer are more than 3 years old, so for anyone else like myself who ran into this issue now, this is another option (V13 of HotChocolate):

In your Program.cs modify the GraphQl server section like this and add AddMaxExecutionDepthRule:

builder.Services.AddGraphQLServer()
    .AddQueryType<GraphQlQuery>()
    .AddProjections()
    .AddFiltering()
    .AddSorting()
    .AddMaxExecutionDepthRule(5);

Here 5 (or any other number) is the depth that you want.

Reference: https://chillicream.com/docs/hotchocolate/v13/security#execution-depth

Maximus2012
  • 1,799
  • 2
  • 12
  • 15