Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please document the format of the AST #45

Open
bionicles opened this issue Mar 10, 2021 · 5 comments
Open

please document the format of the AST #45

bionicles opened this issue Mar 10, 2021 · 5 comments

Comments

@bionicles
Copy link

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?

@tailhook
Copy link
Collaborator

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.

@bionicles
Copy link
Author

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?

@tailhook
Copy link
Collaborator

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.

@bionicles
Copy link
Author

bionicles commented Apr 5, 2021

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?

@tailhook
Copy link
Collaborator

tailhook commented Apr 6, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants