Configuring "depth" in the variables, when passing through the query


(Aman) #1

@amanmangal

In this example - https://github.com/dgraph-io/pydgraph/blob/master/examples/simple/simple.py

At the method,

def query_data(client):

you have mentioned variables = {'$a': 'Alice'}, but is it possible to pass multiple data, like depth along with the name?

query = """query get_all($a: string, $b: integer) { get_all(func: anyoftext(name@en, $a))@recurse(depth: $b) {
        uid
        name@en
        normalizes_to {name@en normalizes_to {name@en}}
        ~normalizes_to {name@en normalizes_to {name@en}
     }
variables = {'$a': keywords, '$b': 5}

where keywords is a list of words and depth is an integer.
If I do that I am getting an error, as

Exception: Values and keys in variable map must be strings

If I hard code in the query,

query = """query get_all($a: string) { get_all(func: anyoftext(name@en, $a))@recurse(depth: 5) {
        uid
        name@en
        normalizes_to {name@en normalizes_to {name@en}}
        ~normalizes_to {name@en normalizes_to {name@en}
     }
variables = {'$a': keywords}

It works !!


(Aman Mangal) #2

Shouldn’t you have to specify type of b variable in the first query, that it is integer?


(Aman) #4

I missed this part in my query, but still I am getting the same error.


(Aman Mangal) #5
query = """query get_all($a: string, $b: int) { get_all(func: anyoftext(name@en, $a))@recurse(depth: $b) {
        uid
        name@en
        normalizes_to {name@en normalizes_to {name@en}}
        ~normalizes_to {name@en normalizes_to {name@en}
     }
variables = {'$a': keywords, '$b': '5'}

You have to use type as int, and the values in the map are always string. We parse them internally based on the expected type.


(Aman) #6

@amanmangal even after doing the changes as you suggested. Still I am getting below errors.

    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
	status = StatusCode.UNKNOWN
	details = "Expected value inside @recurse() for key: depth."
	debug_error_string = "{"created":"@1565942227.567215716","description":"Error received from peer ipv4:132.186.173.33:9080","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"Expected value inside @recurse() for key: depth.","grpc_status":2}"
>

(Aman Mangal) #7

Seems like something is not correct with arguments to recurse. Could you share the query?


(Aman) #8

@amanmangal

"""query get_all($a: string,  $b: int) { get_all(func: anyoftext(name@en, $a))@recurse(depth: $b) {
        uid
        name@en
        normalizes_to {name@en normalizes_to {name@en}}
        ~normalizes_to {name@en normalizes_to {name@en}}
        metric_used_in {name@en normalizes_to {name@en}}
        ~metric_used_in {name@en normalizes_to {name@en}}


     }
    }"""

keywords = "policy"
variables = {'$a': keywords, '$b': '5'}

res = client.txn().query(query, variables=variables)

(Aman Mangal) #9

We currently do not support depth value to be passed as a variable argument. Feel free to raise an issue on GitHub, this may be useful to support.

@recurse(depth: $b) 

$b needs to be replaced with actual value here.


(Aman) #10

@amanmangal.

Sure will do that.
Earlier I was replacing a number with the variable but it won’t solve my problem as it will result to hard-coded values to which I don’t want.

Is there any workaround?

Thanks


(Aman Mangal) #11

I think for now you just have to use javascript (or any other language for that matter) to format the query.