From ed08e0cb8bc2a3482aa2158f46d31832f45d30b2 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 1 Sep 2020 13:15:24 +0900 Subject: [PATCH 1/5] Fix formatting for directives in FragmentDefinition --- src/language-graphql/printer-graphql.js | 19 ++++++++++++++++++- .../__snapshots__/jsfmt.spec.js.snap | 10 +++++----- .../newline/__snapshots__/jsfmt.spec.js.snap | 14 +++++++------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 637a4f2621d6..09ab7ef79e8b 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -611,7 +611,24 @@ function printDirectives(path, print, n) { return ""; } - return group(concat([line, join(line, path.map(print, "directives"))])); + if (n.kind === "FragmentDefinition" || n.kind === "OperationDefinition") { + return group(concat([line, join(line, path.map(print, "directives"))])); + } + + return concat([ + " ", + group( + indent( + concat([ + softline, + join( + concat([ifBreak("", " "), softline]), + path.map(print, "directives") + ), + ]) + ) + ), + ]); } function printSequence(sequencePath, options, print) { diff --git a/tests/graphql/directives/__snapshots__/jsfmt.spec.js.snap b/tests/graphql/directives/__snapshots__/jsfmt.spec.js.snap index c943173bf49a..1194993028e6 100644 --- a/tests/graphql/directives/__snapshots__/jsfmt.spec.js.snap +++ b/tests/graphql/directives/__snapshots__/jsfmt.spec.js.snap @@ -32,7 +32,7 @@ query MyQuery @directive(arg: 5) { field @skip(if: true) @nope otherField ...fragmentSpread - @include(if: ["this isn't even a boolean", "wow, that's really odd"]) + @include(if: ["this isn't even a boolean", "wow, that's really odd"]) } fragment YouCanHaveDirectivesHereToo on SomeType @yesReally(what: "yes") { @@ -45,10 +45,10 @@ fragment YouCanHaveDirectivesHereToo on SomeType @yesReally(what: "yes") { } thisFieldHasALotOfDirectives - @thisIsthefirst - @thisIsTheSecond - @thisIsTheThird - @thisIstheFourthWillBeTooLongForSure(and: "it has arguments as well") + @thisIsthefirst + @thisIsTheSecond + @thisIsTheThird + @thisIstheFourthWillBeTooLongForSure(and: "it has arguments as well") } query QueryWVars($x: String) @directive { diff --git a/tests/graphql/newline/__snapshots__/jsfmt.spec.js.snap b/tests/graphql/newline/__snapshots__/jsfmt.spec.js.snap index 7af04e274db1..70320b53dbce 100644 --- a/tests/graphql/newline/__snapshots__/jsfmt.spec.js.snap +++ b/tests/graphql/newline/__snapshots__/jsfmt.spec.js.snap @@ -63,16 +63,16 @@ query MyQuery arg2: 10 ) { field - @skip( - if: true + @skip( + if: true - # comment - cursor: 10 - ) - @nope + # comment + cursor: 10 + ) + @nope otherField ...fragmentSpread - @include(if: ["this isn't even a boolean", "wow, that's really odd"]) + @include(if: ["this isn't even a boolean", "wow, that's really odd"]) } ================================================================================ From 76a1e293a4b58235f5280d7e7efb8e32a95fc8fe Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 1 Sep 2020 13:26:39 +0900 Subject: [PATCH 2/5] Add changelog --- changelog_unreleased/graphql/pr-9116.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 changelog_unreleased/graphql/pr-9116.md diff --git a/changelog_unreleased/graphql/pr-9116.md b/changelog_unreleased/graphql/pr-9116.md new file mode 100644 index 000000000000..6ccaf6cbfc56 --- /dev/null +++ b/changelog_unreleased/graphql/pr-9116.md @@ -0,0 +1,28 @@ +#### Fix formatting for directives in fields (#9116 by @sosukesuzuki) + + +```graphql +# Input +type Query { + someQuery(id: ID!, someOtherData: String!): String! @deprecated @isAuthenticated + versions: Versions! +} + + +# Prettier stable +type Query { + someQuery(id: ID!, someOtherData: String!): String! + @deprecated + @isAuthenticated + versions: Versions! +} + +# Prettier master +type Query { + someQuery(id: ID!, someOtherData: String!): String! + @deprecated + @isAuthenticated + versions: Versions! +} + +``` From ff1b7a96b3dd03eaab6a7a43149a1c2d1cef648f Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Thu, 3 Sep 2020 00:09:02 +0900 Subject: [PATCH 3/5] Update src/language-graphql/printer-graphql.js Co-authored-by: Georgii Dolzhykov --- src/language-graphql/printer-graphql.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 09ab7ef79e8b..178bbe070055 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -622,7 +622,7 @@ function printDirectives(path, print, n) { concat([ softline, join( - concat([ifBreak("", " "), softline]), + line, path.map(print, "directives") ), ]) From dc4e4f4866a1b843b992cf493dc25698a169a32a Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 3 Sep 2020 00:13:56 +0900 Subject: [PATCH 4/5] Fix by Prettier --- src/language-graphql/printer-graphql.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 178bbe070055..9fb4becc9bd2 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -618,15 +618,7 @@ function printDirectives(path, print, n) { return concat([ " ", group( - indent( - concat([ - softline, - join( - line, - path.map(print, "directives") - ), - ]) - ) + indent(concat([softline, join(line, path.map(print, "directives"))])) ), ]); } From 7de5319ecaf824ea08863a359e285a2d982a88ec Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 3 Sep 2020 00:18:44 +0900 Subject: [PATCH 5/5] Refactor --- src/language-graphql/printer-graphql.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 9fb4becc9bd2..5320a063a55b 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -611,16 +611,13 @@ function printDirectives(path, print, n) { return ""; } + const printed = join(line, path.map(print, "directives")); + if (n.kind === "FragmentDefinition" || n.kind === "OperationDefinition") { - return group(concat([line, join(line, path.map(print, "directives"))])); + return group(concat([line, printed])); } - return concat([ - " ", - group( - indent(concat([softline, join(line, path.map(print, "directives"))])) - ), - ]); + return concat([" ", group(indent(concat([softline, printed])))]); } function printSequence(sequencePath, options, print) {