HotChocolate GraphQL:EF实体扩展类型无法应用过滤问题
- 场景:使用
ObjectTypeExtension扩展EF实体的导航属性并调用UseFiltering()时触发类型加载错误;直接在EF生成类的导航属性上添加[UseFiltering()]特性则可正常运行。 - 核心诉求:必须通过
ObjectTypeExtension增强实体功能,避免EF实体与HotChocolate代码耦合。
报错代码行
descriptor.Field(t => t.Brands).UseFiltering();
报错信息
Could not load type 'HotChocolate.Types.IInputType' from assembly 'HotChocolate.Types, Version=13.8.1.0, Culture=neutral, PublicKeyToken=null'. (Product.QL.Types.LobTeamType)
at HotChocolate.Configuration.TypeInitializer.DiscoverTypes()
at HotChocolate.Configuration.TypeInitializer.Initialize()
at HotChocolate.SchemaBuilder.Setup.InitializeTypes(SchemaBuilder builder, IDescriptorContext context, IReadOnlyList`1 types)
at HotChocolate.SchemaBuilder.Setup.Create(SchemaBuilder builder, LazySchema lazySchema, IDescriptorContext context)
at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaAsync(ConfigurationContext context, RequestExecutorSetup setup, RequestExecutorOptions executorOptions, IServiceProvider schemaServices, TypeModuleChangeMonitor typeModuleChangeMonitor, CancellationToken cancellationToken)
at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaServicesAsync(ConfigurationContext context, RequestExecutorSetup setup, CancellationToken cancellationToken)
at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorNoLockAsync(String schemaName, CancellationToken cancellationToken)
at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorAsync(String schemaName, CancellationToken cancellationToken)
at HotChocolate.Execution.RequestExecutorProxy.GetRequestExecutorAsync(CancellationToken cancellationToken)
at HotChocolate.AspNetCore.HttpPostMiddlewareBase.HandleRequestAsync(HttpContext context)
at HotChocolate.AspNetCore.HttpPostMiddlewareBase.InvokeAsync(HttpContext context)
at Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.<>c__DisplayClass19_0.<<UseCancellation>b__1>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.InvokeCore(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
相关代码示例
EF实体类
public partial class LobTeam { public int LobTeamId { get; set; } public Guid LobTeamUid { get; set; } public string Name { get; set; } = null!; public int StatusId { get; set; } public virtual ICollection<LobTeamBrand> LobTeamBrands { get; set; } = new List<LobTeamBrand>(); public virtual ICollection<Platform> Platforms { get; set; } = new List<Platform>(); public virtual ICollection<Brand> Brands { get; set; } }
ObjectTypeExtension扩展类
public sealed class LobTeamType : ObjectTypeExtension<LobTeam> { protected override void Configure(IObjectTypeDescriptor<LobTeam> descriptor) { descriptor .Field(t => t.LobTeamBrands) .Ignore(); descriptor .Field(t => t.Brands) .UseFiltering(); } }
Program.cs配置
builder.Services .AddGraphQLServer() .AddQueryType<ProductQuery>() .RegisterDbContext<ProductDataContext>(DbContextKind.Resolver) .AddProjections() .AddFiltering() .AddSorting() .AddTypeExtension<LobTeamType>();
解决方案
统一HotChocolate包版本
错误根源大概率是项目中HotChocolate相关包版本不匹配。检查HotChocolate.AspNetCore、HotChocolate.Data、HotChocolate.Types等所有依赖包,确保版本统一为13.8.1.0或升级到最新稳定版。显式指定过滤输入类型
若版本一致仍报错,尝试在调用UseFiltering时显式声明过滤类型,帮助框架正确解析:descriptor .Field(t => t.Brands) .UseFiltering<BrandFilterInputType>();BrandFilterInputType可通过代码生成工具生成,或手动定义对应Brand实体的过滤输入类型。注册导航属性对应实体的扩展类型
确认Brand实体的ObjectType或ObjectTypeExtension已添加到GraphQL配置中,否则框架无法识别导航属性类型,无法生成过滤输入:builder.Services .AddGraphQLServer() // 其他配置项 .AddTypeExtension<BrandType>();清理缓存并重建项目
删除项目的bin、obj目录,清理NuGet缓存后重新构建,避免旧程序集缓存导致的类型加载异常。
内容的提问来源于stack exchange,提问作者punyiambic

