Skip to content

Commit

Permalink
Merge pull request #33 from graphql-dotnet/request-handler-validate
Browse files Browse the repository at this point in the history
Validation and New Types (Guid, DateTimeOffset)
  • Loading branch information
tlil authored Mar 12, 2017
2 parents 20fa629 + 3c20fab commit 19eaa68
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/GraphQL.Conventions/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
[assembly: AssemblyProduct("GraphQL.Conventions")]
[assembly: AssemblyCopyright("Copyright 2016-2017 Tommy Lillehagen. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("1.1.8.0")]
[assembly: AssemblyFileVersion("1.1.8.0")]
[assembly: AssemblyInformationalVersion("1.1.8.0")]
[assembly: AssemblyVersion("1.1.9.0")]
[assembly: AssemblyFileVersion("1.1.9.0")]
[assembly: AssemblyInformationalVersion("1.1.9.0")]
[assembly: CLSCompliant(false)]

[assembly: InternalsVisibleTo("GraphQL.Conventions.Tests")]
2 changes: 2 additions & 0 deletions src/GraphQL.Conventions/Web/IRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface IRequestHandler
{
Task<Response> ProcessRequest(Request request, IUserContext userContext);

Response Validate(Request request);

string DescribeSchema(bool returnJson = false);
}
}
25 changes: 22 additions & 3 deletions src/GraphQL.Conventions/Web/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class RequestHandlerBuilder : IDependencyInjector

bool _useValidation = true;

bool _outputViolationsAsWarnings;

internal RequestHandlerBuilder()
{
_dependencyInjector = this;
Expand Down Expand Up @@ -93,9 +95,10 @@ public RequestHandlerBuilder TreatAsWarning<TException>()
return this;
}

public RequestHandlerBuilder WithoutValidation()
public RequestHandlerBuilder WithoutValidation(bool outputViolationsAsWarnings = false)
{
_useValidation = false;
_outputViolationsAsWarnings = outputViolationsAsWarnings;
return this;
}

Expand All @@ -106,7 +109,8 @@ public IRequestHandler Generate()
_schemaTypes,
_assemblyTypes,
_exceptionsTreatedAsWarnings,
_useValidation);
_useValidation,
_outputViolationsAsWarnings);
}

public object Resolve(TypeInfo typeInfo)
Expand All @@ -125,22 +129,36 @@ class RequestHandlerImpl : IRequestHandler

readonly bool _useValidation;

readonly bool _outputViolationsAsWarnings;

internal RequestHandlerImpl(
IDependencyInjector dependencyInjector,
IEnumerable<Type> schemaTypes,
IEnumerable<Type> assemblyTypes,
IEnumerable<Type> exceptionsTreatedAsWarning,
bool useValidation)
bool useValidation,
bool outputViolationsAsWarnings)
{
_dependencyInjector = dependencyInjector;
_engine.WithAttributesFromAssemblies(assemblyTypes);
_exceptionsTreatedAsWarnings.AddRange(exceptionsTreatedAsWarning);
_useValidation = useValidation;
_outputViolationsAsWarnings = outputViolationsAsWarnings;
_engine.BuildSchema(schemaTypes.ToArray());
}

public async Task<Response> ProcessRequest(Request request, IUserContext userContext)
{
var validationWarnings = new List<ExecutionError>();
if (!_useValidation && _outputViolationsAsWarnings)
{
var validationResult = Validate(request);
if (validationResult?.Errors?.Any() ?? false)
{
validationWarnings.AddRange(validationResult.Errors);
}
}

var result = await _engine
.NewExecutor()
.WithQueryString(request.QueryString)
Expand All @@ -154,6 +172,7 @@ public async Task<Response> ProcessRequest(Request request, IUserContext userCon

var response = new Response(request, result);
var errors = result?.Errors?.Where(e => !string.IsNullOrWhiteSpace(e?.Message));
response.Warnings.AddRange(validationWarnings);
foreach (var error in errors ?? new List<ExecutionError>())
{
if (_exceptionsTreatedAsWarnings.Contains(error.InnerException.GetType()))
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQL.Conventions/Web/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public Response(

public bool IsValid => ValidationResult != null ? ValidationResult.IsValid : !HasErrors;

public IList<ExecutionError> Errors { get; } = new List<ExecutionError>();
public List<ExecutionError> Errors { get; } = new List<ExecutionError>();

public IList<ExecutionError> Warnings { get; } = new List<ExecutionError>();
public List<ExecutionError> Warnings { get; } = new List<ExecutionError>();
}
}
2 changes: 1 addition & 1 deletion src/GraphQL.Conventions/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.8-*",
"version": "1.1.9-*",
"description": "GraphQL Conventions for .NET",
"authors": [
"Tommy Lillehagen"
Expand Down

0 comments on commit 19eaa68

Please sign in to comment.