Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hotchocolate validation with C#

So I've been playing around with Hotchocolate lately and I made a class which gives me back a list of students, but I want to have some validation functions for it. I didn't really find anything that helps me from the official hotchocolate website.

Student.cs

public class Student
{
    [GraphQLNonNullType]
    public string Name{ get; set; }
    [GraphQLNonNullType]
    public string LastName{ get; set; }
    [GraphQLNonNullType]
    public string Picture { get; set; }
}

This is my query, which currently gives me back all students from a list.

StudentQuery.cs

public class StudentQuery
{
    [UseFiltering]
    [UseSorting]
    public List<Student> GetStudents()
    {
        return MongoDBHelper.LoadRecords<Student>(EMongoCollection.Students);
    }

}

Now my question is, how can I make a ValidationRule for a student, saying for example that a student has to at least have 3 characters for his name? Could someone be kind enough to provide me some example?

Thanks in advance.

like image 981
Makus Avatar asked Oct 23 '20 07:10

Makus


People also ask

Is it possible to use hotchocolate for input validation?

Thanks in advance. HotChocolate itself does not have a integration for input validation of this kind. The framework only does GraphQL validation. So checks for invalid GraphQL queries. (e.g. Wrong Type)

How to create secure resolvers or methods in hotchocolate?

Just decorate the required resolvers or methods with [HotChocolate.AspNetCore.Authorization.Authorize] attribute. Recall Part1 , we created a resolver or method like 'Welcome' in Query type (QueryResolver.cs). So let's make this method secured, we can do it by applying the 'Authorize' attribute on top of the resolver. Resolvers/QueryResolver.cs:

Can fluentchoco be used with abstractvalidator?

Having evaluated the above list for targeting Azure Functions, I found that FluentChoco was very simple to configure and plays nicely with AbstractValidator<T> implementations. It also does not have a dependency on Asp.net (and so can be added to a class library).


1 Answers

HotChocolate itself does not have a integration for input validation of this kind. The framework only does GraphQL validation. So checks for invalid GraphQL queries. (e.g. Wrong Type)

If you want to use validation there are a few community libraries to choose from:

  • AppAny.HotChocolate.FluentValidation - Input field HotChocolate + FluentValidation integration
  • DataAnnotatedModelValidations - Data Annotated Model Validation Middleware for HotChocolate
  • FairyBread - Input validation for HotChocolate (FluentValidation)
  • FluentChoco - FluentValidation middleware for HotChocolate
  • Graph.ArgumentValidator - Adds input argument validation to HotChocolate (DataAnnotations)

Community libraries are listed here: https://github.com/ChilliCream/hotchocolate/blob/main/COMMUNITY.md

like image 139
Pascal Senn Avatar answered Nov 08 '22 02:11

Pascal Senn