Skip to content

Tracking Issue: Allow filter on null for relations #5414

@uguptu

Description

@uguptu

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

  1. Create a content type Child
    type ChildType {
        id: ID!
        name: String 
        sex: String
    }
    
  2. Create a content type Parent with a relation to many Child instances
    type ParentType {
        id: ID!
        name: String 
        children: [ChildType]
    }
    
  3. 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]
    
  4. Create Child1, Child2, Parent1 with relations to Child1 and Child2, Parent2 without any Child relations
  5. 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
            }
        }
    }    ```
    
  6. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: bugIssue reporting a bugseverity: mediumIf it breaks the basic use of the product but can be worked aroundsource: core:databaseSource is core/database packagestatus: confirmedConfirmed by a Strapi Team member or multiple community members

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions