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

GraphQL: Fix formatting for directives in fields #9116

Merged
merged 5 commits into from Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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!
}

```
19 changes: 18 additions & 1 deletion src/language-graphql/printer-graphql.js
Expand Up @@ -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]),
sosukesuzuki marked this conversation as resolved.
Show resolved Hide resolved
path.map(print, "directives")
),
])
)
),
]);
}

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