Skip to content
Merged
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
58 changes: 58 additions & 0 deletions src/language/__tests__/printer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,64 @@ describe('Printer: Query document', () => {
`);
});

it('puts large object values on multiple lines if line is long (> 80 chars)', () => {
const printed = print(
parse(
'{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}}',
),
);

expect(printed).to.equal(dedent`
{
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
}
}
`);
});

it('puts large list values on multiple lines if line is long (> 80 chars)', () => {
const printed = print(
parse(
'{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}}',
),
);

expect(printed).to.equal(dedent`
{
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
}
}
`);
});

it('Legacy: prints fragment with variable directives', () => {
const queryASTWithVariableDirective = parse(
'fragment Foo($foo: TestType @test) on TestType @testDirective { id }',
Expand Down
18 changes: 16 additions & 2 deletions src/language/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,22 @@ const printDocASTReducer: ASTReducer<string> = {
BooleanValue: { leave: ({ value }) => (value ? 'true' : 'false') },
NullValue: { leave: () => 'null' },
EnumValue: { leave: ({ value }) => value },
ListValue: { leave: ({ values }) => '[' + join(values, ', ') + ']' },
ObjectValue: { leave: ({ fields }) => '{ ' + join(fields, ', ') + ' }' },
ListValue: {
leave: ({ values }) => {
const valuesLine = '[' + join(values, ', ') + ']';

if (valuesLine.length > MAX_LINE_LENGTH) {
return '[\n' + indent(join(values, '\n')) + '\n]';
}
return valuesLine;
},
},
ObjectValue: {
leave: ({ fields }) => {
const fieldsLine = '{ ' + join(fields, ', ') + ' }';
return fieldsLine.length > MAX_LINE_LENGTH ? block(fields) : fieldsLine;
},
},
ObjectField: { leave: ({ name, value }) => name + ': ' + value },

// Directive
Expand Down