Example #1
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            await _next(context).ConfigureAwait(false);

            var pagingDetails = new PagingDetails
            {
                First  = context.ArgumentValue <int?>("first"),
                After  = context.ArgumentValue <string>("after"),
                Last   = context.ArgumentValue <int?>("last"),
                Before = context.ArgumentValue <string>("before"),
            };

            IQueryable <T> source = context.Result switch
            {
                IQueryable <T> q => q,
                IEnumerable <T> e => e.AsQueryable(),
                _ => null
            };

            if (source != null)
            {
                IConnectionResolver connectionResolver = _createConnectionResolver(
                    source, pagingDetails);

                context.Result = await connectionResolver
                                 .ResolveAsync(context.RequestAborted)
                                 .ConfigureAwait(false);
            }
        }
Example #2
0
            public async Task InvokeAsync(IMiddlewareContext context)
            {
                IFieldCollection <IInputField> arguments = context.Selection.Field.Arguments;

                foreach (IInputField argument in arguments)
                {
                    var  value      = context.ArgumentValue <object?>(argument.Name) !;
                    Type actualType = value.GetType();

                    if (argument.RuntimeType != actualType)
                    {
                        context.ReportError($"RuntimeType ({argument.RuntimeType}) not equal to actual type ({actualType})");
                    }

                    if (context.Selection.Field.Name.Value.StartsWith("array"))
                    {
                        if (!argument.RuntimeType.IsArray)
                        {
                            context.ReportError($"Field defined with array but ArgDeg saying it's a {argument.RuntimeType}");
                        }

                        if (!actualType.IsArray)
                        {
                            context.ReportError($"Field defined with array but actual type is a {actualType}");
                        }
                    }
                }

                await _next(context);
            }
Example #3
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            var queryArgument = context.ArgumentValue <object>("params");

            if (queryArgument != null)
            {
                var service = context.Service <IGraphQLValidationService>();
                var result  = await service.ValidateObjectAsync(queryArgument, context.RequestAborted);

                if (!result.IsValid)
                {
                    var eb = ErrorBuilder.New()
                             .SetMessage("There are some validations errors")
                             .SetCode("ValidationError")
                             .SetPath(context.Path)
                             .AddLocation(context.Selection.SyntaxNode);

                    foreach (var error in result.Errors)
                    {
                        eb.SetExtension(error.Field, error.Errors);
                    }

                    context.Result = eb.Build();

                    return;
                }
            }

            await _next.Invoke(context);
        }
Example #4
0
        public async Task Invoke(
            IMiddlewareContext context)
        {
            if (context.Selection.SyntaxNode.Arguments.Any())
            {
                var serviceProvider = context.Service <IServiceProvider>();
                var errors          = new List <(IInputField Argument, IList <ValidationFailure> Failures)>();

                foreach (IInputField argument in context.Field.Arguments)
                {
                    var validationOptions = new ArgumentValidationOptions <object>(
                        argument, context.ArgumentValue <object>(argument.Name));

                    if (!validationOptions.AllowedToValidate())
                    {
                        continue;
                    }

                    IValidator validator = validationOptions.GetValidator(serviceProvider);

                    if (validator == null)
                    {
                        continue;
                    }

                    ValidationResult result = await validator.ValidateAsync(
                        validationOptions.BuildValidationContext(), context.RequestAborted).ConfigureAwait(false);

                    if (result.Errors.Any())
                    {
                        errors.Add((argument, result.Errors));
                    }
                }

                if (errors.Any())
                {
                    var errorBuilder = ActivatorUtilities.CreateInstance(
                        serviceProvider, _errorBuilderType) as IValidationErrorBuilder;

                    foreach (var error in errors)
                    {
                        foreach (var failure in error.Failures)
                        {
                            context.ReportError(
                                errorBuilder.BuildError(
                                    ErrorBuilder.New(), failure, error.Argument, context)
                                .Build());
                        }
                    }

                    return;
                }
            }

            await _next(context).ConfigureAwait(false);
        }
Example #5
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            await _next(context).ConfigureAwait(false);

            var targetCrs = context.ArgumentValue <int?>(WellKnownFields.CrsFieldName);

            if (targetCrs.HasValue)
            {
                TransformInPlace(context.Result, targetCrs.Value);
            }
        }
