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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With