Strange problem using uid() function in queries

(Gordon Guthrie) #1

Having strange problems getting the Python client to work.

I have a function that creates a single new node and I checked that it worked by querying ratel with a function

{read_by_uid(func: uid(0x9c4c)) {uid server schema object_type object system database}}


But when I try and query it with the Python client I get a different result:

def read_by_uid(connection, uid):
    print("reading by uid {}".format(uid))
    query = """query read_by_uid($UID: string) {
                read_by_uid(func: uid($UID)) {
    variables = {"$UID": uid}
        res = connection.txn(read_only=True).query(query, variables=variables)
        payload = json.loads(res.json)
        print("payload is {}".format(payload))
    except BaseException as e:
        print("something {} went wrong".format(e))
        raise Exception("Something {} went wrong reading an object by uid {}".format(e, uid))
    return payload['read_by_uid']

In this instance the playload returns only includes the uid I pass in:

payload is {'read_by_uid': [{'uid': '0x9c4c'}]}

Our instinct is that the query parameterisation is turning the hex uid into something else and the query is then failing to find the node (as you would expect).

To investigate this we went through the Python client instrumenting it, only to run into a brick wall - the actually application of the paramaters to the query appears to happen inside the protocol buffer code generated by the gRPC compiler toolchain. Strike 1

We then tried to see if any of the debug apis could help us. It seems it is possible to append a ?debug=True parameter to the http API - but not in the python client. Strike 2

Then we looked at the debug api’s that the alpha nodes expose on localhost:8080/debug/vars but they show stats but not details of query execution logs or anything. Strike 3

The next stage would be to see if we can investigate the post with the CLI debug tool, but as we are running Dgraph inside a transient docker-compose mini-cluster I don’t know how to get access to the file system to run the debug tools.

Any other suggestions? or longer Python tutorials with more examples?

(Daniel Mai) #2

You can take a look at the zPages for Server.Query. That shows you the recent queries that Dgraph Alpha has processed, including the query variable mapping.

(Gordon Guthrie) #3

Thanks for that, the zPages are smashing

Worked out what my problem was - I was writing an object and then trying to read it back inside the same transaction - so it wasn’t-yet-written and thus returned nothing…