我使用HotChocolate (11.2.2)与EF,并希望过滤一个子属性。根据GraphQL文档,这应该可以通过在导航属性上使用filter关键字来实现,但是HotChocolate失败了。
我的模式:
type A {
Name: string,
RefTo: [B]
}
type B {
TypeName: string,
Value: int
}这是由EF支持的,我为HotChocolate提供了一个HotChocolate。
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<A> GetAs([Service] Context db) => db.As.AsSingleQuery().AsNoTrackingWithIdentityResolution();现在,我只想包括那些Bs,其中TypeName等于"ExampleType",如下所示:
query {
As {
Name,
RefTo(where: { TypeName: { eq: "ExampleType" } })
{
TypeName,
Value
}
}
}但HotChcolate似乎不明白这一点,并说:
未知参数"where“on field "A.RefTo".validation
可以用EF核心模型过滤导航属性吗?
发布于 2021-04-23 15:44:24
您也必须向RefTo添加过滤。
[UseFiltering]
public ICollection<A> RefTo {get; set;}发布于 2022-03-31 03:32:50
如果要将UseFiltering、UseSorting等应用于具有结果类型ICollection的所有字段,则可以使用TypeInterceptor
using System;
using System.Collections.Generic;
using HotChocolate.Configuration;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
namespace MyApp
{
public class FilterCollectionTypeInterceptor : TypeInterceptor
{
private static bool IsCollectionType(Type t)
=> t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>);
public override void OnBeforeRegisterDependencies(ITypeDiscoveryContext discoveryContext, DefinitionBase? definition,
IDictionary<string, object?> contextData)
{
if (definition is not ObjectTypeDefinition objectTypeDefinition) return;
for (var i = 0; i < objectTypeDefinition.Fields.Count; i++)
{
var field = objectTypeDefinition.Fields[i];
if (field.ResultType is null || !IsCollectionType(field.ResultType)) continue;
var descriptor = field.ToDescriptor(discoveryContext.DescriptorContext)
.UseFiltering()
.UseSorting();
objectTypeDefinition.Fields[i] = descriptor.ToDefinition();
}
}
}
}https://stackoverflow.com/questions/67232513
复制相似问题