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

Preserve docstrings in SDL of federated services. #2830

Merged
merged 3 commits into from
Jun 12, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,61 @@ type Money {
`);
});

it('should preserve description text in generated SDL', async () => {
const query = `query GetServiceDetails {
_service {
sdl
}
}`;
const schema = buildFederatedSchema(gql`
"A user. This user is very complicated and requires so so so so so so so so so so so so so so so so so so so so so so so so so so so so so so so so much description text"
type User @key(fields: "id") {
"""
The unique ID of the user.
"""
id: ID!
"The user's name."
name: String
username: String
foo(
"Description 1"
arg1: String
"Description 2"
arg2: String
"Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3"
arg3: String
): String
}
`);

const { data, errors } = await graphql(schema, query);
expect(errors).toBeUndefined();
expect(data._service.sdl).toEqual(`"""
A user. This user is very complicated and requires so so so so so so so so so so
so so so so so so so so so so so so so so so so so so so so so so much
description text
"""
type User @key(fields: "id") {
"The unique ID of the user."
id: ID!
"The user's name."
name: String
username: String
foo(
"Description 1"
arg1: String
"Description 2"
arg2: String
"""
Description 3 Description 3 Description 3 Description 3 Description 3
Description 3 Description 3 Description 3 Description 3 Description 3 Description 3
"""
arg3: String
): String
}
`);
});

describe(`should add an _entities query root field to the schema`, () => {
it(`when a query root type with the default name has been defined`, () => {
const schema = buildFederatedSchema(gql`
Expand Down
37 changes: 13 additions & 24 deletions packages/apollo-federation/src/service/printFederatedSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ function printEnum(type: GraphQLEnumType): string {
const values = type
.getValues()
.map(
(value, i) =>
printDescription(value, ' ', !i) +
value =>
printDescription(value, ' ') +
' ' +
value.name +
printDeprecated(value),
Expand All @@ -232,7 +232,7 @@ function printEnum(type: GraphQLEnumType): string {

function printInputObject(type: GraphQLInputObjectType): string {
const fields = Object.values(type.getFields()).map(
(f, i) => printDescription(f, ' ', !i) + ' ' + printInputValue(f),
f => printDescription(f, ' ') + ' ' + printInputValue(f),
);
return printDescription(type) + `input ${type.name}` + printBlock(fields);
}
Expand All @@ -241,8 +241,8 @@ function printFields(
type: GraphQLInterfaceType | GraphQLObjectType | GraphQLInputObjectType,
) {
const fields = Object.values(type.getFields()).map(
(f, i) =>
printDescription(f, ' ', !i) +
f =>
printDescription(f, ' ') +
' ' +
f.name +
printArgs(f.args, ' ') +
Expand Down Expand Up @@ -272,8 +272,8 @@ function printArgs(args: GraphQLArgument[], indentation = '') {
'(\n' +
args
.map(
(arg, i) =>
printDescription(arg, ' ' + indentation, !i) +
arg =>
printDescription(arg, ' ' + indentation) +
' ' +
indentation +
printInputValue(arg),
Expand Down Expand Up @@ -332,30 +332,19 @@ function printDescription(
| GraphQLEnumValue
| GraphQLUnionType,
indentation: string = '',
firstInBlock: boolean = true,
): string {
if (!def.description) {
return '';
}

const lines = descriptionLines(def.description, 120 - indentation.length);
return printDescriptionWithComments(lines, indentation, firstInBlock);
}

function printDescriptionWithComments(
lines: string[],
indentation: string,
firstInBlock: boolean,
) {
let description = indentation && !firstInBlock ? '\n' : '';
for (let i = 0; i < lines.length; i++) {
if (lines[i] === '') {
description += indentation + '#\n';
} else {
description += indentation + '# ' + lines[i] + '\n';
}
if (lines.length === 1) {
return indentation + `"${lines[0]}"\n`;
} else {
return (
indentation + ['"""', ...lines, '"""'].join('\n' + indentation) + '\n'
);
}
return description;
}

function descriptionLines(description: string, maxLen: number): Array<string> {
Expand Down