Skip to content

Commit

Permalink
Fix error filters not being activated
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbirke committed Dec 21, 2020
1 parent 71c9064 commit 5c7cff0
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 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 @@ -10,14 +10,14 @@ namespace HotChocolate
public static class ExecutionSchemaExtensions
{
public static IRequestExecutor MakeExecutable(
this ISchema schema)
this ISchema schema, IServiceCollection? serviceCollection = null)
{
if (schema is null)
{
throw new ArgumentNullException(nameof(schema));
}

return new ServiceCollection()
return (serviceCollection ?? new ServiceCollection())
.AddGraphQL()
.Configure(o => o.Schema = schema)
.Services
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,34 @@ await ExpectError(
.AddErrorFilter<DummyErrorFilter>());
}

[Fact]
public async Task AddClassErrorFilter_SchemaBuiltViaServiceExtensions_ErrorFilterWorks()
{
var serviceCollection = new ServiceCollection();
var schema = await serviceCollection
.AddGraphQLServer()
.AddErrorFilter<DummyErrorFilter>()
.AddQueryType<Query>()
.BuildSchemaAsync();

var resp = await schema.MakeExecutable(serviceCollection).ExecuteAsync("{ foo }");
resp.MatchSnapshot();
}

[Fact]
public async Task AddClassErrorFilterUsingFactory_SchemaBuiltViaServiceExtensions_ErrorFilterWorks()
{
var serviceCollection = new ServiceCollection();
var schema = await serviceCollection
.AddGraphQLServer()
.AddErrorFilter(f => new DummyErrorFilter())
.AddQueryType<Query>()
.BuildSchemaAsync();

var resp = await schema.MakeExecutable(serviceCollection).ExecuteAsync("{ foo }");
resp.MatchSnapshot();
}

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

await TestHelper.ExpectError(
query,
b =>
b =>
{
configure(b);
b.AddErrorFilter(error =>
Expand All @@ -101,5 +129,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 All @@ -25,5 +27,5 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit 5c7cff0

Please sign in to comment.