# GraphQl hasFilter usage

**URL:** https://discuss.dgraph.io/t/graphql-hasfilter-usage/12139
**Category:** GraphQL
**Tags:** kind:question
**Created:** [January 2, 2021, 2:27pm UTC](https://discuss.dgraph.io/t/graphql-hasfilter-usage/12139 "2021-01-02T14:27:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![noobloser](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/noobloser/32/5752_2.png) [@noobloser](https://discuss.dgraph.io/u/noobloser)
#### Post date: [January 2, 2021, 2:27pm UTC](https://discuss.dgraph.io/t/graphql-hasfilter-usage/12139/1 "2021-01-02T14:27:55Z")

</div>

Hi dgraph Team,  
I noticed in my schema the hasFilter from the [#11944](http://discuss.hypermode.com/t/documentation-update-v20-11-0-release/11944) v20.11.0 update.  
I’d like to know how the usage of multiple hasFilters works, as the example just point out a single value.

This is the [example](https://dgraph.io/docs/master/graphql/queries/search-filtering/#filter-for-objects-with-specified-non-null-fields-using-has):

```auto
queryStudent(filter: { has : name } ){
   tid
   age
   name
}

```

I personally would expect the hasFilter to work with a List like [Filter] in and/or, or a regular Filter in not, where one could set the hasFilter by boolean, so I could also easily filter that a field should be null.

Example 1 - List

```auto
queryStudent(filter: { has : [name, age] } ){
   tid
   age
   name
}

```

Example 2 - filter

```auto
queryStudent(filter: { has : {name: true, age: false} } ){
   tid
   age
   name
}

```

If the implementation is by design, nvm my question and I just missed an explanation in the docs 😊

---

<div class="post-metadata">

### Author: ![verneleem](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/verneleem/32/5801_2.png) [@verneleem](https://discuss.dgraph.io/u/verneleem)
#### Post date: [January 2, 2021, 5:30pm UTC](https://discuss.dgraph.io/t/graphql-hasfilter-usage/12139/2 "2021-01-02T17:30:07Z")

</div>

You can combine multiple has statements inside and/or/not logic to accomplish your same queries.

`and: [{ has: name },{ has: age }]`

`has: name, not: { has: age }`

---

<div class="post-metadata">

### Author: ![noobloser](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/noobloser/32/5752_2.png) [@noobloser](https://discuss.dgraph.io/u/noobloser)
#### Post date: [January 2, 2021, 5:53pm UTC](https://discuss.dgraph.io/t/graphql-hasfilter-usage/12139/3 "2021-01-02T17:53:50Z")

</div>

@verneleem that is indeed a solution for the problem, but makes it by design more complicated for generic filter generations.  
Thus my question if it wouldn’t be better to have has as a list.

---

<div class="post-metadata">

### Author: ![verneleem](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/verneleem/32/5801_2.png) [@verneleem](https://discuss.dgraph.io/u/verneleem)
#### Post date: [January 2, 2021, 6:40pm UTC](https://discuss.dgraph.io/t/graphql-hasfilter-usage/12139/4 "2021-01-02T18:40:59Z")

</div>

I believe the goal was to map more of the DQL functionality to the GraphQL generated schema. The has filter was first available in DQL ([has function docs](https://dgraph.io/docs/query-language/functions/#has)). So when this filter was brought into GraphQL, it was introduced with very similar syntax, e.g. every has function only accepts a single parameter and multiple has functions could be [connected with AND, OR, and NOT](https://dgraph.io/docs/query-language/connecting-filters/) logic. This provides a simpler GraphQL to DQL rewriting script.

You could somewhat easily create a script to rewrite your two examples into acceptable syntax:

```javascript
const hasArrayFormatter = (fields) => {
  if (!Array.isArray(fields) || fields.length < 1) return {}
  fields = fields.map(field => {
    return { has: field }
  })
  return {
    and: fields
  }
}
const hasArray = ["name","age"]
const formattedHas = hasArrayFormatter(hasArray)

```

response: `{ and: [{ has: "name" }, { has: "age" }] }`

```javascript
const hasPropsFormatter = (fields) => {
  if (typeof fields !== 'object' || fields === null || Object.keys(fields).length === 0) return {}
  const filters = []
  for(const [field, required] of Object.entries(fields)) {
    if (required) {
      filters.push({ has: field })
    } else {
      filters.push({ not: { has: field } })
    }
  }
  return { and: filters }
}
const hasProps = { name: true, age: false }
const formattedHas = hasPropsFormatter(hasProps)

```

response: `{ and: [{ has: "name" }, { not: { has: "age" } }] }`

You could then plug these straight into your query:

```javascript
const query = gql`
  query Students($filter: StudentFilter) {
    queryStudent(filter: $filter) {
      tid
      age
      name
    }
  }
`
// ...
const {data} = useQuery(query, { variables: { filter: formattedHas } })
// ...

```

---

<div class="post-metadata">

### Author: ![noobloser](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/noobloser/32/5752_2.png) [@noobloser](https://discuss.dgraph.io/u/noobloser)
#### Post date: [January 3, 2021, 9:40pm UTC](https://discuss.dgraph.io/t/graphql-hasfilter-usage/12139/5 "2021-01-03T21:40:16Z")

</div>

@verneleem thank you for your really elaborate answer, it was way more than I expected 😃.

I now recon my error, as I viewed the and/or filter as junctions for separate entities, but of couse, one can look at it more like SQL AND expression, too.
