Skip to content

Commit

Permalink
Fix error filters not being activated (#2774)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbirke authored Dec 23, 2020
1 parent 71c9064 commit da0f109
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static IRequestExecutorBuilder AddErrorFilter<T>(
throw new ArgumentNullException(nameof(factory));
}

return builder.ConfigureSchemaServices(s => s.AddSingleton(factory));
return builder.ConfigureSchemaServices(s => s.AddSingleton<IErrorFilter, T>(factory));
}

public static IRequestExecutorBuilder AddErrorFilter<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ await ExpectError(
}
return error;
}),

expectedErrorCount: 2);
}

Expand All @@ -58,6 +58,42 @@ await ExpectError(
.AddErrorFilter<DummyErrorFilter>());
}

[Fact]
public async Task AddClassErrorFilter_SchemaBuiltViaServiceExtensions_ErrorFilterWorks()
{
// arrange
var serviceCollection = new ServiceCollection();
var schema = await serviceCollection
.AddGraphQLServer()
.AddErrorFilter<DummyErrorFilter>()
.AddQueryType<Query>()
.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<Query>()
.BuildRequestExecutorAsync();

// act
var resp = await schema.ExecuteAsync("{ foo }");

// assert
resp.MatchSnapshot();
}

[Fact]
public async Task AddClassErrorFilterWithFactory()
{
Expand All @@ -80,7 +116,7 @@ private async Task ExpectError(

await TestHelper.ExpectError(
query,
b =>
b =>
{
configure(b);
b.AddErrorFilter(error =>
Expand All @@ -101,5 +137,10 @@ public IError OnError(IError error)
return error.WithCode("Foo123");
}
}

public class Query
{
public string GetFoo() => throw new Exception("FooError");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"errors": [
{
"message": "Unexpected Execution Error",
"locations": [
{
"line": 1,
"column": 3
}
],
"path": [
"foo"
],
"extensions": {
"code": "Foo123"
}
}
],
"data": {
"foo": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"errors": [
{
"message": "Unexpected Execution Error",
"locations": [
{
"line": 1,
"column": 3
}
],
"path": [
"foo"
],
"extensions": {
"code": "Foo123"
}
}
],
"data": {
"foo": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
<PropertyGroup>
<AssemblyName>HotChocolate.Execution.Tests</AssemblyName>
<RootNamespace>HotChocolate.Execution</RootNamespace>
<TargetFrameworks>net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Execution\HotChocolate.Execution.csproj" />
<ProjectReference Include="..\..\src\Subscriptions.InMemory\HotChocolate.Subscriptions.InMemory.csproj" />
<ProjectReference Include="..\StarWars\HotChocolate.StarWars.csproj" />
Expand Down

0 comments on commit da0f109

Please sign in to comment.