@@ -11,20 +11,8 @@ import (
11
11
"k8s.io/apimachinery/pkg/runtime/schema"
12
12
)
13
13
14
- // RelationEnhancer handles schema enhancement for relation fields
15
- type RelationEnhancer struct {
16
- gateway * Gateway
17
- }
18
-
19
- // NewRelationEnhancer creates a new relation enhancer
20
- func NewRelationEnhancer (gateway * Gateway ) * RelationEnhancer {
21
- return & RelationEnhancer {
22
- gateway : gateway ,
23
- }
24
- }
25
-
26
- // AddRelationFields adds relation fields to schemas that contain *Ref fields
27
- func (re * RelationEnhancer ) AddRelationFields (fields graphql.Fields , properties map [string ]spec.Schema ) {
14
+ // addRelationFields adds relation fields to schemas that contain *Ref fields
15
+ func (g * Gateway ) addRelationFields (fields graphql.Fields , properties map [string ]spec.Schema ) {
28
16
for fieldName := range properties {
29
17
if ! strings .HasSuffix (fieldName , "Ref" ) {
30
18
continue
@@ -38,7 +26,7 @@ func (re *RelationEnhancer) AddRelationFields(fields graphql.Fields, properties
38
26
continue
39
27
}
40
28
41
- enhancedType := re .enhanceRefTypeWithRelation (refField .Type , baseName )
29
+ enhancedType := g .enhanceRefTypeWithRelation (refField .Type , baseName )
42
30
if enhancedType == nil {
43
31
continue
44
32
}
@@ -50,31 +38,31 @@ func (re *RelationEnhancer) AddRelationFields(fields graphql.Fields, properties
50
38
}
51
39
52
40
// enhanceRefTypeWithRelation adds a relation field to a *Ref object type
53
- func (re * RelationEnhancer ) enhanceRefTypeWithRelation (originalType graphql.Output , baseName string ) graphql.Output {
41
+ func (g * Gateway ) enhanceRefTypeWithRelation (originalType graphql.Output , baseName string ) graphql.Output {
54
42
objType , ok := originalType .(* graphql.Object )
55
43
if ! ok {
56
44
return originalType
57
45
}
58
46
59
47
cacheKey := objType .Name () + "_" + baseName + "_Enhanced"
60
- if enhancedType , exists := re . gateway .enhancedTypesCache [cacheKey ]; exists {
48
+ if enhancedType , exists := g .enhancedTypesCache [cacheKey ]; exists {
61
49
return enhancedType
62
50
}
63
51
64
- enhancedFields := re .copyOriginalFields (objType .Fields ())
65
- re .addRelationField (enhancedFields , baseName )
52
+ enhancedFields := g .copyOriginalFields (objType .Fields ())
53
+ g .addRelationField (enhancedFields , baseName )
66
54
67
55
enhancedType := graphql .NewObject (graphql.ObjectConfig {
68
56
Name : sanitizeFieldName (cacheKey ),
69
57
Fields : enhancedFields ,
70
58
})
71
59
72
- re . gateway .enhancedTypesCache [cacheKey ] = enhancedType
60
+ g .enhancedTypesCache [cacheKey ] = enhancedType
73
61
return enhancedType
74
62
}
75
63
76
64
// copyOriginalFields converts FieldDefinition to Field for reuse
77
- func (re * RelationEnhancer ) copyOriginalFields (originalFieldDefs graphql.FieldDefinitionMap ) graphql.Fields {
65
+ func (g * Gateway ) copyOriginalFields (originalFieldDefs graphql.FieldDefinitionMap ) graphql.Fields {
78
66
enhancedFields := make (graphql.Fields , len (originalFieldDefs ))
79
67
for fieldName , fieldDef := range originalFieldDefs {
80
68
enhancedFields [fieldName ] = & graphql.Field {
@@ -87,39 +75,39 @@ func (re *RelationEnhancer) copyOriginalFields(originalFieldDefs graphql.FieldDe
87
75
}
88
76
89
77
// addRelationField adds a single relation field to the enhanced fields
90
- func (re * RelationEnhancer ) addRelationField (enhancedFields graphql.Fields , baseName string ) {
91
- targetType , targetGVK , ok := re .findRelationTarget (baseName )
78
+ func (g * Gateway ) addRelationField (enhancedFields graphql.Fields , baseName string ) {
79
+ targetType , targetGVK , ok := g .findRelationTarget (baseName )
92
80
if ! ok {
93
81
return
94
82
}
95
83
96
84
sanitizedBaseName := sanitizeFieldName (baseName )
97
85
enhancedFields [sanitizedBaseName ] = & graphql.Field {
98
86
Type : targetType ,
99
- Resolve : re . gateway .resolver .RelationResolver (baseName , * targetGVK ),
87
+ Resolve : g .resolver .RelationResolver (baseName , * targetGVK ),
100
88
}
101
89
}
102
90
103
91
// findRelationTarget locates the GraphQL output type and its GVK for a relation target
104
- func (re * RelationEnhancer ) findRelationTarget (baseName string ) (graphql.Output , * schema.GroupVersionKind , bool ) {
92
+ func (g * Gateway ) findRelationTarget (baseName string ) (graphql.Output , * schema.GroupVersionKind , bool ) {
105
93
targetKind := cases .Title (language .English ).String (baseName )
106
94
107
- for defKey , defSchema := range re . gateway .definitions {
108
- if re .matchesTargetKind (defSchema , targetKind ) {
95
+ for defKey , defSchema := range g .definitions {
96
+ if g .matchesTargetKind (defSchema , targetKind ) {
109
97
// Resolve or build the GraphQL type
110
98
var fieldType graphql.Output
111
- if existingType , exists := re . gateway .typesCache [defKey ]; exists {
99
+ if existingType , exists := g .typesCache [defKey ]; exists {
112
100
fieldType = existingType
113
101
} else {
114
- ft , _ , err := re . gateway .convertSwaggerTypeToGraphQL (defSchema , defKey , []string {}, make (map [string ]bool ))
102
+ ft , _ , err := g .convertSwaggerTypeToGraphQL (defSchema , defKey , []string {}, make (map [string ]bool ))
115
103
if err != nil {
116
104
continue
117
105
}
118
106
fieldType = ft
119
107
}
120
108
121
109
// Extract GVK from the schema definition
122
- gvk , err := re . gateway .getGroupVersionKind (defKey )
110
+ gvk , err := g .getGroupVersionKind (defKey )
123
111
if err != nil || gvk == nil {
124
112
continue
125
113
}
@@ -132,7 +120,7 @@ func (re *RelationEnhancer) findRelationTarget(baseName string) (graphql.Output,
132
120
}
133
121
134
122
// matchesTargetKind checks if a schema definition matches the target kind
135
- func (re * RelationEnhancer ) matchesTargetKind (defSchema spec.Schema , targetKind string ) bool {
123
+ func (g * Gateway ) matchesTargetKind (defSchema spec.Schema , targetKind string ) bool {
136
124
gvkExt , ok := defSchema .Extensions ["x-kubernetes-group-version-kind" ]
137
125
if ! ok {
138
126
return false
0 commit comments