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

Pretty format variable definitions for both operations and fragments. #1557

Closed
wants to merge 2 commits into from

Conversation

adek05
Copy link

@adek05 adek05 commented Oct 23, 2018

When there is more than a couple argument values, pretty formatting
isn't doing a good job.

Instead I would like to take the following approach:

When there is one argument, print as is, meaning:

query Q ($arg: Type = Default) {
When there is more than one do the following:

  query Q (
    $arg1: Type = Default,
    $arg2
  ) {

An alternative would be to have closing bracket be in the same line
as final argument. I am fine with either, as changes which introduce
another argument on last position always touch two lines - trailing
comma will have to be added.

Edited tests to make sure everything works as expected.

Copy link
Contributor

@mjmahone mjmahone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for improving the readability of generated queries! This is a minor frustration for me every few months.

Just the one small nit about removing the comma, and this will be good to go!

`,
{ experimentalFragmentVariables: true },
);
expect(print(fragmentWithVariable)).to.equal(dedent`
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType {
fragment Foo(
$a: ComplexType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer we just drop the comma entirely. It's totally unnecessary, and causes more arguments about whether we should have a trailing comma or not. But as this PR is about bringing fragment variable definitions to parity with query variable definitions, I won't block us on that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can fix the test, that is fine.

if (args.length === 1) {
return '(' + args[0] + ')';
}
return '(\n' + indent(join(args, ',\n')) + '\n)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were only using the comma due to having everything on one line. A newline is a perfectly acceptable variable definition separator, just as it is for fields inside a document.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!

@IvanGoncharov
Copy link
Member

@adek05 @mjmahone Single line approach works for simple cases but not for something like this:

query Q ($arg: Type = Default @someQueryVariableDirective(withArg: "test")) {

VS

query Q (
  $arg: Type = Default @someQueryVariableDirective(withArg: "test")
) {

I think single argument per line is safer option for now but in long run we need to implement something simular to prettier algorithm.

@mjmahone
Copy link
Contributor

mjmahone commented Nov 4, 2018

@IvanGoncharov I wasn't sure whether single variable should be special-cased, but your example convinced me it should not be.

Copy link
Author

@adek05 adek05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for comments, I'll make single argument follow the default.

@adek05 adek05 force-pushed the fix-arguments-formatting branch 2 times, most recently from 1d266c4 to 917eefc Compare November 5, 2018 16:28
Adrian Zgorzalek added 2 commits November 5, 2018 17:42
When there is more than a couple argument values, pretty formatting
isn't doing a good job.

Instead I would like to take the following approach:

When there is one argument, print as is, meaning:

  query Q ($arg: Type = Default) {

When there is more than one do the following:

  query Q (
    $arg1: Type = Default,
    $arg2
  ) {

An alternative would be to have closing bracket be in the same line
as final argument. I am fine with either, as changes which introduce
another argument on last position always touch two lines - traiiling
comma will have to be added.
@adek05 adek05 force-pushed the fix-arguments-formatting branch 2 times, most recently from 202804c to eb60915 Compare November 5, 2018 16:50
@adek05
Copy link
Author

adek05 commented Nov 5, 2018

Changes incorporated, let me know if there is anything else you would like to see or good to go.

query ($foo: TestType) @testDirective {
query (
$foo: TestType
) @testDirective {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels worse to me. Why wrap like this?

if (!args || args.length === 0) {
return '';
}
return '(\n' + indent(join(args, '\n')) + '\n)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to see this function factored out to avoid duplication, but I think the additional new lines aren't helping legibility

@leebyron
Copy link
Contributor

leebyron commented Nov 5, 2018

Let's remember that this AST printer does not aim to be prettier, it should print well when possible, but I don't think we can attempt full support for edge cases like @IvanGoncharov's without implementing parts of Prettier's algorithm. Prettier already has support for GraphQL, which should be used for more complex cases.

However, I also want to avoid a "fully expanded" printing of the AST because of these limitations. I think that hurts legibility more than it helps it. For instance simple cases like the example below, one line is preferable.

query Foo($bar: Int, $foo: Float) {
  field
}

Would it be possible to compromise by using the length of the child strings to determine if they should be a one line or multi-line print?

Also, why only variable definitions? argument definitions, arguments on fields, elements of input objects, lists, are also examples of things currently always printed as a comma-separated list but could suffer from the same issue. I'd love to see a solution that creates consistency instead of a one-off for variable definitions alone.

@IvanGoncharov
Copy link
Member

Extracted as #3230

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

Successfully merging this pull request may close these issues.

5 participants