How do I filter items with certain fields being null?


#1

I got a schema where my object has a image relation to the FILE api. If the image is null, how do I filter it out of my results?

...(filter:{image:{id_not: null}}) { ...

throws an error:
The inline argument "id_not" of kind "NullValue" is not supported. Use variables instead of inline arguments to overcome this limitation.

Query:

query{
  allSkills(filter:{image:{id_not: null}}) {
    id
    image {
      url
      id
    }
  }
}

Schema:

type Skill implements Node {
  createdAt: DateTime!
  id: ID! @isUnique
  image: File @relation(name: "SkillImage")
  name: String!
  updatedAt: DateTime!
}

#2

The inline argument “id_not” of kind “NullValue” is not supported. Use variables instead of inline arguments to overcome this limitation.

The error is thrown by Apollo Client because it does not yet support using null inline. With Apollo Client, you need to use a GraphQL variable if you want to set an input argument to null.

Also, you can set the whole image field to null inside the filter, like this:

# we use ID and not ID!, because we set it to null

query withImage($id: ID) {
  allSkills(
    filter: {
      image: {
        id_not: $id
      }
    }
  ) {
    id
    image {
      url
      id
    }
  }
}

With this variable:

id: null

On the other hand, if you want to get all skills without an image, you can use this query:

query withoutImage($id: ID) {
  allSkills(
    filter: {
      image: {
        id: $id
      }
    }
  ) {
    id
    image {
      url
      id
    }
  }
}

or, my preferred solution, this:

query withoutImage($filter: FileFilter) {
  allSkills(
    filter: {
      image: $filter
    }
  ) {
    id
    image {
      url
      id
    }
  }
}

Parent children relations - get only parent query
#3

image


#4

That’s right! I just updated the answer with the correct syntax.


#5


#6

Seems like I forgot to change ID! to ID


#7

In case this helps anyone else, here is my scenario.

I wanted to filter on allLogs with a specific Report tied to them OR those with no report.

The below code works fine from the playground and using Apollo

query($reportId: ID, $nullVariable: ReportFilter){
    allLogs(
      filter: {
        OR:[
          {
          	report: {
              id: $reportId
            }
          },
          {
          	report: $nullVariable
          },
        ]
      }
    ){
      id
      logDate
      report {
    	id
      }
    }
  }

nullVarialbe = null;

#8

Sorry for replying on an old topic. But i’m in the exact situation but the provided answer doensn’t work (anymore?)

query all_persons($id: ID) {
  persons(where: { address: { id: $id } }) {
    id
    firstname
    lastname
    address {
      id
      street
    }
  }
}

This is my query.
But I get the exact same results with:

id: $id
id_not: $id

I want the persons that doesn’t have a Node connected to an address.
Both results are the records that are connected with an address.