From ddf5cc28098a7ce8331beeabbd2b74b36ff2aff0 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Tue, 14 Jul 2015 18:10:50 -0700 Subject: [PATCH] [RFC] standard printer uses no comma between fields A couple issues have opened up about inconsistency in rendering graphql queries in the spec and in the ref impl - especially with respect to whitespace and commas. Here I'm proposing that it's easier to read queries where every field is always on it's own line (eg, no `image { width, height }` one-liners) and that lines are split only by line-terminators and not also commas. If there's agreement around this proposal, the second step will be to apply the printer to all graphql examples in the spec. --- src/language/__tests__/printer.js | 8 ++++---- src/language/printer.js | 9 +++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/language/__tests__/printer.js b/src/language/__tests__/printer.js index a1779edaf0..a522de014c 100644 --- a/src/language/__tests__/printer.js +++ b/src/language/__tests__/printer.js @@ -49,12 +49,12 @@ describe('Printer', () => { expect(printed).to.equal( `query queryName($foo: ComplexType, $site: Site = MOBILE) { whoever123is: node(id: [123, 456]) { - id, + id ... on User @defer { field2 { - id, + id alias: field1(first: 10, after: $foo) @include(if: $foo) { - id, + id ...frag } } @@ -75,7 +75,7 @@ fragment frag on Friend { } { - unnamed(truthy: true, falsey: false), + unnamed(truthy: true, falsey: false) query } `); diff --git a/src/language/printer.js b/src/language/printer.js index b5f7a21da4..bff2787e10 100644 --- a/src/language/printer.js +++ b/src/language/printer.js @@ -33,7 +33,9 @@ export function print(ast) { }, VariableDefinition: node => join([node.variable + ': ' + node.type, node.defaultValue], ' = '), - SelectionSet: node => blockList(node.selections, ',\n'), + SelectionSet: node => + length(node.selections) === 0 ? null : + indent('{\n' + join(node.selections, '\n')) + '\n}', Field: node => join([ join([ @@ -94,11 +96,6 @@ export function print(ast) { }); } -function blockList(list, separator) { - return length(list) === 0 ? null : - indent('{\n' + join(list, separator)) + '\n}'; -} - function indent(maybeString) { return maybeString && maybeString.replace(/\n/g, '\n '); }