Skip to content

Commit f3dcbb3

Browse files
author
James Baxley
authored
Merge pull request #2830 from apollographql/jackson/preserve-federation-descriptions
Preserve docstrings in SDL of federated services.
2 parents 7d5c8f4 + d1e2162 commit f3dcbb3

2 files changed

Lines changed: 68 additions & 24 deletions

File tree

packages/apollo-federation/src/service/__tests__/buildFederatedSchema.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,61 @@ type Money {
7373
`);
7474
});
7575

76+
it('should preserve description text in generated SDL', async () => {
77+
const query = `query GetServiceDetails {
78+
_service {
79+
sdl
80+
}
81+
}`;
82+
const schema = buildFederatedSchema(gql`
83+
"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"
84+
type User @key(fields: "id") {
85+
"""
86+
The unique ID of the user.
87+
"""
88+
id: ID!
89+
"The user's name."
90+
name: String
91+
username: String
92+
foo(
93+
"Description 1"
94+
arg1: String
95+
"Description 2"
96+
arg2: String
97+
"Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3 Description 3"
98+
arg3: String
99+
): String
100+
}
101+
`);
102+
103+
const { data, errors } = await graphql(schema, query);
104+
expect(errors).toBeUndefined();
105+
expect(data._service.sdl).toEqual(`"""
106+
A user. This user is very complicated and requires so so so so so so so so so so
107+
so so so so so so so so so so so so so so so so so so so so so so much
108+
description text
109+
"""
110+
type User @key(fields: "id") {
111+
"The unique ID of the user."
112+
id: ID!
113+
"The user's name."
114+
name: String
115+
username: String
116+
foo(
117+
"Description 1"
118+
arg1: String
119+
"Description 2"
120+
arg2: String
121+
"""
122+
Description 3 Description 3 Description 3 Description 3 Description 3
123+
Description 3 Description 3 Description 3 Description 3 Description 3 Description 3
124+
"""
125+
arg3: String
126+
): String
127+
}
128+
`);
129+
});
130+
76131
describe(`should add an _entities query root field to the schema`, () => {
77132
it(`when a query root type with the default name has been defined`, () => {
78133
const schema = buildFederatedSchema(gql`

packages/apollo-federation/src/service/printFederatedSchema.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ function printEnum(type: GraphQLEnumType): string {
220220
const values = type
221221
.getValues()
222222
.map(
223-
(value, i) =>
224-
printDescription(value, ' ', !i) +
223+
value =>
224+
printDescription(value, ' ') +
225225
' ' +
226226
value.name +
227227
printDeprecated(value),
@@ -232,7 +232,7 @@ function printEnum(type: GraphQLEnumType): string {
232232

233233
function printInputObject(type: GraphQLInputObjectType): string {
234234
const fields = Object.values(type.getFields()).map(
235-
(f, i) => printDescription(f, ' ', !i) + ' ' + printInputValue(f),
235+
f => printDescription(f, ' ') + ' ' + printInputValue(f),
236236
);
237237
return printDescription(type) + `input ${type.name}` + printBlock(fields);
238238
}
@@ -241,8 +241,8 @@ function printFields(
241241
type: GraphQLInterfaceType | GraphQLObjectType | GraphQLInputObjectType,
242242
) {
243243
const fields = Object.values(type.getFields()).map(
244-
(f, i) =>
245-
printDescription(f, ' ', !i) +
244+
f =>
245+
printDescription(f, ' ') +
246246
' ' +
247247
f.name +
248248
printArgs(f.args, ' ') +
@@ -272,8 +272,8 @@ function printArgs(args: GraphQLArgument[], indentation = '') {
272272
'(\n' +
273273
args
274274
.map(
275-
(arg, i) =>
276-
printDescription(arg, ' ' + indentation, !i) +
275+
arg =>
276+
printDescription(arg, ' ' + indentation) +
277277
' ' +
278278
indentation +
279279
printInputValue(arg),
@@ -332,30 +332,19 @@ function printDescription(
332332
| GraphQLEnumValue
333333
| GraphQLUnionType,
334334
indentation: string = '',
335-
firstInBlock: boolean = true,
336335
): string {
337336
if (!def.description) {
338337
return '';
339338
}
340339

341340
const lines = descriptionLines(def.description, 120 - indentation.length);
342-
return printDescriptionWithComments(lines, indentation, firstInBlock);
343-
}
344-
345-
function printDescriptionWithComments(
346-
lines: string[],
347-
indentation: string,
348-
firstInBlock: boolean,
349-
) {
350-
let description = indentation && !firstInBlock ? '\n' : '';
351-
for (let i = 0; i < lines.length; i++) {
352-
if (lines[i] === '') {
353-
description += indentation + '#\n';
354-
} else {
355-
description += indentation + '# ' + lines[i] + '\n';
356-
}
341+
if (lines.length === 1) {
342+
return indentation + `"${lines[0]}"\n`;
343+
} else {
344+
return (
345+
indentation + ['"""', ...lines, '"""'].join('\n' + indentation) + '\n'
346+
);
357347
}
358-
return description;
359348
}
360349

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

0 commit comments

Comments
 (0)