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"]) } ================================================================================