diff --git a/src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.ErrorFilter.cs b/src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.ErrorFilter.cs index c6f6316e9c3..c0ab8d3fcb4 100644 --- a/src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.ErrorFilter.cs +++ b/src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.ErrorFilter.cs @@ -42,7 +42,7 @@ public static IRequestExecutorBuilder AddErrorFilter( throw new ArgumentNullException(nameof(factory)); } - return builder.ConfigureSchemaServices(s => s.AddSingleton(factory)); + return builder.ConfigureSchemaServices(s => s.AddSingleton(factory)); } public static IRequestExecutorBuilder AddErrorFilter( diff --git a/src/HotChocolate/Core/test/Execution.Tests/Errors/ErrorHandlerTests.cs b/src/HotChocolate/Core/test/Execution.Tests/Errors/ErrorHandlerTests.cs index c2d294761ba..c09e398b30e 100644 --- a/src/HotChocolate/Core/test/Execution.Tests/Errors/ErrorHandlerTests.cs +++ b/src/HotChocolate/Core/test/Execution.Tests/Errors/ErrorHandlerTests.cs @@ -41,7 +41,7 @@ await ExpectError( } return error; }), - + expectedErrorCount: 2); } @@ -58,6 +58,42 @@ await ExpectError( .AddErrorFilter()); } + [Fact] + public async Task AddClassErrorFilter_SchemaBuiltViaServiceExtensions_ErrorFilterWorks() + { + // arrange + var serviceCollection = new ServiceCollection(); + var schema = await serviceCollection + .AddGraphQLServer() + .AddErrorFilter() + .AddQueryType() + .BuildRequestExecutorAsync(); + + // act + var resp = await schema.ExecuteAsync("{ foo }"); + + // assert + resp.MatchSnapshot(); + } + + [Fact] + public async Task AddClassErrorFilterUsingFactory_SchemaBuiltViaServiceExtensions_ErrorFilterWorks() + { + // arrange + var serviceCollection = new ServiceCollection(); + var schema = await serviceCollection + .AddGraphQLServer() + .AddErrorFilter(f => new DummyErrorFilter()) + .AddQueryType() + .BuildRequestExecutorAsync(); + + // act + var resp = await schema.ExecuteAsync("{ foo }"); + + // assert + resp.MatchSnapshot(); + } + [Fact] public async Task AddClassErrorFilterWithFactory() { @@ -80,7 +116,7 @@ private async Task ExpectError( await TestHelper.ExpectError( query, - b => + b => { configure(b); b.AddErrorFilter(error => @@ -101,5 +137,10 @@ public IError OnError(IError error) return error.WithCode("Foo123"); } } + + public class Query + { + public string GetFoo() => throw new Exception("FooError"); + } } } diff --git a/src/HotChocolate/Core/test/Execution.Tests/Errors/__snapshots__/ErrorHandlerTests.AddClassErrorFilterUsingFactory_SchemaBuiltViaServiceExtensions_ErrorFilterWorks.snap b/src/HotChocolate/Core/test/Execution.Tests/Errors/__snapshots__/ErrorHandlerTests.AddClassErrorFilterUsingFactory_SchemaBuiltViaServiceExtensions_ErrorFilterWorks.snap new file mode 100644 index 00000000000..61e9f12e967 --- /dev/null +++ b/src/HotChocolate/Core/test/Execution.Tests/Errors/__snapshots__/ErrorHandlerTests.AddClassErrorFilterUsingFactory_SchemaBuiltViaServiceExtensions_ErrorFilterWorks.snap @@ -0,0 +1,22 @@ +{ + "errors": [ + { + "message": "Unexpected Execution Error", + "locations": [ + { + "line": 1, + "column": 3 + } + ], + "path": [ + "foo" + ], + "extensions": { + "code": "Foo123" + } + } + ], + "data": { + "foo": null + } +} diff --git a/src/HotChocolate/Core/test/Execution.Tests/Errors/__snapshots__/ErrorHandlerTests.AddClassErrorFilter_SchemaBuiltViaServiceExtensions_ErrorFilterWorks.snap b/src/HotChocolate/Core/test/Execution.Tests/Errors/__snapshots__/ErrorHandlerTests.AddClassErrorFilter_SchemaBuiltViaServiceExtensions_ErrorFilterWorks.snap new file mode 100644 index 00000000000..61e9f12e967 --- /dev/null +++ b/src/HotChocolate/Core/test/Execution.Tests/Errors/__snapshots__/ErrorHandlerTests.AddClassErrorFilter_SchemaBuiltViaServiceExtensions_ErrorFilterWorks.snap @@ -0,0 +1,22 @@ +{ + "errors": [ + { + "message": "Unexpected Execution Error", + "locations": [ + { + "line": 1, + "column": 3 + } + ], + "path": [ + "foo" + ], + "extensions": { + "code": "Foo123" + } + } + ], + "data": { + "foo": null + } +} diff --git a/src/HotChocolate/Core/test/Execution.Tests/HotChocolate.Execution.Tests.csproj b/src/HotChocolate/Core/test/Execution.Tests/HotChocolate.Execution.Tests.csproj index f86dfdbb94a..0475cce49f6 100644 --- a/src/HotChocolate/Core/test/Execution.Tests/HotChocolate.Execution.Tests.csproj +++ b/src/HotChocolate/Core/test/Execution.Tests/HotChocolate.Execution.Tests.csproj @@ -3,9 +3,11 @@ HotChocolate.Execution.Tests HotChocolate.Execution + net5.0 +