Example #6
0
        public async Task Invoke(IMiddlewareContext context)
        {
            if (context.FieldSelection.Arguments.Count > 0)
            {
                foreach (var argument in context.FieldSelection.Arguments)
                {
                    var value = context.ArgumentValue <object>(argument.Name.Value);
                    Validate(value, context);
                }
            }

            await _next(context);
        }
Example #7
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            var arguments = context.Field.Arguments;

            if (arguments.Count is 0)
            {
                await _next(context);
            }

            var errors = new List <ValidationResult>();

            foreach (var argument in arguments)
            {
                if (argument == null)
                {
                    continue;
                }

                var input = context.ArgumentValue <object>(argument.Name);

                if (input == null)
                {
                    continue;
                }

                if (!ValidatorSettings.ValidateAllInputs &&
                    input.GetType().GetCustomAttribute(typeof(ValidatableAttribute)) == null)
                {
                    continue;
                }

                var validationContext = new ValidationContext(input);
                Validator.TryValidateObject(input, validationContext, errors, true);
            }

            if (errors.Any())
            {
                foreach (var error in errors)
                {
                    context.ReportError(ErrorBuilder.New()
                                        .SetMessage(error.ErrorMessage)
                                        .Build());
                }
            }
            else
            {
                await _next(context);
            }
        }
Example #8
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            var arguments = context.Field.Arguments;

            var validationResults = new List <ValidationResult>();

            foreach (var argument in arguments)
            {
                if (argument == null ||
                    !_options.ShouldValidate(context, argument))
                {
                    continue;
                }

                var resolvedValidators = _validatorProvider.GetValidators(context, argument);
                try
                {
                    var value = context.ArgumentValue <object>(argument.Name);
                    foreach (var resolvedValidator in resolvedValidators)
                    {
                        var validationContext = new ValidationContext <object>(value);
                        var validationResult  = await resolvedValidator.Validator.ValidateAsync(validationContext, context.RequestAborted);

                        if (validationResult != null)
                        {
                            validationResults.Add(validationResult);
                            _validationResultHandler.Handle(context, validationResult);
                        }
                    }
                }
                finally
                {
                    foreach (var resolvedValidator in resolvedValidators)
                    {
                        resolvedValidator.Scope?.Dispose();
                    }
                }
            }

            var invalidValidationResults = validationResults.Where(r => !r.IsValid);

            if (invalidValidationResults.Any())
            {
                OnInvalid(context, invalidValidationResults);
            }

            await _next(context);
        }
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            var queryArgument = context.ArgumentValue <object>("params");

            if (queryArgument == null)
            {
                context.Result = ErrorBuilder.New()
                                 .SetMessage("mutation argument is required")
                                 .SetCode("400")
                                 .SetPath(context.Path)
                                 .AddLocation(context.Selection.SyntaxNode)
                                 .Build();

                return;
            }

            await _next.Invoke(context);
        }
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            var arguments = context.Selection.Field.Arguments;

            var invalidResults = new List <ArgumentValidationResult>();

            foreach (var argument in arguments)
            {
                if (argument == null)
                {
                    continue;
                }

                var resolvedValidators = _validatorProvider
                                         .GetValidators(context, argument)
                                         .ToArray();
                if (resolvedValidators.Length > 0)
                {
                    try
                    {
                        var value = context.ArgumentValue <object?>(argument.Name);
                        if (value == null)
                        {
                            continue;
                        }

                        foreach (var resolvedValidator in resolvedValidators)
                        {
                            var validationContext = new ValidationContext <object?>(value);
                            var validationResult  = await resolvedValidator.Validator.ValidateAsync(
                                validationContext,
                                context.RequestAborted);

                            if (validationResult != null &&
                                !validationResult.IsValid)
                            {
                                invalidResults.Add(
                                    new ArgumentValidationResult(
                                        argument.Name,
                                        resolvedValidator.Validator,
                                        validationResult));
                            }
                        }
                    }
                    finally
                    {
                        foreach (var resolvedValidator in resolvedValidators)
                        {
                            resolvedValidator.Scope?.Dispose();
                        }
                    }
                }
            }

            if (invalidResults.Any())
            {
                _validationErrorsHandler.Handle(context, invalidResults);
                return;
            }

            await _next(context);
        }