7

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?

2
  • If you implement a custom query rewriter it should be possible. Commented Mar 30, 2020 at 22:09
  • Is there any update on this one? I am running into similar scenario. It would be helpful if there is a way to tackle this. Commented May 26, 2021 at 13:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.