@@ -156,6 +156,9 @@ func (g *Generator) Run() error {
156156 g .applyAutoGeneration (depGraph )
157157 }
158158
159+ // Build scanned types registry with GQL annotation metadata
160+ g .buildScannedTypesRegistry ()
161+
159162 // Check if we have any namespaces defined
160163 hasNamespaces := len (g .P .TypeNamespaces ) > 0 || len (g .P .EnumNamespaces ) > 0
161164
@@ -295,13 +298,17 @@ func (g *Generator) buildDependencyOrder() []string {
295298 return
296299 }
297300 visited [n ] = true
298- st := g .P .Structs [n ]
299- if st == nil {
301+ typeSpec := g .P .StructTypes [n ]
302+ st , ok := typeSpec .Type .(* ast.StructType )
303+ if ! ok {
300304 return
301305 }
302306 for _ , f := range st .Fields .List {
303307 ft := FieldTypeName (f .Type )
304- if _ , ok := g .P .Structs [ft ]; ok {
308+ if ts , ok := g .P .StructTypes [ft ]; ok {
309+ if _ , ok := ts .Type .(* ast.StructType ); ! ok {
310+ continue
311+ }
305312 dfs (ft )
306313 }
307314 }
@@ -322,7 +329,7 @@ func (g *Generator) generateTypeContent(typeName string, typeSpec *ast.TypeSpec,
322329 if d .HasTypeDirective {
323330 slog .Debug ("Generating type from directive" , "type" , typeName , "count" , len (d .Types ))
324331 for _ , typeDef := range d .Types {
325- typeContent := g .generateTypeFromDef (typeSpec , g . P . Structs [ typeName ] , d , typeDef , ctx )
332+ typeContent := g .generateTypeFromDef (typeSpec , typeSpec . Type .( * ast. StructType ) , d , typeDef , ctx )
326333 if typeContent != "" {
327334 buf .WriteString (typeContent )
328335 }
@@ -334,7 +341,7 @@ func (g *Generator) generateTypeContent(typeName string, typeSpec *ast.TypeSpec,
334341 Name : typeName ,
335342 Description : "" ,
336343 }
337- typeContent := g .generateTypeFromDef (typeSpec , g . P . Structs [ typeName ] , d , defaultTypeDef , ctx )
344+ typeContent := g .generateTypeFromDef (typeSpec , typeSpec . Type .( * ast. StructType ) , d , defaultTypeDef , ctx )
338345 if typeContent != "" {
339346 buf .WriteString (typeContent )
340347 }
@@ -352,7 +359,7 @@ func (g *Generator) generateInputContent(typeName string, typeSpec *ast.TypeSpec
352359 if d .HasInputDirective {
353360 slog .Debug ("Generating input from directive" , "type" , typeName , "count" , len (d .Inputs ))
354361 for _ , inputDef := range d .Inputs {
355- inputContent := g .generateInputFromDef (typeSpec , g . P . Structs [ typeName ] , d , inputDef , ctx )
362+ inputContent := g .generateInputFromDef (typeSpec , typeSpec . Type .( * ast. StructType ) , d , inputDef , ctx )
356363 if inputContent != "" {
357364 buf .WriteString (inputContent )
358365 }
@@ -364,7 +371,7 @@ func (g *Generator) generateInputContent(typeName string, typeSpec *ast.TypeSpec
364371 Name : typeName + "Input" ,
365372 Description : "" ,
366373 }
367- inputContent := g .generateInputFromDef (typeSpec , g . P . Structs [ typeName ] , d , defaultInputDef , ctx )
374+ inputContent := g .generateInputFromDef (typeSpec , typeSpec . Type .( * ast. StructType ) , d , defaultInputDef , ctx )
368375 if inputContent != "" {
369376 buf .WriteString (inputContent )
370377 }
@@ -534,7 +541,7 @@ func (g *Generator) generateByNamespace(orders []string) (map[string]string, err
534541 for _ , typeName := range typeNames {
535542 typeDefs := items .types [typeName ]
536543 typeSpec := g .P .StructTypes [typeName ]
537- structType := g . P . Structs [ typeName ]
544+ structType := typeSpec . Type .( * ast. StructType )
538545 d := ParseDirectives (typeSpec , g .P .TypeToDecl [typeName ])
539546
540547 for _ , typeDef := range typeDefs {
@@ -543,13 +550,11 @@ func (g *Generator) generateByNamespace(orders []string) (map[string]string, err
543550 buf .WriteString (typeContent )
544551 }
545552 }
546- }
547-
548- // Generate inputs for this namespace
553+ } // Generate inputs for this namespace
549554 slog .Debug ("Generating inputs for namespace" , "namespace" , namespace , "count" , len (items .inputs ))
550555 for typeName , inputDefs := range items .inputs {
551556 typeSpec := g .P .StructTypes [typeName ]
552- structType := g . P . Structs [ typeName ]
557+ structType := typeSpec . Type .( * ast. StructType )
553558 d := ParseDirectives (typeSpec , g .P .TypeToDecl [typeName ])
554559
555560 for _ , inputDef := range inputDefs {
@@ -718,7 +723,7 @@ func (g *Generator) generateByNamespaceAndPackage(orders []string) (map[string]s
718723 Strategy : "namespace+package" ,
719724 Namespace : ns ,
720725 }
721- typeContent := g .generateTypeFromDef (typeSpec , g . P . Structs [ typeName ] , d , typeDef , ctx )
726+ typeContent := g .generateTypeFromDef (typeSpec , typeSpec . Type .( * ast. StructType ) , d , typeDef , ctx )
722727 if typeContent != "" {
723728 buf .WriteString (typeContent )
724729 }
@@ -760,7 +765,7 @@ func (g *Generator) generateByNamespaceAndPackage(orders []string) (map[string]s
760765 Strategy : "namespace+package" ,
761766 Namespace : ns ,
762767 }
763- inputContent := g .generateInputFromDef (typeSpec , g . P . Structs [ typeName ] , d , inputDef , ctx )
768+ inputContent := g .generateInputFromDef (typeSpec , typeSpec . Type .( * ast. StructType ) , d , inputDef , ctx )
764769 if inputContent != "" {
765770 buf .WriteString (inputContent )
766771 }
@@ -1006,7 +1011,7 @@ func (g *Generator) generatePackageFiles(orders []string) (map[string]string, er
10061011 for _ , typeName := range typeNames {
10071012 typeDefs := items .types [typeName ]
10081013 typeSpec := g .P .StructTypes [typeName ]
1009- structType := g . P . Structs [ typeName ]
1014+ structType := typeSpec . Type .( * ast. StructType )
10101015 d := ParseDirectives (typeSpec , g .P .TypeToDecl [typeName ])
10111016
10121017 for _ , typeDef := range typeDefs {
@@ -1015,13 +1020,11 @@ func (g *Generator) generatePackageFiles(orders []string) (map[string]string, er
10151020 buf .WriteString (typeContent )
10161021 }
10171022 }
1018- }
1019-
1020- // Generate inputs for this package
1023+ } // Generate inputs for this package
10211024 slog .Debug ("Generating inputs for package" , "package" , pkgPath , "count" , len (items .inputs ))
10221025 for typeName , inputDefs := range items .inputs {
10231026 typeSpec := g .P .StructTypes [typeName ]
1024- structType := g . P . Structs [ typeName ]
1027+ structType := typeSpec . Type .( * ast. StructType )
10251028 d := ParseDirectives (typeSpec , g .P .TypeToDecl [typeName ])
10261029
10271030 for _ , inputDef := range inputDefs {
@@ -1673,15 +1676,18 @@ func (g *Generator) expandEmbeddedFieldNamed(f *ast.Field, d StructDirectives, i
16731676 }
16741677
16751678 // Look up the embedded struct in the parser
1676- embeddedStruct , exists := g .P .Structs [embeddedTypeName ]
1679+ typeSpec , exists := g .P .StructTypes [embeddedTypeName ]
16771680 if ! exists {
16781681 return "" // Embedded struct not found in parsed types
16791682 }
1683+ embeddedStruct , ok := typeSpec .Type .(* ast.StructType )
1684+ if ! ok {
1685+ return "" // Not a struct type
1686+ }
16801687
16811688 // If generating for input and the embedded type should be auto-generated as input,
16821689 // mark it for generation
16831690 if forInput {
1684- typeSpec := g .P .StructTypes [embeddedTypeName ]
16851691 if typeSpec != nil {
16861692 genDecl := g .P .TypeToDecl [embeddedTypeName ]
16871693 directives := ParseDirectives (typeSpec , genDecl )
@@ -1994,6 +2000,43 @@ func (g *Generator) extractBaseTypeName(graphQLType string) string {
19942000}
19952001
19962002// isTypeInScope checks if a type name exists in the parsed types (structs or enums)
2003+ // buildScannedTypesRegistry populates the ScannedTypes registry with metadata about all scanned types
2004+ func (g * Generator ) buildScannedTypesRegistry () {
2005+ for typeName , typeSpec := range g .P .StructTypes {
2006+ if genDecl , hasDecl := g .P .TypeToDecl [typeName ]; hasDecl {
2007+ directives := ParseDirectives (typeSpec , genDecl )
2008+
2009+ info := & ScannedTypeInfo {
2010+ TypeName : typeName ,
2011+ HasTypeDirective : directives .HasTypeDirective ,
2012+ HasInputDirective : directives .HasInputDirective ,
2013+ GeneratedTypes : make ([]string , 0 ),
2014+ GeneratedInputs : make ([]string , 0 ),
2015+ }
2016+
2017+ // Collect generated type names from @gqlType annotations
2018+ for _ , typeDef := range directives .Types {
2019+ if typeDef .Name != "" {
2020+ info .GeneratedTypes = append (info .GeneratedTypes , typeDef .Name )
2021+ } else {
2022+ info .GeneratedTypes = append (info .GeneratedTypes , typeName )
2023+ }
2024+ }
2025+
2026+ // Collect generated input names from @gqlInput annotations
2027+ for _ , inputDef := range directives .Inputs {
2028+ if inputDef .Name != "" {
2029+ info .GeneratedInputs = append (info .GeneratedInputs , inputDef .Name )
2030+ } else {
2031+ info .GeneratedInputs = append (info .GeneratedInputs , typeName + "Input" )
2032+ }
2033+ }
2034+
2035+ g .P .ScannedTypes [typeName ] = info
2036+ }
2037+ }
2038+ }
2039+
19972040func (g * Generator ) isTypeInScope (typeName string ) bool {
19982041 if typeName == "" {
19992042 return true
@@ -2004,7 +2047,23 @@ func (g *Generator) isTypeInScope(typeName string) bool {
20042047 return true
20052048 }
20062049
2007- // Check if it's a struct type
2050+ // Check if this type name was generated from a scanned type with GQL annotations
2051+ for _ , scannedInfo := range g .P .ScannedTypes {
2052+ // Check if typeName matches any generated type names
2053+ for _ , genType := range scannedInfo .GeneratedTypes {
2054+ if genType == typeName {
2055+ return true
2056+ }
2057+ }
2058+ // Check if typeName matches any generated input names
2059+ for _ , genInput := range scannedInfo .GeneratedInputs {
2060+ if genInput == typeName {
2061+ return true
2062+ }
2063+ }
2064+ }
2065+
2066+ // Check if it's a struct type (scanned but no annotations)
20082067 if _ , exists := g .P .StructTypes [typeName ]; exists {
20092068 return true
20102069 }
0 commit comments