diff --git a/Directory.Packages.props b/Directory.Packages.props index dc1e67d26..5904ecbb2 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,132 +1,132 @@  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Graphql/Conventions/GraphqlConvention.cs b/src/Graphql/Conventions/GraphqlConvention.cs index 6ded84b46..a953d45f1 100644 --- a/src/Graphql/Conventions/GraphqlConvention.cs +++ b/src/Graphql/Conventions/GraphqlConvention.cs @@ -16,6 +16,13 @@ namespace Rocket.Surgery.LaunchPad.Graphql.Conventions { public class GraphqlConvention : IServiceConvention { + private readonly IFairyBreadOptions _options; + + public GraphqlConvention(IFairyBreadOptions? options = null) + { + _options = options ?? new DefaultFairyBreadOptions(); + } + public void Register(IConventionContext context, IConfiguration configuration, IServiceCollection services) { var types = context.AssemblyCandidateFinder.GetCandidateAssemblies("MediatR") @@ -25,8 +32,8 @@ public void Register(IConventionContext context, IConfiguration configuration, I .ToArray(); services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(new DefaultFairyBreadOptions()); + services.TryAddSingleton(); + services.TryAddSingleton(_options); services.TryAddEnumerable(ServiceDescriptor.Singleton(new AutoConfigureMediatRMutation(types))); var sb = services diff --git a/src/Graphql/CustomInputValidationMiddleware.cs b/src/Graphql/CustomInputValidationMiddleware.cs index de0fc02bb..9c2791e6d 100644 --- a/src/Graphql/CustomInputValidationMiddleware.cs +++ b/src/Graphql/CustomInputValidationMiddleware.cs @@ -16,26 +16,27 @@ class CustomInputValidationMiddleware private readonly FieldDelegate _next; private readonly IFairyBreadOptions _options; private readonly IValidatorProvider _validatorProvider; - private readonly IValidationResultHandler _validationResultHandler; + private readonly IValidationErrorsHandler _validationErrorsHandler; public CustomInputValidationMiddleware( FieldDelegate next, IFairyBreadOptions options, IValidatorProvider validatorProvider, - IValidationResultHandler validationResultHandler + IValidationErrorsHandler validationErrorsHandler ) { _next = next; _options = options; _validatorProvider = validatorProvider; - _validationResultHandler = validationResultHandler; + _validationErrorsHandler = validationErrorsHandler; } + public async Task InvokeAsync(IMiddlewareContext context) { var arguments = context.Field.Arguments; - var validationResults = new List(); + var invalidResults = new List(); foreach (var argument in arguments) { @@ -45,19 +46,27 @@ public async Task InvokeAsync(IMiddlewareContext context) continue; } - var resolvedValidators = _validatorProvider.GetValidators(context, argument).ToArray(); + var resolvedValidators = _validatorProvider.GetValidators(context, argument); try { - var value = context.ArgumentValue(argument.Name); + var value = context.ArgumentValue(argument.Name); + if (value == null) + { + continue; + } + foreach (var resolvedValidator in resolvedValidators) { - var validationContext = new ValidationContext(value); + var validationContext = new ValidationContext(value); validationContext.SetServiceProvider(context.Services); - var validationResult = await resolvedValidator.Validator.ValidateAsync(validationContext, context.RequestAborted); - if (validationResult != null) + var validationResult = await resolvedValidator.Validator.ValidateAsync( + validationContext, + context.RequestAborted + ); + if (validationResult != null && + !validationResult.IsValid) { - validationResults.Add(validationResult); - _validationResultHandler.Handle(context, validationResult); + invalidResults.Add(validationResult); } } } @@ -70,18 +79,15 @@ public async Task InvokeAsync(IMiddlewareContext context) } } - var invalidValidationResults = validationResults.Where(r => !r.IsValid); - if (invalidValidationResults.Any()) + if (invalidResults.Any()) { - OnInvalid(context, invalidValidationResults); + _validationErrorsHandler.Handle(context, invalidResults); + context.Result = null; + } + else + { + await _next(context); } - - await _next(context); - } - - protected virtual void OnInvalid(IMiddlewareContext context, IEnumerable invalidValidationResults) - { - throw new ValidationException(invalidValidationResults.SelectMany(vr => vr.Errors)); } } } \ No newline at end of file