Skip to content

Commit

Permalink
Merge pull request apollographql/apollo-server#2830 from apollographq…
Browse files Browse the repository at this point in the history
…l/jackson/preserve-federation-descriptions

Preserve docstrings in SDL of federated services.
Apollo-Orig-Commit-AS: apollographql/apollo-server@f3dcbb3
  • Loading branch information
James Baxley authored Jun 12, 2019
2 parents f11c1ed + 5eea33f commit ead50a0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
55 changes: 55 additions & 0 deletions federation-js/src/service/__tests__/buildFederatedSchema.test.ts
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 federation-js/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

0 comments on commit ead50a0

Please sign in to comment.