Skip to content

Commit

Permalink
fix(schema-parser): handle union types in input fields by defaulting …
Browse files Browse the repository at this point in the history
…to the first type

This also fixes a issue where the inputs were not properly generated
when they were optional and an array.
  • Loading branch information
schettn committed Dec 4, 2024
1 parent 36f381b commit 58a58e8
Showing 1 changed file with 10 additions and 43 deletions.
53 changes: 10 additions & 43 deletions packages/pylon-builder/src/schema/schema-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,51 +803,18 @@ export class SchemaParser {
])
})
} else {
let properties = getPublicPropertiesOfType(
this.checker,
type.getNonNullableType()
)

if (properties.length === 0) {
//get first union type of non nullable type
const nt = type.getNonNullableType()
if (nt.isUnion()) {
properties = getPublicPropertiesOfType(this.checker, nt.types[0]!)
}
}

if (properties.length > 0) {
if (!referenceSchema[processing].has(type)) {
referenceSchema[processing].set(type, {})
}
const firstType = type.types[0]

// Go through all properties of the union type and add them to the reference schema
properties.forEach(property => {
const propertyType = this.checker.getTypeOfSymbolAtLocation(
property,
this.sfiFile
)
consola.warn(
`Warning: Union types in input fields are not supported yet. Defaulting to the first type (${this.checker.typeToString(
firstType
)}) at path: ${path.join(' > ')}`
)

if (!isFunction(propertyType)) {
referenceSchema[processing].get(type)![
property.escapedName as string
] = {
returnType: propertyType,
args: {}
}

recLoop(
propertyType,
{
propetyName: property.escapedName as string,
parentType: type
},
processing,
[...path, property.escapedName as string]
)
}
})
}
recLoop(firstType.getNonNullableType(), info, processing, [
...path,
'NON_NULLABLE'
])
}
} else if (isFunction(type)) {
// skip fn for inputs
Expand Down

1 comment on commit 58a58e8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for pylon-docs ready!

✅ Preview
https://pylon-docs-8eno98bd3-schettns-projects.vercel.app

Built with commit 58a58e8.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.