From 4844fdc7a3d8ea6efb973929a1c3a689a344c50d Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Sun, 22 Nov 2020 23:05:47 +0100 Subject: [PATCH] Fixed Validation Resources (#2642) --- .../benchmark/Execution.Benchmarks/Program.cs | 31 ------------------- .../Core/src/Validation/ErrorHelper.cs | 16 ++++++++++ .../Properties/Resources.Designer.cs | 6 ++++ .../src/Validation/Properties/Resources.resx | 3 ++ .../Rules/MaxExecutionDepthVisitor.cs | 2 +- ...utionDepthShouldNotLeadToEmptyObjects.snap | 6 ++-- .../DocumentValidatorTests.cs | 4 +-- ...ValidatorTests.MaxDepthRuleIsIncluded.snap | 6 ++-- ...th4LevelsViaFragments_MaxDepthReached.snap | 6 ++-- ...elsWithInlineFragment_MaxDepthReached.snap | 6 ++-- ...pth3_QueryWith4Levels_MaxDepthReached.snap | 6 ++-- 11 files changed, 43 insertions(+), 49 deletions(-) diff --git a/src/HotChocolate/Core/benchmark/Execution.Benchmarks/Program.cs b/src/HotChocolate/Core/benchmark/Execution.Benchmarks/Program.cs index 02525f1db28..8ca96fe061a 100644 --- a/src/HotChocolate/Core/benchmark/Execution.Benchmarks/Program.cs +++ b/src/HotChocolate/Core/benchmark/Execution.Benchmarks/Program.cs @@ -9,36 +9,5 @@ class Program { static void Main(string[] args) => BenchmarkRunner.Run(typeof(Program).Assembly); - - /* - static async Task Main(string[] args) - { - var bench = new DefaultExecutionPipelineBenchmark(); - - for (int i = 0; i < 1000; i++) - { - Console.WriteLine("starting " + i); - var time = Stopwatch.StartNew(); - await bench.SchemaIntrospection(); - Console.WriteLine("finished " + i + " " + time.Elapsed); - } - - for (int i = 0; i < 1000; i++) - { - Console.WriteLine("starting " + i); - var time = Stopwatch.StartNew(); - await bench.GetHero(); - Console.WriteLine("finished " + i + " " + time.Elapsed); - } - - for (int i = 0; i < 50; i++) - { - Console.WriteLine("starting " + i); - var time = Stopwatch.StartNew(); - await bench.GetHeroWithFriendsFiveParallelRequests(); - Console.WriteLine("finished " + i + " " + time.Elapsed); - } - } - */ } } diff --git a/src/HotChocolate/Core/src/Validation/ErrorHelper.cs b/src/HotChocolate/Core/src/Validation/ErrorHelper.cs index 4af1881ea7c..81bd418cac9 100644 --- a/src/HotChocolate/Core/src/Validation/ErrorHelper.cs +++ b/src/HotChocolate/Core/src/Validation/ErrorHelper.cs @@ -590,5 +590,21 @@ internal static class ErrorHelper .SetExtension("detectedComplexity", detectedComplexity) .Build(); } + + public static IError MaxExecutionDepth( + this IDocumentValidatorContext context, + OperationDefinitionNode operation, + int allowedExecutionDepth, + int detectedExecutionDepth) + { + return ErrorBuilder.New() + .SetMessage( + Resources.ErrorHelper_MaxExecutionDepth, + detectedExecutionDepth, allowedExecutionDepth) + .AddLocation(operation) + .SetExtension("allowedExecutionDepth", allowedExecutionDepth) + .SetExtension("detectedExecutionDepth", detectedExecutionDepth) + .Build(); + } } } diff --git a/src/HotChocolate/Core/src/Validation/Properties/Resources.Designer.cs b/src/HotChocolate/Core/src/Validation/Properties/Resources.Designer.cs index f6b73d72a6e..61c24630991 100644 --- a/src/HotChocolate/Core/src/Validation/Properties/Resources.Designer.cs +++ b/src/HotChocolate/Core/src/Validation/Properties/Resources.Designer.cs @@ -260,6 +260,12 @@ internal class Resources { return ResourceManager.GetString("ErrorHelper_MaxOperationComplexity", resourceCulture); } } + + internal static string ErrorHelper_MaxExecutionDepth { + get { + return ResourceManager.GetString("ErrorHelper_MaxExecutionDepth", resourceCulture); + } + } internal static string HotChocolateValidationBuilderExtensions_MinimumAllowedValue { get { diff --git a/src/HotChocolate/Core/src/Validation/Properties/Resources.resx b/src/HotChocolate/Core/src/Validation/Properties/Resources.resx index 7a4b7a61acd..06d041ca59a 100644 --- a/src/HotChocolate/Core/src/Validation/Properties/Resources.resx +++ b/src/HotChocolate/Core/src/Validation/Properties/Resources.resx @@ -206,6 +206,9 @@ The GraphQL document has an operation complexity of {0} which exceeds the max allowed operation complexity of {1}. + + The GraphQL document has an execution depth of {0} which exceeds the max allowed execution depth of {1}. + The minimum allowed value is 1. diff --git a/src/HotChocolate/Core/src/Validation/Rules/MaxExecutionDepthVisitor.cs b/src/HotChocolate/Core/src/Validation/Rules/MaxExecutionDepthVisitor.cs index af780d2f90f..e49a2e441da 100644 --- a/src/HotChocolate/Core/src/Validation/Rules/MaxExecutionDepthVisitor.cs +++ b/src/HotChocolate/Core/src/Validation/Rules/MaxExecutionDepthVisitor.cs @@ -30,7 +30,7 @@ public MaxExecutionDepthVisitor(IMaxExecutionDepthOptionsAccessor options) if (_options.MaxAllowedExecutionDepth.HasValue && _options.MaxAllowedExecutionDepth < context.Max) { - context.Errors.Add(context.MaxOperationComplexity( + context.Errors.Add(context.MaxExecutionDepth( node, _options.MaxAllowedExecutionDepth.Value, context.Max)); return Break; } diff --git a/src/HotChocolate/Core/test/Execution.Tests/Integration/StarWarsCodeFirst/__snapshots__/StarWarsCodeFirstTests.ExecutionDepthShouldNotLeadToEmptyObjects.snap b/src/HotChocolate/Core/test/Execution.Tests/Integration/StarWarsCodeFirst/__snapshots__/StarWarsCodeFirstTests.ExecutionDepthShouldNotLeadToEmptyObjects.snap index 2205bff1307..b780026ea72 100644 --- a/src/HotChocolate/Core/test/Execution.Tests/Integration/StarWarsCodeFirst/__snapshots__/StarWarsCodeFirstTests.ExecutionDepthShouldNotLeadToEmptyObjects.snap +++ b/src/HotChocolate/Core/test/Execution.Tests/Integration/StarWarsCodeFirst/__snapshots__/StarWarsCodeFirstTests.ExecutionDepthShouldNotLeadToEmptyObjects.snap @@ -1,7 +1,7 @@ { "errors": [ { - "message": "The GraphQL document has an operation complexity of 6 which exceeds the max allowed operation complexity of 3.", + "message": "The GraphQL document has an execution depth of 6 which exceeds the max allowed execution depth of 3.", "locations": [ { "line": 2, @@ -9,8 +9,8 @@ } ], "extensions": { - "allowedComplexity": 3, - "detectedComplexity": 6 + "allowedExecutionDepth": 3, + "detectedExecutionDepth": 6 } } ] diff --git a/src/HotChocolate/Core/test/Validation.Tests/DocumentValidatorTests.cs b/src/HotChocolate/Core/test/Validation.Tests/DocumentValidatorTests.cs index cf01c7038f5..23980e9d132 100644 --- a/src/HotChocolate/Core/test/Validation.Tests/DocumentValidatorTests.cs +++ b/src/HotChocolate/Core/test/Validation.Tests/DocumentValidatorTests.cs @@ -624,8 +624,8 @@ public void MaxDepthRuleIsIncluded() t => { Assert.Equal( - "The GraphQL document has an operation complexity of 2 " + - "which exceeds the max allowed operation complexity of 1.", + "The GraphQL document has an execution depth of 2 " + + "which exceeds the max allowed execution depth of 1.", t.Message); }); } diff --git a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/DocumentValidatorTests.MaxDepthRuleIsIncluded.snap b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/DocumentValidatorTests.MaxDepthRuleIsIncluded.snap index ddce08322d9..402cd2f8377 100644 --- a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/DocumentValidatorTests.MaxDepthRuleIsIncluded.snap +++ b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/DocumentValidatorTests.MaxDepthRuleIsIncluded.snap @@ -1,6 +1,6 @@ [ { - "Message": "The GraphQL document has an operation complexity of 2 which exceeds the max allowed operation complexity of 1.", + "Message": "The GraphQL document has an execution depth of 2 which exceeds the max allowed execution depth of 1.", "Code": null, "Path": null, "Locations": [ @@ -10,8 +10,8 @@ } ], "Extensions": { - "allowedComplexity": 1, - "detectedComplexity": 2 + "allowedExecutionDepth": 1, + "detectedExecutionDepth": 2 }, "Exception": null } diff --git a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsViaFragments_MaxDepthReached.snap b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsViaFragments_MaxDepthReached.snap index dcf21d56c24..26f48143e7a 100644 --- a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsViaFragments_MaxDepthReached.snap +++ b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsViaFragments_MaxDepthReached.snap @@ -1,6 +1,6 @@ [ { - "Message": "The GraphQL document has an operation complexity of 4 which exceeds the max allowed operation complexity of 3.", + "Message": "The GraphQL document has an execution depth of 4 which exceeds the max allowed execution depth of 3.", "Code": null, "Path": null, "Locations": [ @@ -10,8 +10,8 @@ } ], "Extensions": { - "allowedComplexity": 3, - "detectedComplexity": 4 + "allowedExecutionDepth": 3, + "detectedExecutionDepth": 4 }, "Exception": null } diff --git a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsWithInlineFragment_MaxDepthReached.snap b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsWithInlineFragment_MaxDepthReached.snap index dcf21d56c24..26f48143e7a 100644 --- a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsWithInlineFragment_MaxDepthReached.snap +++ b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4LevelsWithInlineFragment_MaxDepthReached.snap @@ -1,6 +1,6 @@ [ { - "Message": "The GraphQL document has an operation complexity of 4 which exceeds the max allowed operation complexity of 3.", + "Message": "The GraphQL document has an execution depth of 4 which exceeds the max allowed execution depth of 3.", "Code": null, "Path": null, "Locations": [ @@ -10,8 +10,8 @@ } ], "Extensions": { - "allowedComplexity": 3, - "detectedComplexity": 4 + "allowedExecutionDepth": 3, + "detectedExecutionDepth": 4 }, "Exception": null } diff --git a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4Levels_MaxDepthReached.snap b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4Levels_MaxDepthReached.snap index dcf21d56c24..26f48143e7a 100644 --- a/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4Levels_MaxDepthReached.snap +++ b/src/HotChocolate/Core/test/Validation.Tests/__snapshots__/MaxDepthRuleTests.MaxDepth3_QueryWith4Levels_MaxDepthReached.snap @@ -1,6 +1,6 @@ [ { - "Message": "The GraphQL document has an operation complexity of 4 which exceeds the max allowed operation complexity of 3.", + "Message": "The GraphQL document has an execution depth of 4 which exceeds the max allowed execution depth of 3.", "Code": null, "Path": null, "Locations": [ @@ -10,8 +10,8 @@ } ], "Extensions": { - "allowedComplexity": 3, - "detectedComplexity": 4 + "allowedExecutionDepth": 3, + "detectedExecutionDepth": 4 }, "Exception": null }