Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hotchocolate custom filter on parent based on child collection

I'm trying to implement similar to described here filtering model with hotchocolate code-first approach. I need to filter movies if at least one of their actors meets certain criteria. Model looks like this:

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
    }
}

There seems to be no ability to add custom filter to MoviesFileringTypeDef since it only allows using properties of Movie class, and collections are not accepted there. Is it possible to implement such filter with hotchocolate?

like image 346
Sly Avatar asked Nov 15 '22 14:11

Sly


1 Answers

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.

like image 98
Anonymous Pawan Avatar answered Dec 19 '22 10:12

Anonymous Pawan