Skip to content

Commit 5c31fd3

Browse files
author
Adrian Zgorzalek
committed
Pretty format variable definitions for both operations and fragments.
When there is more than a couple argument values, pretty formatting isn't doing a good job. Instead I would like to take the following approach: When there is one argument, print as is, meaning: query Q ($arg: Type = Default) { When there is more than one do the following: query Q ( $arg1: Type = Default, $arg2 ) { An alternative would be to have closing bracket be in the same line as final argument. I am fine with either, as changes which introduce another argument on last position always touch two lines - traiiling comma will have to be added.
1 parent f529809 commit 5c31fd3

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/language/__tests__/printer-test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,22 @@ describe('Printer: Query document', () => {
157157
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType {
158158
id
159159
}
160+
161+
fragment Simple($a: Boolean = true) on TestType {
162+
id
163+
}
160164
`,
161165
{ experimentalFragmentVariables: true },
162166
);
163167
expect(print(fragmentWithVariable)).to.equal(dedent`
164-
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType {
168+
fragment Foo(
169+
$a: ComplexType,
170+
$b: Boolean = false
171+
) on TestType {
172+
id
173+
}
174+
175+
fragment Simple($a: Boolean = true) on TestType {
165176
id
166177
}
167178
`);
@@ -174,7 +185,10 @@ describe('Printer: Query document', () => {
174185

175186
expect(printed).to.equal(
176187
dedent(String.raw`
177-
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
188+
query queryName(
189+
$foo: ComplexType,
190+
$site: Site = MOBILE
191+
) @onQuery {
178192
whoever123is: node(id: [123, 456]) {
179193
id
180194
... on User @onInlineFragment {

src/language/printer.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const printDocASTReducer = {
2828
OperationDefinition(node) {
2929
const op = node.operation;
3030
const name = node.name;
31-
const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')');
31+
const varDefs = printVariableDefinitions(node.variableDefinitions);
3232
const directives = join(node.directives, ' ');
3333
const selectionSet = node.selectionSet;
3434
// Anonymous queries with no directives or variable definitions can use
@@ -78,7 +78,7 @@ const printDocASTReducer = {
7878
}) =>
7979
// Note: fragment variable definitions are experimental and may be changed
8080
// or removed in the future.
81-
`fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` +
81+
`fragment ${name}${printVariableDefinitions(variableDefinitions)} ` +
8282
`on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}` +
8383
selectionSet,
8484

@@ -283,3 +283,17 @@ function printBlockString(value, isDescription) {
283283
? `"""\n${isDescription ? escaped : indent(escaped)}\n"""`
284284
: `"""${escaped.replace(/"$/, '"\n')}"""`;
285285
}
286+
287+
/**
288+
* Prints variables one per line with a trailing comma, when there is more
289+
* than one argument. Otherwise print inline.
290+
*/
291+
function printVariableDefinitions(args) {
292+
if (!args || args.length === 0) {
293+
return '';
294+
}
295+
if (args.length === 1) {
296+
return '(' + args[0] + ')';
297+
}
298+
return '(\n' + indent(join(args, ',\n')) + '\n)';
299+
}

0 commit comments

Comments
 (0)