Skip to content

Commit

Permalink
Fixed Complexity Validation Status Code (#5117)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jun 3, 2022
1 parent d4356d3 commit a6450c6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
Expand Up @@ -154,6 +154,33 @@ public async Task SingleRequest_GetHeroName_Casing_Is_Preserved()
result.MatchSnapshot();
}

[Fact]
public async Task Complexity_Exceeded()
{
// arrange
TestServer server = CreateStarWarsServer(
configureServices: c => c.AddGraphQLServer().ModifyRequestOptions(o=>
{
o.Complexity.Enable = true;
o.Complexity.MaximumAllowed = 1;
}));

// act
ClientQueryResult result =
await server.PostAsync(new ClientQueryRequest
{
Query = @"
{
HERO: hero {
name
}
}"
});

// assert
result.MatchSnapshot();
}

[Fact]
public async Task SingleRequest_GetHeroName_With_EnumVariable()
{
Expand Down
@@ -0,0 +1,16 @@
{
"ContentType": "application/json; charset=utf-8",
"StatusCode": "BadRequest",
"Data": null,
"Errors": [
{
"message": "The maximum allowed operation complexity was exceeded.",
"extensions": {
"complexity": 2,
"allowedComplexity": 1,
"code": "HC0047"
}
}
],
"Extensions": null
}
10 changes: 7 additions & 3 deletions src/HotChocolate/Core/src/Execution/ErrorHelper.cs
Expand Up @@ -198,9 +198,13 @@ internal static class ErrorHelper
ErrorCodes.Execution.ComplexityExceeded,
extensions: new Dictionary<string, object?>
{
{ nameof(complexity), complexity },
{ nameof(allowedComplexity), allowedComplexity }
}));
{ nameof(complexity), complexity },
{ nameof(allowedComplexity), allowedComplexity }
}),
contextData: new Dictionary<string, object?>
{
{ WellKnownContextData.ValidationErrors, true }
});

public static IQueryResult StateInvalidForComplexityAnalyzer() =>
QueryResultBuilder.CreateError(
Expand Down

0 comments on commit a6450c6

Please sign in to comment.