# Filtering with null / optional variable via apollo client

**URL:** <https://discuss.dgraph.io/t/filtering-with-null-optional-variable-via-apollo-client/11320>\
**Category:** App Development\
**Tags:** area:querylang:vars, apollo-client\
**Created:** [November 5, 2020, 8:34pm UTC](https://discuss.dgraph.io/t/filtering-with-null-optional-variable-via-apollo-client/11320 "2020-11-05T20:34:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcown](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/marcown/32/6069_2.png) [@marcown](https://discuss.dgraph.io/u/marcown)\
**Post date:** [November 5, 2020, 8:34pm UTC](https://discuss.dgraph.io/t/filtering-with-null-optional-variable-via-apollo-client/11320/1 "2020-11-05T20:34:33Z")

</div>

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:

```auto
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:

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

}

```

it works.

Thanks

marc

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [November 6, 2020, 2:34am UTC](https://discuss.dgraph.io/t/filtering-with-null-optional-variable-via-apollo-client/11320/2 "2020-11-06T02:34:18Z")

</div>

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

> [@marcown](#):
>
> it works.

* * *

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!]`

---

<div class="post-metadata">

**Author:** ![marcown](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/marcown/32/6069_2.png) [@marcown](https://discuss.dgraph.io/u/marcown)\
**Post date:** [November 6, 2020, 7:20am UTC](https://discuss.dgraph.io/t/filtering-with-null-optional-variable-via-apollo-client/11320/3 "2020-11-06T07:20:31Z")

</div>

ID is just used as a string here.

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

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [November 6, 2020, 8:08am UTC](https://discuss.dgraph.io/t/filtering-with-null-optional-variable-via-apollo-client/11320/4 "2020-11-06T08:08:01Z")

</div>

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:

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

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

```

Add data mutation:

```auto
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:

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

```

Which gives me:

```auto
{
  "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

> [@marcown](#):
>
> By providing null I expect that no filter is applied and all results are shown

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.

```auto
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.
