graphql-parser icon indicating copy to clipboard operation
graphql-parser copied to clipboard

please document the format of the AST

Open bionicles opened this issue 3 years ago • 5 comments

in the docs, you show the result of printing the AST -- what's the format of the data structure? It would be handy to look at. Is it just a nested JSON?

bionicles avatar Mar 10 '21 18:03 bionicles

I'm not sure what you refer specifically. I think there are two possible formats: Debug which is mostly generated by Rust, so you can probably refer to the Rust documentation (but you should not rely on specifics) and you can also format AST as GraphQL language. Then you should refer to the GraphQL language reference. The actual whitespace in GraphQL is unstable for now, we only guarantee that it has valid syntax.

tailhook avatar Mar 12 '21 16:03 tailhook

what i mean is, i'd like to write a function to map the GraphQL AST onto an AST for a different query language (SQL, Cypher, Gremlin, CQL, Redis, Mongo) etc etc -- is there a way to traverse the AST and transform it into a different language?

bionicles avatar Mar 13 '21 15:03 bionicles

Well, I usually use normal functions and matches. Since every node is unique. And having a method per node type or similar usually not very convenient in Rust (because either requires borrow checker hacks, or a lot of unwrapping optionals). There are #26 and #31 but none of them is merged because I don't have enough time to work on them.

tailhook avatar Mar 15 '21 12:03 tailhook

all i mean is, in the docs, can you put an example of what the ast looks like? not a big deal, but right now the docs just show the AST as equivalent to the query string:

let ast = parse_query::<&str>("query MyQuery { field1, field2 }")?;
// Format canonical representation
assert_eq!(format!("{}", ast), "\
query MyQuery {
  field1
  field2
}
");

if AST is the same thing as the query, then "parse_query" doesn't actually do anything. I thought the AST was a Tree, not a string! Why do we want the AST to be a string?

bionicles avatar Apr 05 '21 21:04 bionicles

It's just that "Display" trait of the AST outputs the query. This is convenient if you want to extract a piece of query or modify query and convert back to string.

To see AST nodes use debug output: format!("{:#?}", ast)

tailhook avatar Apr 06 '21 10:04 tailhook