From 708e0f9ce7b32c9d917fde8c9e6bb10adff63da4 Mon Sep 17 00:00:00 2001 From: Nico Schett Date: Thu, 26 Sep 2024 12:19:44 +0200 Subject: [PATCH 1/3] fix: use correct arg description in schema printing --- packages/pylon-builder/src/schema/schema-parser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pylon-builder/src/schema/schema-parser.ts b/packages/pylon-builder/src/schema/schema-parser.ts index 13c666b..294be3e 100644 --- a/packages/pylon-builder/src/schema/schema-parser.ts +++ b/packages/pylon-builder/src/schema/schema-parser.ts @@ -209,7 +209,7 @@ export class SchemaParser { args = `(${field.args .map( arg => - `${addDescription(type.description)}${ + `${addDescription(arg.type.description)}${ arg.name }: ${typeDefinitionToGraphQLType(arg.type)}` ) From 4cb3f98af952aaf6284a46c4d1425c11a2c8d258 Mon Sep 17 00:00:00 2001 From: Nico Schett Date: Thu, 26 Sep 2024 12:22:06 +0200 Subject: [PATCH 2/3] refactor: split get documentation logic into type and symbol function --- .../pylon-builder/src/schema/schema-parser.ts | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/pylon-builder/src/schema/schema-parser.ts b/packages/pylon-builder/src/schema/schema-parser.ts index 294be3e..b9091ce 100644 --- a/packages/pylon-builder/src/schema/schema-parser.ts +++ b/packages/pylon-builder/src/schema/schema-parser.ts @@ -352,33 +352,33 @@ export class SchemaParser { } } - private getJsDocHeaderFromType = (type: ts.Type) => { + private getSymbolDocumentation(symbol: ts.Symbol) { let header = '' - if (type.symbol) { - const typeDeclaration = type.symbol.declarations?.[0] as unknown as - | { - jsDoc: ts.JSDoc[] - } - | undefined + header += ts.displayPartsToString( + symbol.getDocumentationComment(this.checker) + ) - const comments = typeDeclaration?.jsDoc?.map(doc => doc.comment).join(' ') + const tags = symbol + .getJsDocTags(this.checker) + .map(t => `@${t.name} ${ts.displayPartsToString(t.text)}`) + .join('\n') - if (comments) { - header = comments - } + if (tags) { + header += '\n' + tags + } + + return header + } - const tags = type.symbol.getJsDocTags() - const tagComments = tags - ?.map(tag => `@${tag.name} ${tag.text?.map(t => t.text).join(' ')}`) - .join('\n') + private getTypeDocumentation = (type: ts.Type) => { + const symbol = type.getSymbol() - if (tagComments) { - header = `${header} ${tagComments}` - } + if (symbol) { + return this.getSymbolDocumentation(symbol) } - return header + return '' } /** From ece4044723ca4749237aade4ec374abf52808d57 Mon Sep 17 00:00:00 2001 From: Nico Schett Date: Thu, 26 Sep 2024 12:23:08 +0200 Subject: [PATCH 3/3] fix: argument documentation generation This is done via a workaround because there is a issue where the type symbol is not the same as the original symbol from which the type was derived. --- .../pylon-builder/src/schema/schema-parser.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/pylon-builder/src/schema/schema-parser.ts b/packages/pylon-builder/src/schema/schema-parser.ts index b9091ce..ef78166 100644 --- a/packages/pylon-builder/src/schema/schema-parser.ts +++ b/packages/pylon-builder/src/schema/schema-parser.ts @@ -67,7 +67,10 @@ type ReferenceSchemaType = { returnType: ts.Type args: { // value needs to be inputs type - [key: string]: ts.Type + [key: string]: { + type: ts.Type + documentation: string + } } } @@ -142,7 +145,7 @@ export class SchemaParser { .map((union): Union => { return { ...union, - description: this.getJsDocHeaderFromType(union.rawType) + description: this.getTypeDocumentation(union.rawType) } }) @@ -151,7 +154,7 @@ export class SchemaParser { .map((enumType): Enum => { return { ...enumType, - description: this.getJsDocHeaderFromType(enumType.rawType) + description: this.getTypeDocumentation(enumType.rawType) } }) } @@ -284,7 +287,7 @@ export class SchemaParser { } else { this.schema[processing].push({ name, - description: this.getJsDocHeaderFromType(type), + description: this.getTypeDocumentation(type), fields: [] }) @@ -305,7 +308,7 @@ export class SchemaParser { name: propertyName, type: { ...fieldDef, - description: this.getJsDocHeaderFromType(fieldType) + description: this.getTypeDocumentation(fieldType) }, args: [] } @@ -314,22 +317,24 @@ export class SchemaParser { for (const [argName, arg] of Object.entries(property.args)) { const argType = arg - const fieldDef = getTypeDefinition(argType, { + const fieldDef = getTypeDefinition(argType.type, { isInputType: true, propertyName: argName }) if ( - this.schema.scalars.includes(this.checker.typeToString(argType)) + this.schema.scalars.includes( + this.checker.typeToString(argType.type) + ) ) { - fieldDef.name = this.checker.typeToString(argType) + fieldDef.name = this.checker.typeToString(argType.type) } field.args.push({ name: argName, type: { ...fieldDef, - description: this.getJsDocHeaderFromType(argType) + description: argType.documentation } }) } @@ -341,7 +346,7 @@ export class SchemaParser { name: propertyName, type: { ...fieldDef, - description: this.getJsDocHeaderFromType(fieldType) + description: this.getTypeDocumentation(fieldType) } } @@ -577,7 +582,10 @@ export class SchemaParser { // set args to empty object if not set if (schemaType.args) { - schemaType.args[arg.escapedName as string] = argType + schemaType.args[arg.escapedName as string] = { + type: argType, + documentation: this.getSymbolDocumentation(arg) + } recLoop( argType,