-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Description
Edited by @derrickmehaffy
Hijacking this issue and setting it as a tracking issue for anything related to filtering on _null for relational fields or any field that doesn't work with the _null filter.
We are aware of this issue, and it should be addressed in our upcoming Strapi v4. We will not be back porting the fix to the v3 as we have almost entirely rewritten the database and query layers in the v4, which in doing so has already fixed this issue.
For more information about the v4 see: https://strapi.io/v4
Strapi GraphQL plugin provides automatically generated query types for every content type, and they contain where parameter. If there is a filter where: { field: $value }, and $value is set to null, the filter is ignored. There is also a filter where: { field_null: true }, which would be useful to use to check if certain relation is not set. But this filter doesn't work with relations, and when field_null: true, filter is ignored.
Steps to reproduce the behavior
- Create a content type Child
type ChildType { id: ID! name: String sex: String } - Create a content type Parent with a relation to many Child instances
type ParentType { id: ID! name: String children: [ChildType] } - Strapi will automatically create query types
parents( sort: String limit: Int start: Int where: JSON ): [ParentType] children( sort: String limit: Int start: Int where: JSON ): [ChildType] - Create Child1, Child2, Parent1 with relations to Child1 and Child2, Parent2 without any Child relations
- Try to query in GraphQL:
query1 { parentsWithSons: parents(where: { children: { sex: 'male' } }) { name children { name sex } } } query2 { childlessParents: parents(where: { children: null }) { name children { name sex } } } query3 { childlessParents: parents(where: { children_null: true }) { name children { name sex } } } ``` - Observe that first query works as expected, while query 2 and 3 return ALL parents
Expected behavior
First query should return all parents who has sons.
Second query should return all parents, as setting null to a filter should still skip the filter
Third query should return NO parents, as it explicitly set to return items with children === null.
System
- Node.js version: 12
- NPM version: any
- Strapi version: beta.18.8, beta.19
- Database: MongoDB
- Operating system: Alpine Linux in Docker