1- import ts from "typescript" ;
2- import { DataType , Flow , FunctionDefinition , NodeFunction , NodeParameter } from "@code0-tech/sagittarius-graphql-types" ;
3- import { createCompilerHost , getParameterCode , getSharedTypeDeclarations , } from "../utils" ;
4- import { getNodeValidation } from "../validation/getNodeValidation" ; // Wieder hinzugefügt
1+ import { DataType , Flow , FunctionDefinition , NodeFunction } from "@code0-tech/sagittarius-graphql-types" ;
2+ import { getInferredTypesFromFlow , sanitizeId } from "../utils" ;
53
64export interface NodeTypes {
75 parameters : string [ ] ;
@@ -16,73 +14,39 @@ export const getTypesFromNode = (
1614 functions ?: FunctionDefinition [ ] ,
1715 dataTypes ?: DataType [ ]
1816) : NodeTypes => {
19- const funcMap = new Map ( functions ?. map ( f => [ f . identifier , f ] ) ) ;
20- const funcDef = funcMap . get ( node ?. functionDefinition ?. identifier ) ;
21-
22- if ( ! funcDef ) {
23- return {
24- parameters : [ ] ,
25- returnType : "any" ,
26- } ;
27- }
17+ if ( ! node ) return { parameters : [ ] , returnType : "any" } ;
18+
19+ const nodeId = node . id || "temp_node_id" ;
20+
21+ // To ensure generics and triangulated types are resolved without hardcoding,
22+ // we must ensure the compiler has enough context.
23+ // If some parameters are missing values, the compiler might return 'any'.
24+ // We create a version of the node where missing values are filled with a marker
25+ // that the TypeScript engine can use to infer the intended type from the signature.
26+
27+ const nodeWithDefaults = {
28+ ...node ,
29+ id : nodeId ,
30+ parameters : {
31+ ...node . parameters ,
32+ nodes : node . parameters ?. nodes ?. map ( p => {
33+ if ( p ?. value ) return p ;
34+ // If value is missing, we still want the compiler to see the argument position
35+ return { ...p , value : { __typename : "LiteralValue" , value : null } } ;
36+ } ) || [ ]
37+ }
38+ } ;
2839
2940 const mockFlow : Flow = {
3041 id : "gid://sagittarius/Flow/0" as any ,
31- nodes : { __typename : "NodeFunctionConnection" , nodes : [ node ] }
42+ nodes : { __typename : "NodeFunctionConnection" , nodes : [ nodeWithDefaults ] }
3243 } as Flow ;
3344
34- const params = ( node ?. parameters ?. nodes as NodeParameter [ ] ) || [ ] ;
35- const paramCodes = params . map ( param => getParameterCode ( param , mockFlow , ( f , n ) => getNodeValidation ( f , n , functions , dataTypes ) ) ) ;
36-
37- const funcCallArgs = paramCodes . map ( code => code === 'undefined' ? '({} as any)' : code ) . join ( ", " ) ;
38-
39- const signature = funcDef . signature ;
40- const sourceCode = `
41- ${ getSharedTypeDeclarations ( dataTypes ) }
42- declare function testFunc${ signature } ;
43- const result = testFunc(${ funcCallArgs } );
44- ` ;
45-
46- const fileName = "index.ts" ;
47- const host = createCompilerHost ( fileName , sourceCode ) ;
48- const sourceFile = host . getSourceFile ( fileName ) ! ;
49- const program = host . languageService . getProgram ( ) ! ;
50- const checker = program . getTypeChecker ( ) ;
51-
52- let inferredReturnType = "any" ;
53- let inferredParameterTypes : string [ ] = [ ] ;
54-
55- const visitor = ( n : ts . Node ) => {
56- if ( ts . isVariableDeclaration ( n ) && n . name . getText ( ) === "result" ) {
57- const resultType = checker . getTypeAtLocation ( n ) ;
58- inferredReturnType = checker . typeToString (
59- resultType ,
60- n ,
61- ts . TypeFormatFlags . NoTruncation
62- ) ;
63-
64- if ( ts . isCallExpression ( n . initializer ! ) ) {
65- const callExpr = n . initializer ;
66- const signature = checker . getResolvedSignature ( callExpr ) ;
67- if ( signature ) {
68- inferredParameterTypes = signature . getParameters ( ) . map ( p => {
69- const type = checker . getTypeOfSymbolAtLocation ( p , callExpr ) ;
70- return checker . typeToString (
71- type ,
72- callExpr ,
73- ts . TypeFormatFlags . NoTruncation
74- ) ;
75- } ) ;
76- }
77- }
78- }
79- ts . forEachChild ( n , visitor ) ;
80- } ;
81-
82- visitor ( sourceFile ) ;
45+ const inferred = getInferredTypesFromFlow ( mockFlow , functions , dataTypes ) ;
46+ const sId = sanitizeId ( nodeId ) ;
8347
8448 return {
85- parameters : inferredParameterTypes ,
86- returnType : inferredReturnType ,
49+ parameters : inferred . parameters . get ( sId ) || [ ] ,
50+ returnType : inferred . nodes . get ( sId ) || "any" ,
8751 } ;
8852} ;
0 commit comments