Skip to content

Commit 2d2a4ce

Browse files
committed
Make print() break long List and Object Values over multiple line
1 parent e171a14 commit 2d2a4ce

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/language/__tests__/printer-test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,64 @@ describe('Printer: Query document', () => {
110110
`);
111111
});
112112

113+
it('puts large object values on multiple lines if line is long (> 80 chars)', () => {
114+
const printed = print(
115+
parse(
116+
'{trip(obj:{wheelchair:false,smallObj:{a: 1},largeObj:{wheelchair:false,smallObj:{a: 1},arriveBy:false,includePlannedCancellations:true,transitDistanceReluctance:2000,anotherLongFieldName:"Lots and lots and lots and lots of text"},arriveBy:false,includePlannedCancellations:true,transitDistanceReluctance:2000,anotherLongFieldName:"Lots and lots and lots and lots of text"}){dateTime}}',
117+
),
118+
);
119+
120+
expect(printed).to.equal(dedent`
121+
{
122+
trip(
123+
obj: {
124+
wheelchair: false,
125+
smallObj: { a: 1 },
126+
largeObj: {
127+
wheelchair: false,
128+
smallObj: { a: 1 },
129+
arriveBy: false,
130+
includePlannedCancellations: true,
131+
transitDistanceReluctance: 2000,
132+
anotherLongFieldName: "Lots and lots and lots and lots of text"
133+
},
134+
arriveBy: false,
135+
includePlannedCancellations: true,
136+
transitDistanceReluctance: 2000,
137+
anotherLongFieldName: "Lots and lots and lots and lots of text"
138+
}
139+
) {
140+
dateTime
141+
}
142+
}
143+
`);
144+
});
145+
146+
it('puts large list values on multiple lines if line is long (> 80 chars)', () => {
147+
const printed = print(
148+
parse(
149+
'{trip(list:[["small array", "small", "small"], ["Lots and lots and lots and lots of text", "Lots and lots and lots and lots of text", "Lots and lots and lots and lots of text"]]){dateTime}}',
150+
),
151+
);
152+
153+
expect(printed).to.equal(dedent`
154+
{
155+
trip(
156+
list: [
157+
["small array", "small", "small"],
158+
[
159+
"Lots and lots and lots and lots of text",
160+
"Lots and lots and lots and lots of text",
161+
"Lots and lots and lots and lots of text"
162+
]
163+
]
164+
) {
165+
dateTime
166+
}
167+
}
168+
`);
169+
});
170+
113171
it('Legacy: prints fragment with variable directives', () => {
114172
const queryASTWithVariableDirective = parse(
115173
'fragment Foo($foo: TestType @test) on TestType @testDirective { id }',

src/language/printer.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,22 @@ const printDocASTReducer: ASTReducer<string> = {
148148
BooleanValue: { leave: ({ value }) => (value ? 'true' : 'false') },
149149
NullValue: { leave: () => 'null' },
150150
EnumValue: { leave: ({ value }) => value },
151-
ListValue: { leave: ({ values }) => '[' + join(values, ', ') + ']' },
152-
ObjectValue: { leave: ({ fields }) => '{ ' + join(fields, ', ') + ' }' },
151+
ListValue: {
152+
leave: ({ values }) => {
153+
if (values.reduce((t, v) => t + v.length + 2, 0) > MAX_LINE_LENGTH) {
154+
return '[\n' + indent(join(values, ',\n')) + '\n]';
155+
}
156+
return '[' + join(values, ', ') + ']';
157+
},
158+
},
159+
ObjectValue: {
160+
leave: ({ fields }) => {
161+
if (fields.reduce((t, f) => t + f.length + 2, 0) > MAX_LINE_LENGTH) {
162+
return '{\n' + indent(join(fields, ',\n')) + '\n}';
163+
}
164+
return '{ ' + join(fields, ', ') + ' }';
165+
},
166+
},
153167
ObjectField: { leave: ({ name, value }) => name + ': ' + value },
154168

155169
// Directive

0 commit comments

Comments
 (0)