Skip to content

please document the format of the AST #45

Open
@bionicles

Description

@bionicles

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?

Activity

tailhook

tailhook commented on Mar 12, 2021

@tailhook
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

bionicles commented on Mar 13, 2021

@bionicles
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

tailhook commented on Mar 15, 2021

@tailhook
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

bionicles commented on Apr 5, 2021

@bionicles
Author

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

tailhook commented on Apr 6, 2021

@tailhook
Collaborator

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @tailhook@bionicles

        Issue actions

          please document the format of the AST · Issue #45 · graphql-rust/graphql-parser