# Query and queryWithVars

**URL:** https://discuss.dgraph.io/t/query-and-querywithvars/2796
**Category:** Dgraph
**Created:** [June 26, 2018, 6:57am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796 "2018-06-26T06:57:22Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![shanghai-Jerry](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shanghai-jerry/32/863_2.png) [@shanghai-Jerry](https://discuss.dgraph.io/u/shanghai-Jerry)
#### Post date: [June 26, 2018, 6:57am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/1 "2018-06-26T06:57:22Z")

</div>

recently， i found there are some differences between query and queryWithVars.

i use queryWithVars:

```auto
query withvar($a:string, $b:string){
 query(func:uid($a)) {
  uid
  name
  one:~candidate_company @facets(eq(dept_name, "$b")) {
    count(uid)
  }
 }
}

```

and init vars like  
vars.put("$a", uid);  
vars.put("$b", deptName);

Not result in ~candidate\_company @facets(eq(dept\_name, “$b”)) {  
count(uid)  
}  
return

result

```auto
{"query":[{"uid":"0x1b948c","name":"95202659"}]}

```

but when i use query:

```auto
{
 query(func:uid(%s)) {
  uid
  name
  one:~candidate_company @facets(eq(dept_name, "%s")) {
    count(uid)
  }
 }
}

```

and format query like  
query = String.format(query, uid, deptName);

the result return

```auto
{"query":[{"uid":"0x1b948c","name":"95202659","one":[{"count":1}]}]}

```

any clues?  
@mrjn

---

<div class="post-metadata">

### Author: ![BlankRain](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/blankrain/32/1592_2.png) [@BlankRain](https://discuss.dgraph.io/u/BlankRain)
#### Post date: [June 26, 2018, 9:02am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/2 "2018-06-26T09:02:13Z")

</div>

haha

> [@shanghai-Jerry](#):
>
> @facets(eq(dept\_name, “$b”)

how about try `@facets(eq(dept_name, $b)` ?

---

<div class="post-metadata">

### Author: ![shanghai-Jerry](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shanghai-jerry/32/863_2.png) [@shanghai-Jerry](https://discuss.dgraph.io/u/shanghai-Jerry)
#### Post date: [June 26, 2018, 10:00am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/3 "2018-06-26T10:00:05Z")

</div>

i also think this might be the problem. but i haven’t try it. i will check this out later

after try:

```auto
{
  set {
    _:jerry <name> "jerry" .
    _:company <name> "百度" . 
    _:jerry <candidate_company> _:company (dept_name="上海研发中心") .
   }
}

```

query:

```auto
{
 query(func:uid(0x02)) {
  uid
  name
  one:~candidate_company @facets(eq(dept_name, "上海研发中心")) {
    count(uid)
  }
 }
}

```

result:

```auto
{
  "data": {
    "query": [
      {
        "uid": "0x2",
        "name": "百度",
        "one": [
          {
            "count": 1
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 24811,
      "processing_ns": 146292,
      "encoding_ns": 961954
    },
    "txn": {
      "start_ts": 22,
      "lin_read": {
        "ids": {
          "1": 8
        }
      }
    }
  }
}

```

but with queryWithVars:

```auto
query withvar($a:string,$b:string){
 query(func:uid($a)) {
  uid
  name
  one:~candidate_company @facets(eq(dept_name, $b)) {
    count(uid)
  }
 }
}

```

only return

```auto
{"query":[{"uid":"0x2","name":"百度"}]}

```

it seems that it’s not the different between “$b” and $b

@MichelDiz  
what do you think of this?

---

<div class="post-metadata">

### Author: ![BlankRain](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/blankrain/32/1592_2.png) [@BlankRain](https://discuss.dgraph.io/u/BlankRain)
#### Post date: [June 26, 2018, 10:35am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/4 "2018-06-26T10:35:02Z")

</div>

seems like a bug~  
you can create a new issue

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [June 26, 2018, 3:50pm UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/5 "2018-06-26T15:50:37Z")

</div>

Maybe the $ b field is wrong, or is passing some extra character. If this field is wrong the answer will be empty indeed. You have to make sure you are correctly passing the Var. btw this is a “GraphQL Variable”, It is not a Dgraph variable itself.

For test effect, try to see if searching $ b for dept\_name, returns the correct department.

```auto
{
  dept(func: eq(dept_name, $b)) {
   uid
   expand(_all_)
  }
}

```

It would be interesting to be able to expand the variable within the query field. But at the moment I can not test, I’m just with Ratel to test things fast. And Ratel does not apparently accept Graphql Var.

> I was digging here [dgraph/systest/mutations\_test.go at master · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/blob/master/systest/mutations_test.go)  
> to see if there was anything that might help

---

<div class="post-metadata">

### Author: ![shanghai-Jerry](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shanghai-jerry/32/863_2.png) [@shanghai-Jerry](https://discuss.dgraph.io/u/shanghai-Jerry)
#### Post date: [June 27, 2018, 2:03am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/6 "2018-06-27T02:03:55Z")

</div>

i am sure there is not passing some extra character, and i found it might be a bug filter in facets. look this query:

```auto
query withvar($a:string,$b:string,$c:string){
 query(func:uid($a)) {
  uid
  name
  one:~candidate_company @facets(eq(dept_name, "$b")) {
    count(uid)
  }
  ~candidate_company @filter(eq(name, "$c")) {
    uid
    name
  }
 }
}

```

with vars:

```auto
vars.put("$a", "0x02");
vars.put("$b", "ai");
vars.put("$c", "jerry");

```

result:

```auto
{"query":[{"uid":"0xb","name":"百度","~candidate_company":[{"uid":"0xa","name":"jerry"}]}]}

```

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [June 27, 2018, 2:11am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/7 "2018-06-27T02:11:02Z")

</div>

Okay, Fill a issue on Github. As you tested with common characters (and not Chinese) already helped filter out a possible bug with characters. Sounds like a valid issue. Even so, I’ll do tests during this.

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [July 1, 2018, 2:06am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/8 "2018-07-01T02:06:19Z")

</div>

Hey, can you test this with the last release? cuz there is a new Support for GraphQL vars. Can be related.

> Support GraphQL vars with filters. #2359  
> [Support GraphQL variables with filters. by pawanrawal · Pull Request #2359 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/pull/2359)  
> Support GraphQL vars as args for Regexp function. #2353  
> [Support GraphQL variables as args for regexp function by pawanrawal · Pull Request #2353 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/pull/2353)

> **[Releases · dgraph-io/dgraph](https://github.com/dgraph-io/dgraph/releases)**
>
> The high-performance database for modern applications - dgraph-io/dgraph

Cheers.

---

<div class="post-metadata">

### Author: ![shanghai-Jerry](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shanghai-jerry/32/863_2.png) [@shanghai-Jerry](https://discuss.dgraph.io/u/shanghai-Jerry)
#### Post date: [July 1, 2018, 2:56am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/9 "2018-07-01T02:56:52Z")

</div>

bad news, it’s still not working in facets : predicate @facets(eq(dept\_name, “$b”)) {  
…  
}

but it works in this way: predicate @filter(gt(birthday, “$b”)) {  
…  
}

---

<div class="post-metadata">

### Author: ![BlankRain](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/blankrain/32/1592_2.png) [@BlankRain](https://discuss.dgraph.io/u/BlankRain)
#### Post date: [July 3, 2018, 9:13am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/10 "2018-07-03T09:13:21Z")

</div>

> [@MichelDiz](#):
>
> I’m just with Ratel to test things fast. And Ratel does not apparently accept Graphql Var.

@MichelDiz  
as example showed in [Query Structure - Dgraph](https://docs.dgraph.io/query-language/#graphql-variables)  
you can use curl to write some scripts to do this job.

```auto
curl localhost:8080/query -XPOST -H 'X-Dgraph-Vars: {"$b": "10", "$name": "Steven Spielberg"}' -d '
query test($a: int = 2, $b: int!, $name: string) {
  me(func: allofterms(name@en, $name)) {
    director.film (first: $a, offset: $b) {
      genre(first: $a) {
        name@en
      }
    }
  }
}' | python -m json.tool | less

```

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [July 3, 2018, 3:45pm UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/11 "2018-07-03T15:45:23Z")

</div>

Yes Yes. This example has problems to play online. I forgot what I could do it locally. It’s just that you were using your own examples, which I do not have access to. Then my head did not stop to reproduce something close. But I can mount an instance by loading the RDF movielens and test this.

---

<div class="post-metadata">

### Author: ![shanghai-Jerry](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shanghai-jerry/32/863_2.png) [@shanghai-Jerry](https://discuss.dgraph.io/u/shanghai-Jerry)
#### Post date: [July 11, 2018, 3:44am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/12 "2018-07-11T03:44:17Z")

</div>

i have a query like this:  
query withvar($a:string, $b:string) {  
var(func:uid($a)) {  
company\_dept @filter(uid($b)){  
~candidate\_dept @groupby(age\_node) {  
a as count(uid)  
}  
}  
}  
query(func:uid(a)) {  
uid  
name  
total:val(a)  
}  
}  
,  
if i don’t pass var $b, like it’s a empty string, and i want to let this filter: @filter(uid($b)) do not work in this query,  
or what var should i pass to $b to let filter don’t work.  
how to do this?

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [July 11, 2018, 2:37pm UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/13 "2018-07-11T14:37:45Z")

</div>

I did not fully understand the question, but look.

In that case I think that in your application I could deal with this by creating one more query if that one it is not successful. Like:

```auto
If (company_dept.length === 0) {
  doNextquerie();
}

```

The next query would be a predictable one, keeping in mind that it does not have the value used in company\_dept.

---

<div class="post-metadata">

### Author: ![shanghai-Jerry](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shanghai-jerry/32/863_2.png) [@shanghai-Jerry](https://discuss.dgraph.io/u/shanghai-Jerry)
#### Post date: [July 12, 2018, 4:03am UTC](https://discuss.dgraph.io/t/query-and-querywithvars/2796/14 "2018-07-12T04:03:30Z")

</div>

yes, but i want to combine in one query.  
if it’s not possible, one or more query will be great
