ASP.NET Core HotChocolate.FluentValidation校验时IInputObjectTypeDescriptor无Argument方法求助
问题根因
Argument() 是 IObjectFieldDescriptor(Mutation/Query等对象类型的字段描述器)的专属方法,仅用于给Mutation/Query的字段定义入参时调用。你当前操作的是IInputObjectTypeDescriptor(输入对象类型的描述器),本身不存在该方法,属于调用场景错误。
修复步骤
1. 调整服务注册顺序
将FluentValidation官方的服务注册移到GraphQL配置之前,避免校验器未注册完成导致的校验失效:
public void ConfigureServices(IServiceCollection services) { services.ConfigureDbContext(Configuration); services.InjectContainerServices(Configuration); // 先注册FluentValidation services.AddFluentValidation(options => { options.DisableDataAnnotationsValidation = false; options.ImplicitlyValidateChildProperties = true; // 官方自带扫描注册校验器能力,不需要自己写DI扩展,可删除你自定义的AddInjectableFluentValidationDependencies相关代码 options.RegisterValidatorsFromAssembly(Assembly.GetExecutingAssembly()); }); // 后注册GraphQL服务 services.ConfigureGraphQL(); }
2. 删除无效的Argument调用
输入对象类型不需要通过Argument()方法启用校验,二选一以下两种配置方式即可:
- 方式1:保留你当前
AddPlatformInput记录中Name字段的[UseFluentValidation]属性,不需要修改AddPlatformInputType的配置 - 方式2:移除属性标记,在输入类型的字段配置中直接调用
UseFluentValidation():
public class AddPlatformInputType : InputObjectType<AddPlatformInput> { protected override void Configure(IInputObjectTypeDescriptor<AddPlatformInput> descriptor) { descriptor.Description("Represents the input to add for a platform."); descriptor.Field(x => x.Name) .Type<StringType>() .UseFluentValidation() .Description("Represents the name for the platform."); base.Configure(descriptor); } }
3. 清理多余配置
如果你的FluentvalidationInterceptor没有自定义校验逻辑,可直接删除该行注册代码,HotChocolate的AddFluentValidation()已内置默认拦截器,重复注册容易引发冲突。
内容的提问来源于stack exchange,提问作者Wasyster
相关产品推荐
相关产品推荐

