使用 C# 进行热巧克力验证
Hotchocolate validation with C#
所以我最近一直在玩 Hotchocolate,我做了一个 class,它返回了一个学生列表,但我想要一些验证函数。我真的没有从热巧克力官方网站上找到任何对我有帮助的东西。
Student.cs
public class Student
{
[GraphQLNonNullType]
public string Name{ get; set; }
[GraphQLNonNullType]
public string LastName{ get; set; }
[GraphQLNonNullType]
public string Picture { get; set; }
}
这是我的查询,目前返回列表中的所有学生。
StudentQuery.cs
public class StudentQuery
{
[UseFiltering]
[UseSorting]
public List<Student> GetStudents()
{
return MongoDBHelper.LoadRecords<Student>(EMongoCollection.Students);
}
}
现在我的问题是,如何为学生制定 ValidationRule,例如说学生的名字必须至少有 3 个字符?
有人可以给我提供一些例子吗?
提前致谢。
HotChocolate 本身没有这种输入验证的集成。
该框架仅进行 GraphQL 验证。因此检查无效的 GraphQL 查询。 (例如错误类型)
如果您想使用验证,有几个社区库可供选择:
- AppAny.HotChocolate.FluentValidation - 输入字段 HotChocolate + FluentValidation 集成
- DataAnnotatedModelValidations - HotChocolate 的数据注释模型验证中间件
- FairyBread - HotChocolate 的输入验证 (FluentValidation)
- FluentChoco - HotChocolate
的 FluentValidation 中间件
- Graph.ArgumentValidator - 将输入参数验证添加到 HotChocolate (DataAnnotations)
此处列出了社区图书馆:
https://github.com/ChilliCream/hotchocolate/blob/main/COMMUNITY.md
除了第 3 方库..我们可以编写自定义中间件来处理从服务输出的数据的数据验证...
中间件示例代码
public class OutputValidationMiddleware
{
private readonly FieldDelegate next;
private readonly ILogger<OutputValidationMiddleware> logger;
public OutputValidationMiddleware(
FieldDelegate next,
ILogger<OutputValidationMiddleware> logger)
{
this.next = next;
this.logger = logger;
}
public async Task InvokeAsync(IMiddlewareContext context)
{
await next(context).ConfigureAwait(false);
if (context.Result != null && context.Result is IValidatable)
{
var validationErrors = (context.Result as IValidatable).Validate();
foreach (var err in validationErrors)
{
logger.LogWarning(err);
}
}
return;
}
}
现在您的模型需要实现如下所示的 IValidatable
public class Test : IValidatable
{
IEnumerable<string> Validate()
{
// Validate and return any validation errors here.
}
}
现在我们只需要注册就可以了
services.AddGraphQLServer().
UseField<OutputValidationMiddleware>()
所以我最近一直在玩 Hotchocolate,我做了一个 class,它返回了一个学生列表,但我想要一些验证函数。我真的没有从热巧克力官方网站上找到任何对我有帮助的东西。
Student.cs
public class Student
{
[GraphQLNonNullType]
public string Name{ get; set; }
[GraphQLNonNullType]
public string LastName{ get; set; }
[GraphQLNonNullType]
public string Picture { get; set; }
}
这是我的查询,目前返回列表中的所有学生。
StudentQuery.cs
public class StudentQuery
{
[UseFiltering]
[UseSorting]
public List<Student> GetStudents()
{
return MongoDBHelper.LoadRecords<Student>(EMongoCollection.Students);
}
}
现在我的问题是,如何为学生制定 ValidationRule,例如说学生的名字必须至少有 3 个字符? 有人可以给我提供一些例子吗?
提前致谢。
HotChocolate 本身没有这种输入验证的集成。 该框架仅进行 GraphQL 验证。因此检查无效的 GraphQL 查询。 (例如错误类型)
如果您想使用验证,有几个社区库可供选择:
- AppAny.HotChocolate.FluentValidation - 输入字段 HotChocolate + FluentValidation 集成
- DataAnnotatedModelValidations - HotChocolate 的数据注释模型验证中间件
- FairyBread - HotChocolate 的输入验证 (FluentValidation)
- FluentChoco - HotChocolate 的 FluentValidation 中间件
- Graph.ArgumentValidator - 将输入参数验证添加到 HotChocolate (DataAnnotations)
此处列出了社区图书馆: https://github.com/ChilliCream/hotchocolate/blob/main/COMMUNITY.md
除了第 3 方库..我们可以编写自定义中间件来处理从服务输出的数据的数据验证...
中间件示例代码
public class OutputValidationMiddleware
{
private readonly FieldDelegate next;
private readonly ILogger<OutputValidationMiddleware> logger;
public OutputValidationMiddleware(
FieldDelegate next,
ILogger<OutputValidationMiddleware> logger)
{
this.next = next;
this.logger = logger;
}
public async Task InvokeAsync(IMiddlewareContext context)
{
await next(context).ConfigureAwait(false);
if (context.Result != null && context.Result is IValidatable)
{
var validationErrors = (context.Result as IValidatable).Validate();
foreach (var err in validationErrors)
{
logger.LogWarning(err);
}
}
return;
}
}
现在您的模型需要实现如下所示的 IValidatable
public class Test : IValidatable
{
IEnumerable<string> Validate()
{
// Validate and return any validation errors here.
}
}
现在我们只需要注册就可以了
services.AddGraphQLServer().
UseField<OutputValidationMiddleware>()