Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/util/schema.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,20 @@ export const getSchema = (
functions: FunctionDefinition[],
suggestions: boolean = true
): Schema => {

if ((parameterType.flags & ts.TypeFlags.TypeParameter) !== 0) {
const decl = parameterType.symbol?.declarations?.[0]
if (decl && ts.isTypeParameterDeclaration(decl) && decl.constraint) {
// getTypeFromTypeNode statt getBaseConstraintOfType → aliasSymbol bleibt erhalten
const constraintType = checker.getTypeFromTypeNode(decl.constraint)
return getSchema(checker, node, constraintType, functionDeclarations, functions, suggestions)
}
}

// Collect all available suggestions for this parameter
const combinedSuggestions = suggestions ? {
suggestions: [
...getValues(parameterType),
...getValues(parameterType, checker),
...(node ? getReferences(
checker,
node,
Expand All @@ -151,6 +161,19 @@ export const getSchema = (
],
} : {};

// Strip undefined from unions (e.g. string | undefined → string).
// Suggestions are collected above from the original type (preserving aliasSymbol literals),
// the base schema is determined from the stripped type, then both are merged.
if (parameterType.isUnion()) {
const nonUndefined = parameterType.types.filter(
(t) => (t.flags & ts.TypeFlags.Undefined) === 0
)
if (nonUndefined.length === 1) {
const baseSchema = getSchema(checker, node, nonUndefined[0], functionDeclarations, functions, false)
return {...baseSchema, ...combinedSuggestions}
}
}

// Check individual primitive types
if (isBoolean(parameterType)) {
return {input: "boolean", ...combinedSuggestions};
Expand Down
26 changes: 24 additions & 2 deletions src/util/values.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,32 @@ import { LiteralValue } from "@code0-tech/sagittarius-graphql-types";
* // { value: "42" }
* // ]
*/
export const getValues = (type: ts.Type): LiteralValue[] => {
const getLiteralsFromTypeNode = (node: ts.TypeNode): LiteralValue[] => {
if (ts.isUnionTypeNode(node)) return node.types.flatMap(getLiteralsFromTypeNode)
if (ts.isLiteralTypeNode(node)) {
const lit = node.literal
if (ts.isStringLiteral(lit)) return [{value: lit.text, __typename: "LiteralValue"}]
if (ts.isNumericLiteral(lit)) return [{value: Number(lit.text)}]
if (lit.kind === ts.SyntaxKind.TrueKeyword) return [{value: true, __typename: "LiteralValue"}]
if (lit.kind === ts.SyntaxKind.FalseKeyword) return [{value: false, __typename: "LiteralValue"}]
}
return []
}

export const getValues = (type: ts.Type, checker?: ts.TypeChecker): LiteralValue[] => {
// When a type comes from an alias (e.g. HTTP_AUTH_TYPE), TypeScript simplifies
// 'Bearer' | string → string, losing the literals. Read them directly from the
// alias declaration's AST instead, which preserves the original union members.
if (checker && type.aliasSymbol) {
const decl = type.aliasSymbol.declarations?.[0]
if (decl && ts.isTypeAliasDeclaration(decl)) {
return getLiteralsFromTypeNode(decl.type)
}
}

// Handle union types by recursively extracting values from each constituent type
if (type.isUnion()) {
return type.types.flatMap(getValues);
return type.types.flatMap(t => getValues(t, checker));
}

// Extract string literal values
Expand Down
Loading