graphql-rust / graphql-parser

A graphql query language and schema definition language parser and formatter for rust
Apache License 2.0
355 stars 77 forks source link

format broken for nameless operation with variables #46

Closed timsuchanek closed 3 years ago

timsuchanek commented 3 years ago

Formatting this query:

query ($first: Int, $second: Int) {
  field1(first: $first)
  field2(second: $second)
}

returns this result:

query {
  field1(first: $first)
  field2(second: $second)
}

Test code:

use graphql_parser::parse_query;

fn main() {
    let ast = parse_query::<&str>(
        "\
query ($first: Int, $second: Int) {
  field1(first: $first)
  field2(second: $second)
}",
    )
    .unwrap();
    // Format canonical representation
    println!("{}", ast);
}

And I think the culprit is here: https://github.com/graphql-rust/graphql-parser/blob/master/src/query/format.rs#L156

tailhook commented 3 years ago

Hm, I thought this is not allowed syntax (so it's actually a parser which is broken). Is it?

timsuchanek commented 3 years ago

Yes could also be the parser. So graphql-js allows it and we have real world application with that use-case, which already runs fine on graphql-js.

timsuchanek commented 3 years ago

Where would you fix that? If you give me a hint in the code, I could also have a look.

tailhook commented 3 years ago

Okay, looks like I'm wrong. So initially pointed line is the place to fix (probably by just moving closing brace). But please also add a test. Thanks!

timsuchanek commented 3 years ago

Sounds good! PR is ready :)