Skip to content

Commit

Permalink
GraphQL: Fix formatting for directives in fields (#9116)
Browse files Browse the repository at this point in the history
* Fix formatting for directives in FragmentDefinition

* Add changelog

* Update src/language-graphql/printer-graphql.js

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>

* Fix by Prettier

* Refactor

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
  • Loading branch information
sosukesuzuki and thorn0 committed Sep 15, 2020
1 parent a836319 commit d5da779
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
28 changes: 28 additions & 0 deletions changelog_unreleased/graphql/pr-9116.md
@@ -0,0 +1,28 @@
#### Fix formatting for directives in fields (#9116 by @sosukesuzuki)

<!-- prettier-ignore -->
```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!
}

```
8 changes: 7 additions & 1 deletion src/language-graphql/printer-graphql.js
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions tests/graphql/directives/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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") {
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions tests/graphql/newline/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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"])
}
================================================================================
Expand Down

0 comments on commit d5da779

Please sign in to comment.