Hot Chocolate GraphQL – MaxExecutionDepth not working

Hot Chocolate GraphQL – MaxExecutionDepth not working


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.

Share
Improve this question

4

  • Have you tried to move use setting in the Create call

    – Guru Stron

    May 6, 2020 at 14:26

  • @GuruStron not sure what you mean; Create() takes no params.

    – System.Cats.Lol

    May 6, 2020 at 14:28

  • Oh, i misread API, sorry.

    – Guru Stron

    May 6, 2020 at 14:29

  • Added a answer, please try it out.

    – Guru Stron

    May 6, 2020 at 14:44

2 Answers
2

Reset to default


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>();

Share
Improve this answer


0

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

Share
Improve this answer



Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *