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

Allow custom error transformation on GraphQLEngine #119 #120 #122

Merged
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
@@ -0,0 +1,22 @@
using System.Linq;
using GraphQL.Conventions.Execution;

namespace GraphQL.Conventions.Adapters.Engine.ErrorTransformations
{
public class DefaultErrorTransformation : IErrorTransformation
{
public ExecutionErrors Transform(ExecutionErrors errors)
=> errors.Aggregate(new ExecutionErrors(), (result, executionError) =>
{
var exception = new FieldResolutionException(executionError);
var error = new ExecutionError(exception.Message, exception);
foreach (var location in executionError.Locations ?? Enumerable.Empty<ErrorLocation>())
{
error.AddLocation(location.Line, location.Column);
}
error.Path = executionError.Path;
result.Add(error);
return result;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace GraphQL.Conventions.Adapters.Engine.ErrorTransformations
{
public interface IErrorTransformation
{
ExecutionErrors Transform(ExecutionErrors errors);
}
}
25 changes: 11 additions & 14 deletions src/GraphQL.Conventions/Adapters/Engine/GraphQLEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using GraphQL.Conventions.Adapters;
using GraphQL.Conventions.Adapters.Engine.ErrorTransformations;
using GraphQL.Conventions.Adapters.Engine.Listeners.DataLoader;
using GraphQL.Conventions.Builders;
using GraphQL.Conventions.Execution;
Expand Down Expand Up @@ -44,6 +45,8 @@ public class GraphQLEngine

List<System.Type> _middleware = new List<System.Type>();

IErrorTransformation _errorTransformation = new DefaultErrorTransformation();

bool _includeFieldDescriptions;

bool _includeFieldDeprecationReasons;
Expand Down Expand Up @@ -171,6 +174,12 @@ public GraphQLEngine WithMiddleware<T>()
return WithMiddleware(typeof(T));
}

public GraphQLEngine WithCustomErrorTransformation(IErrorTransformation errorTransformation)
{
_errorTransformation = errorTransformation;
return this;
}

public GraphQLEngine PrintFieldDescriptions(bool include = true)
{
_includeFieldDescriptions = include;
Expand Down Expand Up @@ -283,21 +292,9 @@ internal async Task<ExecutionResult> Execute(

var result = await _documentExecutor.ExecuteAsync(configuration).ConfigureAwait(false);

if (result.Errors != null)
if (result.Errors != null && _errorTransformation != null)
{
var errors = new ExecutionErrors();
foreach (var executionError in result.Errors)
{
var exception = new FieldResolutionException(executionError);
var error = new ExecutionError(exception.Message, exception);
foreach (var location in executionError.Locations ?? new ErrorLocation[0])
{
error.AddLocation(location.Line, location.Column);
}
error.Path = executionError.Path;
errors.Add(error);
}
result.Errors = errors;
result.Errors = _errorTransformation.Transform(result.Errors);
}

return result;
Expand Down
65 changes: 65 additions & 0 deletions test/Tests/Adapters/Engine/CustomErrorTransformationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Conventions;
using GraphQL.Conventions.Adapters.Engine.ErrorTransformations;
using GraphQL.Conventions.Execution;
using GraphQL.Conventions.Tests;
using GraphQL.Conventions.Tests.Templates;
using GraphQL.Conventions.Tests.Templates.Extensions;

namespace Tests.Adapters.Engine
{
public class CustomErrorTransformationTests : TestBase
{
[Test]
public async Task Will_Use_Default_Error_Transformation_When_Not_Provided()
{
var engine = GraphQLEngine.New<Query>();
var result = await engine
.NewExecutor()
.WithQueryString("query { queryData }")
.Execute();

result.Errors.ShouldNotBeNull();
result.Errors.Count.ShouldEqual(1);
var error = result.Errors.First();
error.ShouldBeOfType<ExecutionError>();
error.InnerException.ShouldBeOfType<FieldResolutionException>();
error.InnerException.InnerException.ShouldBeOfType<CustomException>();
}

[Test]
public async Task Will_Use_Custom_Error_Transformation_When_Provided()
{
var engine = GraphQLEngine.New<Query>();
var result = await engine
.WithCustomErrorTransformation(new CustomErrorTransformation())
.NewExecutor()
.WithQueryString("query { queryData }")
.Execute();

result.Errors.ShouldNotBeNull();
result.Errors.Count.ShouldEqual(1);
var error = result.Errors.First();
error.ShouldBeOfType<ExecutionError>();
error.InnerException.ShouldBeOfType<TargetInvocationException>();
error.InnerException.InnerException.ShouldBeOfType<CustomException>();
}

class Query
{
public string QueryData() => throw new CustomException();
}

class CustomErrorTransformation : IErrorTransformation
{
public ExecutionErrors Transform(ExecutionErrors errors)
=> errors;
}

class CustomException : Exception { }
}
}
5 changes: 5 additions & 0 deletions test/Tests/Templates/Extensions/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public static void ShouldContainWhenReformatted(this string str, string substrin
str.ShouldContain(substring);
}

public static void ShouldBeOfType<T>(this object actual)
{
Assert.IsInstanceOfType(actual, typeof(T));
}

public static void ShouldBeNamed(this GraphEntityInfo entity, string name)
{
entity.Name.ShouldEqual(name);
Expand Down
1 change: 0 additions & 1 deletion test/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.runner.reporters" Version="2.3.1" />
<PackageReference Include="Shouldly" Version="2.8.2" />
<PackageReference Include="NSubstitute" Version="2.0.0-*" />
<PackageReference Include="ReportGenerator" Version="2.5.9" />
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
Expand Down