laravel-json-api / laravel

JSON:API for Laravel applications
MIT License
522 stars 43 forks source link

Filtering inlcuded resources #286

Open LucindaX opened 3 weeks ago

LucindaX commented 3 weeks ago

I'm trying to filter the resources included on my request.

To give an example :

I have two models, a User and Posts.

/api/v1/users/123?include=posts&filter[posts][approved]=true

The above query would filter the users that have approved posts and return them, but would still return all posts on the relationship ( approved or otherwise ). Assuming the usage of a WhereHas or a Where filter on the UserSchema. This makes sense though, as I'm querying the users resource.

What I would actually like to do is :

A user has many posts, I want to query user "X", including their posts, but only approved posts.

From the documentation, filtering includes does not seem to be available. I thought about having a custom filter, but I am a bit lost on how that would work.

Something like filter[included][posts][approved]=true to distinguish that it's a filter applied to the included resource maybe. And an Included::make('posts') filter that would rely on the filters defined on the PostSchema

OR

The query can be left as it is filter[posts][approved]=true and the filter would be something along the lines of IncludedWhere which would apply the filter to the included resource if requested, if not, it would act as a normal Where filter ... this looks cleaner to me and leaves the specification on the schema rather than complicate the query params.

lindyhopchris commented 4 days ago

Hey. Filtering includes is not supported. It's unlikely to be supported any time soon, because it is hideously complicated. It's not a simple as adding a new filter class, because we would need to apply the filter as we're doing the eager loading - which is done separately.

We do need to refactor the eager loading at some point, so it might be possible to add support for it then. That's going to be a complex bit of work, so isn't likely to happen soon.