Skip to content

Pretty format variable definitions for both operations and fragments. #1557

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/language/__tests__/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ describe('Printer: Query document', () => {
'query ($foo: TestType) @testDirective { id, name }',
);
expect(print(queryAstWithArtifacts)).to.equal(dedent`
query ($foo: TestType) @testDirective {
query (
$foo: TestType
) @testDirective {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels worse to me. Why wrap like this?

id
name
}
Expand All @@ -70,7 +72,9 @@ describe('Printer: Query document', () => {
'mutation ($foo: TestType) @testDirective { id, name }',
);
expect(print(mutationAstWithArtifacts)).to.equal(dedent`
mutation ($foo: TestType) @testDirective {
mutation (
$foo: TestType
) @testDirective {
id
name
}
Expand All @@ -82,7 +86,9 @@ describe('Printer: Query document', () => {
'query ($foo: TestType = {a: 123} @testDirective(if: true) @test) { id }',
);
expect(print(queryAstWithVariableDirective)).to.equal(dedent`
query ($foo: TestType = {a: 123} @testDirective(if: true) @test) {
query (
$foo: TestType = {a: 123} @testDirective(if: true) @test
) {
id
}
`);
Expand All @@ -96,7 +102,9 @@ describe('Printer: Query document', () => {
},
);
expect(print(queryAstWithVariableDirective)).to.equal(dedent`
fragment Foo($foo: TestType @test) on TestType @testDirective {
fragment Foo(
$foo: TestType @test
) on TestType @testDirective {
id
}
`);
Expand Down Expand Up @@ -161,7 +169,10 @@ describe('Printer: Query document', () => {
{ experimentalFragmentVariables: true },
);
expect(print(fragmentWithVariable)).to.equal(dedent`
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType {
fragment Foo(
$a: ComplexType
$b: Boolean = false
) on TestType {
id
}
`);
Expand All @@ -174,7 +185,10 @@ describe('Printer: Query document', () => {

expect(printed).to.equal(
dedent(String.raw`
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
query queryName(
$foo: ComplexType
$site: Site = MOBILE
) @onQuery {
whoever123is: node(id: [123, 456]) {
id
... on User @onInlineFragment {
Expand Down Expand Up @@ -203,7 +217,9 @@ describe('Printer: Query document', () => {
}
}

subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) @onSubscription {
subscription StoryLikeSubscription(
$input: StoryLikeSubscribeInput
) @onSubscription {
storyLikeSubscribe(input: $input) {
story {
likers {
Expand Down
14 changes: 12 additions & 2 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const printDocASTReducer = {
OperationDefinition(node) {
const op = node.operation;
const name = node.name;
const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')');
const varDefs = printVariableDefinitions(node.variableDefinitions);
const directives = join(node.directives, ' ');
const selectionSet = node.selectionSet;
// Anonymous queries with no directives or variable definitions can use
Expand Down Expand Up @@ -78,7 +78,7 @@ const printDocASTReducer = {
}) =>
// Note: fragment variable definitions are experimental and may be changed
// or removed in the future.
`fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` +
`fragment ${name}${printVariableDefinitions(variableDefinitions)} ` +
`on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}` +
selectionSet,

Expand Down Expand Up @@ -283,3 +283,13 @@ function printBlockString(value, isDescription) {
? `"""\n${isDescription ? escaped : indent(escaped)}\n"""`
: `"""${escaped.replace(/"$/, '"\n')}"""`;
}

/**
* Prints variables one per line.
*/
function printVariableDefinitions(args) {
if (!args || args.length === 0) {
return '';
}
return '(\n' + indent(join(args, '\n')) + '\n)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to see this function factored out to avoid duplication, but I think the additional new lines aren't helping legibility

}