diff --git a/federation-js/src/service/__tests__/buildFederatedSchema.test.ts b/federation-js/src/service/__tests__/buildFederatedSchema.test.ts index 8580dbfc2..d09f250d9 100644 --- a/federation-js/src/service/__tests__/buildFederatedSchema.test.ts +++ b/federation-js/src/service/__tests__/buildFederatedSchema.test.ts @@ -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` diff --git a/federation-js/src/service/printFederatedSchema.ts b/federation-js/src/service/printFederatedSchema.ts index 71a6656f0..c51128e3f 100644 --- a/federation-js/src/service/printFederatedSchema.ts +++ b/federation-js/src/service/printFederatedSchema.ts @@ -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), @@ -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); } @@ -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, ' ') + @@ -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), @@ -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 {