【问题标题】:hotchocolate custom filter on parent based on child collection基于子集合的父级热巧克力自定义过滤器
【发布时间】:2020-07-08 22:44:38
【问题描述】:

我正在尝试使用hotchocolate 代码优先方法实现类似于described here 过滤模型。如果至少有一个演员符合某些标准,我需要过滤电影。模型如下所示:

public class Movie
{
    public IList<Actor> Actors { get; set; }
}

public class Actor { }

public class MovieTypeDef : ObjectType<Movie>
{
    protected override void Configure(IObjectTypeDescriptor<Movie> descriptor)
    {
        descriptor.Field(x => x.Actors)
            .Type<NonNullType<ListType<NonNullType<ActorTypeDef>>>>();
    }
}

public class ActorTypeDef : ObjectType<Actor> { }

public class Query
{
    public IList<Movie> Movies()
    {
        return new List<Movie>();
    }
}

public class QueryTypeDef : ObjectType<Query>
{
    protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
    {
        descriptor.Field(x => x.Movies())
            .Type<NonNullType<ListType<MovieTypeDef>>>()
            .UseFiltering<MoviesFileringTypeDef>();
    }
}

public class MoviesFileringTypeDef : FilterInputType<Movie>
{
    protected override void Configure(IFilterInputTypeDescriptor<Movie> descriptor)
    {
        descriptor.Filter(x => x.Actors) // compilation error
    }
}

似乎无法向MoviesFileringTypeDef 添加自定义过滤器,因为它只允许使用Movie 类的属性,并且那里不接受集合。 是否可以用热巧克力实现这样的过滤器?

【问题讨论】:

  • 如果你实现了一个自定义查询重写器,它应该是可能的。
  • 这个有更新吗?我遇到了类似的情况。如果有办法解决这个问题会很有帮助。

标签: c# graphql hotchocolate


【解决方案1】:
I also faced a similar kind of issue while I was trying to apply a filter on the parent entity based on the child entity field. I believe in your case you need to apply a filter in your MovieTypeDef also with QueryType as below:-

    public class MovieTypeDef : ObjectType<Movie>
    {
        protected override void Configure(IObjectTypeDescriptor<Movie> descriptor)
        {
            descriptor.Field(x => x.Actors)
                .Type<NonNullType<ListType<NonNullType<ActorTypeDef>>>>()
                .UseFiltering<YourModel>();;
        }
    }

After this change, you can apply a filter on a nested level also as you can apply with the parent level.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2022-10-25
    • 2019-07-31
    • 2022-11-05
    • 2020-12-22
    • 2021-11-24
    • 1970-01-01
    • 2020-11-13
    相关资源
    最近更新 更多