Add `@whereDoesntHaveConditions` directive
What problem does this feature proposal attempt to solve? The use case would be fetching the available (reads: not yet connected) records in a belongsToMany relation, but instead of getting the records that has at least a relation that satisfy the provided conditions, the query would return the complementary of it, including the ones that have no active relation
Example: The Project model can have many People working on it, in order to relate people to a project I need to fetch the ones that are not already associated with the project, but since the database holds thousands of People records, I need to apply a search/filtering/pagination to them with the already existing directives while also filter out the people that have been already related the project. I thought about using the currently available features of the library by doing two separate queries, one for all the people using regular whereConditions and a query on the project to get the already connected ones, then at this point filter out the already selected people in the front end. This creates an issue since there may be a chance that all the fetched records are going to be filtered in the front end resulting in an empty list of available people.
I don't think this can be solved either with the whereHasConditions directive using a NEQ operator simply because a query made with that seems to return only the records that have at least a relation, hence the ones present in the privot table, so the records that don't have any relation with the model, and because of that are not present with a foreign key in the pivot table, get discarded.
Which possible solutions should be considered?
Taking inspiration from the current @whereHasConditions directive, there could be a @whereDoesntHaveConditions one that does the complementary operation:
people(doesntHaveProject: _ @whereDoesntHaveConditions(columns: ["id", "name"])): [Person!]! @all
The query
{
people(doesntHaveProject: { column: ID, operator: EQ, value: "5"}) {
name
}
}
should return all the people that are not related to the project with id = "5", including the ones that have no active relation with the projects table. Providing an empty object as parameter should return all the people that don't have any relation with Project
Possible downsides The only issue I see with this is that there might be the need of an explicit belongsToMany relation in both the models that are related.
Example: The project can have many people working on it and the same person can work on many projects. For a (strange) reason I might not be interested in the projects a person is working on so I might not need to define the project relation in the Person model, but in order to get the "available people that don't already work on a certain project" I might have to define the projects relation in the Person model anyway for it to work.
Thank you for your well-written proposal. Given we abstracted out the tricky bits of the complex where conditions into https://github.com/nuwave/lighthouse/blob/master/src/WhereConditions/WhereConditionsBaseDirective.php, adding another directive that is essentially a variant of @whereHasConditions should be doable. Accepting PRs.
@sn4ke325 if it still actual you can try #1782