Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error filters not being activated #2774

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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