Query and queryWithVars

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

i use queryWithVars:

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

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

but when i use query:

{
 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

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

any clues?
@mrjn

haha

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

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

after try:

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

query:

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

result:

{
  "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:

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

only return

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

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

@MichelDiz
what do you think of this?

seems like a bug~
you can create a new issue

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.

{
  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 https://github.com/dgraph-io/dgraph/blob/master/systest/mutations_test.go
to see if there was anything that might help

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

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:

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

result:

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

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.

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
https://github.com/dgraph-io/dgraph/pull/2359
Support GraphQL vars as args for Regexp function. #2353
https://github.com/dgraph-io/dgraph/pull/2353

Cheers.

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”)) {

}

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

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
1 Like

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.

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?

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:

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.

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