Filtering with null / optional variable via apollo client

Hi there!

Report a Dgraph Bug

What version of Dgraph are you using?

Dgraph version : v20.11.0-gfbc4c2899

Have you tried reproducing the issue with the latest release?

yes

What is the hardware spec (RAM, OS)?

arch linux, 32GB Ram

Steps to reproduce the issue (command/config used to run Dgraph).

I want to use the apollo client in react with dgraph graphql. I am defining the following graphql operation:

query Users($postId: String) {
queryUsers{
id
name
posts(filter: { id: { eq: $postId } }) {
id
message
}

}
}

in the apollo client i set postId to null, I get:

Message: Dgraph query failed because Dgraph execution failed because : eq expects atleast 1 argument

However, if i use altair and do the following:

queryUsers{
id
name
posts(filter: { id: { eq: null } }) {
id
message
}

}

it works.

Thanks

marc

What do you expect by providing null? Just need a way to return no data? What do you mean by


I believe that if you used ID for the type of id then the correct filter would be filter: { id: [$postId] } with $postId being of type ID!.

If you want to be able to provide a null variable, you could extract the [...] to the variable layer and then it would accept null: filter: { id: $postIds } with variable type $postIds: [ID!]

ID is just used as a string here.

By providing null I expect that no filter is applied and all results are shown

I cannot reproduce your error on Slash GraphQL (v20.07.1-rc1-29-g43c04cff). Your particular error may be specific to 20.11 which I don’t have up and running.

Does your setup differ from this?

Schema:

type Users {
    id: String! @id
    name: String
    posts: [Posts]
}

type Posts {
    id: String! @id
    message: String
}

Add data mutation:

mutation addData {
  addUsers(input: [{
    id: "1"
    name: "First"
    posts: [{
      id: "A"
      message: "Apple"
    }{
      id: "B"
      message: "Ball"
    }]
  }{
    id: "2"
    name: "Second"
    posts: [{
      id: "C"
      message: "Cat"
    }]
  }]) {
    numUids
  }
}

Try to duplicate your error message:

query testError($postId: String) {
  queryUsers {
    id
    name
    posts(filter: { id: { eq: $postId } }) {
      id
      message
    }
  }
}

Which gives me:

{
  "data": {
    "queryUsers": [
      {
        "id": "1",
        "name": "First",
        "posts": []
      },
      {
        "id": "2",
        "name": "Second",
        "posts": []
      }
    ]
  },
  "extensions": { ... }
}

When $postId is null the posts edge will return an empty array. If you want

then that is different. The filter Posts.id === null is not the same as “no filter”.

To get a “no filter is applied”, you need to extrapolate the $postId into the parent type of { eq: String } which in my particular case with my schema directives is StringHashFilter Then when providing a null variable it will return all.

query UsersWithPostFilterVariable ($postIdFilter: StringHashFilter) {
  queryUsers {
    id
    name
    posts(filter: { id: $postIdFilter })
  }
}

I wouldn’t suspect any of this to be any different for version 20.11 but there could very well be a bug in there.