Printing gql+- queries with AsString produces bad query for certain query root functions

I can write and run a gql± query with a regexp function at the root, but when I print it with AsString that part is completely omitted, producing an invalid query. I think that writeRoot in is the culprit. It should probably be extended to support all valid gql± queries.

I parse this query

        q0(func: regexp(name, /myname.*/),first:1){
                      ,, b.xid: test.xid

but when I print the AST I get

query {
  q0 : , first: 1) : :
  test.has {
    test.Temporal { : :
      b.xid : test.xid

Notice the regexp call is completely missing.

1 Like

I can see what you mean: dgraph/graphquery.go at master · dgraph-io/dgraph · GitHub does not have an exhaustive switch case.

I will put in a PR later on this enhancement.

However, I’m curious as to what your application may be that you are reflecting upon the parser structures and then getting the query back again.

Pretty printing for starters. Additionally I would like to construct dgraph queries based on user input and render them out and send them to dgraph, and going through the AST seems less error prone that rendering directly to a string.

I have noticed some other issues with the rendering as well. In the test query I posted the opening curly bracket on the q0 line is missing. Also, functions are printed assuming that their Attrs are in Args, but they aren’t, stuff like that. I would really appreciate is these were fixed as well. Thanks in advance.

1 Like

will do. I had opened a much larger rabbithole than I had expected. See also: Proposal: Boyscout Refactoring of Functions - by enumerating them