Skip to content

Commit ddd6a01

Browse files
Make print() break long List and Object Values over multiple line (#3860)
Co-authored-by: Ivan Goncharov <ivan.goncharov.ua@gmail.com>
1 parent 35b9d96 commit ddd6a01

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+
const valuesLine = '[' + join(values, ', ') + ']';
154+
155+
if (valuesLine.length > MAX_LINE_LENGTH) {
156+
return '[\n' + indent(join(values, '\n')) + '\n]';
157+
}
158+
return valuesLine;
159+
},
160+
},
161+
ObjectValue: {
162+
leave: ({ fields }) => {
163+
const fieldsLine = '{ ' + join(fields, ', ') + ' }';
164+
return fieldsLine.length > MAX_LINE_LENGTH ? block(fields) : fieldsLine;
165+
},
166+
},
153167
ObjectField: { leave: ({ name, value }) => name + ': ' + value },
154168

155169
// Directive

0 commit comments

Comments
 (0)