From d5da779cb195443c935fd7171f5e81141e2f62e5 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Thu, 3 Sep 2020 02:57:17 +0900 Subject: [PATCH] GraphQL: Fix formatting for directives in fields (#9116) * Fix formatting for directives in FragmentDefinition * Add changelog * Update src/language-graphql/printer-graphql.js Co-authored-by: Georgii Dolzhykov * Fix by Prettier * Refactor Co-authored-by: Georgii Dolzhykov --- changelog_unreleased/graphql/pr-9116.md | 28 +++++++++++++++++++ src/language-graphql/printer-graphql.js | 8 +++++- .../__snapshots__/jsfmt.spec.js.snap | 10 +++---- .../newline/__snapshots__/jsfmt.spec.js.snap | 14 +++++----- 4 files changed, 47 insertions(+), 13 deletions(-) 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! +} + +``` diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 637a4f2621d6..5320a063a55b 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -611,7 +611,13 @@ function printDirectives(path, print, n) { return ""; } - return group(concat([line, join(line, path.map(print, "directives"))])); + const printed = join(line, path.map(print, "directives")); + + if (n.kind === "FragmentDefinition" || n.kind === "OperationDefinition") { + return group(concat([line, printed])); + } + + return concat([" ", group(indent(concat([softline, printed])))]); } 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"]) } ================================================================================