.NET6下Hot Chocolate中uint类型成员‘类型未知’问题求助
解决Hot Chocolate中uint类型"类型未知"错误的方案
针对你在.NET6 + Hot Chocolate 13.0.5中使用uint类型遇到的「声明类型YYY的成员XXX类型未知」错误,可按以下步骤排查修复:
1. 正确绑定运行时类型与标量类型
你尝试了BindRuntimeType但可能位置不对,需要在GraphQL服务配置链中明确绑定,确保Hot Chocolate能将CLR的uint类型映射到UnsignedIntType:
builder.Services.AddGraphQLServer() .AddType<UnsignedIntType>() // 关键:绑定uint到对应的标量类型 .BindRuntimeType<uint, UnsignedIntType>() .AddQueryType<Query>() .AddProjections() .AddFiltering() .AddSorting();
2. 为过滤、排序、投影特性添加uint支持
因为你启用了AddFiltering、AddSorting、AddProjections,这些特性需要知道如何处理uint类型,需添加对应的扩展配置:
过滤配置
创建自定义过滤器提供者,注册uint的过滤操作:
public class UIntFilterInputType : FilterInputType<uint> { protected override void Configure(IFilterInputTypeDescriptor<uint> descriptor) { descriptor.Name("UIntFilter"); descriptor.BindFieldsExplicitly(); descriptor.Field(x => x.Equals).Type<UnsignedIntType>(); descriptor.Field(x => x.In).Type<ListType<UnsignedIntType>>(); descriptor.Field(x => x.NotIn).Type<ListType<UnsignedIntType>>(); descriptor.Field(x => x.GreaterThan).Type<UnsignedIntType>(); descriptor.Field(x => x.GreaterThanOrEquals).Type<UnsignedIntType>(); descriptor.Field(x => x.LessThan).Type<UnsignedIntType>(); descriptor.Field(x => x.LessThanOrEquals).Type<UnsignedIntType>(); } }
然后在服务配置中注册:
builder.Services.AddGraphQLServer() .AddType<UnsignedIntType>() .BindRuntimeType<uint, UnsignedIntType>() .AddQueryType<Query>() .AddProjections() // 添加自定义uint过滤器 .AddFiltering(x => x.AddFilterInputType<UIntFilterInputType>()) .AddSorting();
排序配置
为uint注册排序比较器:
builder.Services.AddGraphQLServer() .AddType<UnsignedIntType>() .BindRuntimeType<uint, UnsignedIntType>() .AddQueryType<Query>() .AddProjections() .AddFiltering(x => x.AddFilterInputType<UIntFilterInputType>()) // 注册uint的排序支持 .AddSorting(x => x.AddDefaultSortOperations<uint>())
3. 确认返回类型的属性配置
你的Query返回的是TenantResponseModel,需确保该类中Dog属性同样标记了[GraphQLType(typeof(UnsignedIntType))]且类型为uint,和你给出的FooModel保持一致:
public class TenantResponseModel { public string ID { get; set; } [GraphQLType(typeof(UnsignedIntType))] public uint Dog { get; set; } public Guid UID { get; set; } public string Name { get; set; } }
4. 验证包依赖完整性
确保你已安装正确的标量包:
Install-Package HotChocolate.Types.Scalars -Version 13.0.5
关键说明
Hot Chocolate默认不识别CLR的uint类型,必须通过BindRuntimeType建立映射;同时过滤、排序等特性需要单独配置类型支持,否则在处理相关操作时会因无法识别类型抛出错误。
内容的提问来源于stack exchange,提问作者steve lomicka
相关产品推荐
相关产品推荐

