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 github.com/dgraph-io/dgraph/graphql/dgraph/graphquery.go is the culprit. It should probably be extended to support all valid gql± queries.

I parse this query

query{
        q0(func: regexp(name, /myname.*/),first:1){
                a.name: test.name, a.open: test.open
                test.has{
                        test.Temporal{
                                b.name: test.name, b.open: test.open, b.xid: test.xid
                        }
                }
        }
}

but when I print the AST I get

query {
  q0 : , first: 1)  a.name : test.name
  a.open : test.open
  test.has {
    test.Temporal {
      b.name : test.name
      b.open : test.open
      b.xid : test.xid
    }
  }
}

Notice the regexp call is completely missing.

1 Like

I can see what you mean: https://github.com/dgraph-io/dgraph/blob/master/graphql/dgraph/graphquery.go#L127-L144 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