GithubHelp home page GithubHelp logo

dalrankov / fluentchoco Goto Github PK

View Code? Open in Web Editor NEW
23.0 23.0 3.0 30 KB

FluentValidation middleware for HotChocolate GraphQL

License: MIT License

C# 100.00%
dotnet fluentvalidation graphql hotchocolate

fluentchoco's People

Contributors

dalrankov avatar sergeyshaykhullin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fluentchoco's Issues

FluentChoco won't work on latest HotChocolate v12

here's the error it throws


message: "Method not found: '!!0 HotChocolate.Resolvers.IResolverContext.ArgumentValue(HotChocolate.NameString)'."
stackTrace: "   at FluentChoco.FluentValidationMiddleware.Invoke(IMiddlewareContext context)\r\n   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)\r\n   at FluentChoco.FluentValidationMiddleware.Invoke(IMiddlewareContext context)\r\n   at lambda_method7916(Closure , IMiddlewareContext , FluentValidationMiddleware )\r\n   at HotChocolate.Resolvers.FieldClassMiddlewareFactory.<>c__DisplayClass5_0`1.<CreateDelegate>b__1(IMiddlewareContext context)\r\n   at HotChocolate.AspNetCore.Authorization.AuthorizeMiddleware.InvokeAsync(IDirectiveContext context)\r\n   at HotChocolate.Utilities.MiddlewareCompiler`1.ExpressionHelper.AwaitTaskHelper(Task task)\r\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\r\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)"

removing the UseFluentValidation() won't trigger that error but the fluent validation won't work anymore.

Error on latest version of HotChocolate

When I add .UseFluentValidation() on HotChocolate version 12.6.1, it throws the following error

message: "Method not found: 'HotChocolate.Types.IFieldCollection`1<HotChocolate.Types.IInputField> HotChocolate.Types.IOutputField.get_Arguments()'."
stackTrace: "   at FluentChoco.FluentValidationMiddleware.Invoke(IMiddlewareContext context)\r\n   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)\r\n   at FluentChoco.FluentValidationMiddleware.Invoke(IMiddlewareContext context)\r\n   at lambda_method4315(Closure , IMiddlewareContext , FluentValidationMiddleware )\r\n   at HotChocolate.Resolvers.FieldClassMiddlewareFactory.<>c__DisplayClass5_0`1.<CreateDelegate>b__1(IMiddlewareContext context)\r\n   at HotChocolate.AspNetCore.Authorization.AuthorizeMiddleware.InvokeAsync(IDirectiveContext context)\r\n   at HotChocolate.Utilities.MiddlewareCompiler`1.ExpressionHelper.AwaitTaskHelper(Task task)\r\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\r\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)"

Getting Unexpected Execution Error msg

{
  "errors": [
    {
      "message": "Unexpected Execution Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addUser"
      ],
      "extensions": {
        "message": "Method not found: 'System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.Object> HotChocolate.Types.IField.get_ContextData()'.",
        "stackTrace": "   at FluentChoco.ArgumentValidationOptions`1..ctor(IInputField argument, TArgument instance)\r\n   at FluentChoco.FluentValidationMiddleware.Invoke(IMiddlewareContext context)\r\n   at HotChocolate.Utilities.MiddlewareCompiler`1.ExpressionHelper.AwaitTaskHelper(Task task)\r\n   at HotChocolate.Execution.Processing.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\r\n   at HotChocolate.Execution.Processing.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)"
      }
    }
  ]
}
    public class AddUserInput
    {
        [Required, MinLength(4, ErrorMessage = "Username must be atleast 4 characters.")]
        public string UserName { get; set; }

        [Required, RegularExpression(@"^[^@\s]+@[^@\s]+\.[^@\s]+$", ErrorMessage = "Email Id format is invalid")]
        public string Email { get; set; }

        [Required, RegularExpression(@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$", ErrorMessage = "Password must be atleast 8 characters, at least one uppercase letter, one lowercase letter, one number and one special character.")]
        public string Password { get; set; }
    }
}

Startup.cs

services.RegisterFluentValidators<Startup>();

services.AddGraphQLServer()
                .UseFluentValidation<CustomErrorBuilder>()

CustomErrorBuilder

public class CustomErrorBuilder
    : IValidationErrorBuilder
    {
        public IErrorBuilder BuildError(
            IErrorBuilder builder,
            ValidationFailure failure,
            IInputField argument,
            IMiddlewareContext context)
        {
            return builder.SetMessage(failure.ErrorMessage);
        }
    }
    [ExtendObjectType(Name = "Mutation")]
    public class UserMutations : BaseGraphQL
    {
        public async Task<int> AddUser(AddUserInput input, [Service] MailService mailService, [Service] UserManager<AppUser> userManager)
        {
            //Code for adding user
        }
     }

Did I miss anything ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.