Skip to content

Commit

Permalink
Merge pull request #24 from getcronit/fix/schema-documentation
Browse files Browse the repository at this point in the history
fix: schema documentation
  • Loading branch information
schettn authored Oct 1, 2024
2 parents 02895a7 + ece4044 commit 8d3bb5f
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions packages/pylon-builder/src/schema/schema-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -142,7 +145,7 @@ export class SchemaParser {
.map((union): Union => {
return {
...union,
description: this.getJsDocHeaderFromType(union.rawType)
description: this.getTypeDocumentation(union.rawType)
}
})

Expand All @@ -151,7 +154,7 @@ export class SchemaParser {
.map((enumType): Enum => {
return {
...enumType,
description: this.getJsDocHeaderFromType(enumType.rawType)
description: this.getTypeDocumentation(enumType.rawType)
}
})
}
Expand Down Expand Up @@ -209,7 +212,7 @@ export class SchemaParser {
args = `(${field.args
.map(
arg =>
`${addDescription(type.description)}${
`${addDescription(arg.type.description)}${
arg.name
}: ${typeDefinitionToGraphQLType(arg.type)}`
)
Expand Down Expand Up @@ -284,7 +287,7 @@ export class SchemaParser {
} else {
this.schema[processing].push({
name,
description: this.getJsDocHeaderFromType(type),
description: this.getTypeDocumentation(type),
fields: []
})

Expand All @@ -305,7 +308,7 @@ export class SchemaParser {
name: propertyName,
type: {
...fieldDef,
description: this.getJsDocHeaderFromType(fieldType)
description: this.getTypeDocumentation(fieldType)
},
args: []
}
Expand All @@ -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
}
})
}
Expand All @@ -341,7 +346,7 @@ export class SchemaParser {
name: propertyName,
type: {
...fieldDef,
description: this.getJsDocHeaderFromType(fieldType)
description: this.getTypeDocumentation(fieldType)
}
}

Expand All @@ -352,33 +357,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
}

const tags = type.symbol.getJsDocTags()
const tagComments = tags
?.map(tag => `@${tag.name} ${tag.text?.map(t => t.text).join(' ')}`)
.join('\n')
return header
}

if (tagComments) {
header = `${header} ${tagComments}`
}
private getTypeDocumentation = (type: ts.Type) => {
const symbol = type.getSymbol()

if (symbol) {
return this.getSymbolDocumentation(symbol)
}

return header
return ''
}

/**
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8d3bb5f

Please sign in to comment.