Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix printed schema descriptions that end with a double quote (") #1205

Closed
wants to merge 4 commits into from
Closed
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
79 changes: 78 additions & 1 deletion src/utilities/__tests__/schemaPrinter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,60 @@ describe('Type System Printer', () => {
`);
});

it('One-line prints a short description', () => {
const description = 'This field is awesome';
const output = printSingleFieldSchema({
type: GraphQLString,
description,
});
expect(output).to.equal(dedent`
type Query {
"""This field is awesome"""
singleField: String
}
`);
const recreatedRoot = buildSchema(output).getTypeMap()['Query'];
const recreatedField = recreatedRoot.getFields()['singleField'];
expect(recreatedField.description).to.equal(description);
});

it('Does not one-line print a description that ends with a quote', () => {
const description = 'This field is "awesome"';
const output = printSingleFieldSchema({
type: GraphQLString,
description,
});
expect(output).to.equal(dedent`
type Query {
"""
This field is "awesome"
"""
singleField: String
}
`);
const recreatedRoot = buildSchema(output).getTypeMap()['Query'];
const recreatedField = recreatedRoot.getFields()['singleField'];
expect(recreatedField.description).to.equal(description);
});

it('Preserves leading spaces when printing a description', () => {
const description = ' This field is "awesome"';
const output = printSingleFieldSchema({
type: GraphQLString,
description,
});
expect(output).to.equal(dedent`
type Query {
""" This field is "awesome"
"""
singleField: String
}
`);
const recreatedRoot = buildSchema(output).getTypeMap()['Query'];
const recreatedField = recreatedRoot.getFields()['singleField'];
expect(recreatedField.description).to.equal(description);
});

it('Print Introspection Schema', () => {
const Query = new GraphQLObjectType({
name: 'Query',
Expand Down Expand Up @@ -783,7 +837,30 @@ describe('Type System Printer', () => {
`;
expect(output).to.equal(introspectionSchema);
});

it('Prints a field description that ends with a doublequote (")s', () => {
const description = 'This field is "awesome"';
const output = printSingleFieldSchema({
type: GraphQLString,
description,
});
expect(output).to.equal(dedent`
type Query {
"""
This field is "awesome"
"""
singleField: String
}
`);
const recreatedRoot = buildSchema(output).getTypeMap()['Query'];
const recreatedField = recreatedRoot.getFields()['singleField'];

/* Note: Additional space added to prevent parser from confusing trailing double
* quote inside description with the end of the block string. It shouldn't
* affect result since descriptions are interpreted as Markdown so trailing
* spaces are ignored.
*/
expect(recreatedField.description).to.equal(description);
});
it('Print Introspection Schema with comment descriptions', () => {
const Query = new GraphQLObjectType({
name: 'Query',
Expand Down
27 changes: 21 additions & 6 deletions src/utilities/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,30 @@ function printDescription(
return printDescriptionWithComments(lines, indentation, firstInBlock);
}

let description = indentation && !firstInBlock ? '\n' : '';
if (lines.length === 1 && lines[0].length < 70) {
description += indentation + '"""' + escapeQuote(lines[0]) + '"""\n';
return description;
let description =
indentation && !firstInBlock
? '\n' + indentation + '"""'
: indentation + '"""';

// In some circumstances, a single line can be used for the description.
if (
lines.length === 1 &&
lines[0].length < 70 &&
lines[0][lines[0].length - 1] !== '"'
) {
return description + escapeQuote(lines[0]) + '"""\n';
}

description += indentation + '"""\n';
// Format a multi-line block quote to account for leading space.
const hasLeadingSpace = lines[0][0] === ' ' || lines[0][0] === '\t';
if (!hasLeadingSpace) {
description += '\n';
}
for (let i = 0; i < lines.length; i++) {
description += indentation + escapeQuote(lines[i]) + '\n';
if (i !== 0 || !hasLeadingSpace) {
description += indentation;
}
description += escapeQuote(lines[i]) + '\n';
}
description += indentation + '"""\n';
return description;
Expand Down