Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit 0787ae7

Browse files
committed
removed circular dep from gateway
On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent de3c7a5 commit 0787ae7

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

gateway/schema/relations.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,8 @@ import (
1111
"k8s.io/apimachinery/pkg/runtime/schema"
1212
)
1313

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) {
2816
for fieldName := range properties {
2917
if !strings.HasSuffix(fieldName, "Ref") {
3018
continue
@@ -38,7 +26,7 @@ func (re *RelationEnhancer) AddRelationFields(fields graphql.Fields, properties
3826
continue
3927
}
4028

41-
enhancedType := re.enhanceRefTypeWithRelation(refField.Type, baseName)
29+
enhancedType := g.enhanceRefTypeWithRelation(refField.Type, baseName)
4230
if enhancedType == nil {
4331
continue
4432
}
@@ -50,31 +38,31 @@ func (re *RelationEnhancer) AddRelationFields(fields graphql.Fields, properties
5038
}
5139

5240
// 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 {
5442
objType, ok := originalType.(*graphql.Object)
5543
if !ok {
5644
return originalType
5745
}
5846

5947
cacheKey := objType.Name() + "_" + baseName + "_Enhanced"
60-
if enhancedType, exists := re.gateway.enhancedTypesCache[cacheKey]; exists {
48+
if enhancedType, exists := g.enhancedTypesCache[cacheKey]; exists {
6149
return enhancedType
6250
}
6351

64-
enhancedFields := re.copyOriginalFields(objType.Fields())
65-
re.addRelationField(enhancedFields, baseName)
52+
enhancedFields := g.copyOriginalFields(objType.Fields())
53+
g.addRelationField(enhancedFields, baseName)
6654

6755
enhancedType := graphql.NewObject(graphql.ObjectConfig{
6856
Name: sanitizeFieldName(cacheKey),
6957
Fields: enhancedFields,
7058
})
7159

72-
re.gateway.enhancedTypesCache[cacheKey] = enhancedType
60+
g.enhancedTypesCache[cacheKey] = enhancedType
7361
return enhancedType
7462
}
7563

7664
// 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 {
7866
enhancedFields := make(graphql.Fields, len(originalFieldDefs))
7967
for fieldName, fieldDef := range originalFieldDefs {
8068
enhancedFields[fieldName] = &graphql.Field{
@@ -87,39 +75,39 @@ func (re *RelationEnhancer) copyOriginalFields(originalFieldDefs graphql.FieldDe
8775
}
8876

8977
// 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)
9280
if !ok {
9381
return
9482
}
9583

9684
sanitizedBaseName := sanitizeFieldName(baseName)
9785
enhancedFields[sanitizedBaseName] = &graphql.Field{
9886
Type: targetType,
99-
Resolve: re.gateway.resolver.RelationResolver(baseName, *targetGVK),
87+
Resolve: g.resolver.RelationResolver(baseName, *targetGVK),
10088
}
10189
}
10290

10391
// 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) {
10593
targetKind := cases.Title(language.English).String(baseName)
10694

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) {
10997
// Resolve or build the GraphQL type
11098
var fieldType graphql.Output
111-
if existingType, exists := re.gateway.typesCache[defKey]; exists {
99+
if existingType, exists := g.typesCache[defKey]; exists {
112100
fieldType = existingType
113101
} 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))
115103
if err != nil {
116104
continue
117105
}
118106
fieldType = ft
119107
}
120108

121109
// Extract GVK from the schema definition
122-
gvk, err := re.gateway.getGroupVersionKind(defKey)
110+
gvk, err := g.getGroupVersionKind(defKey)
123111
if err != nil || gvk == nil {
124112
continue
125113
}
@@ -132,7 +120,7 @@ func (re *RelationEnhancer) findRelationTarget(baseName string) (graphql.Output,
132120
}
133121

134122
// 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 {
136124
gvkExt, ok := defSchema.Extensions["x-kubernetes-group-version-kind"]
137125
if !ok {
138126
return false

gateway/schema/schema.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type Gateway struct {
2929
typesCache map[string]*graphql.Object
3030
inputTypesCache map[string]*graphql.InputObject
3131
enhancedTypesCache map[string]*graphql.Object // Cache for enhanced *Ref types
32-
relationEnhancer *RelationEnhancer
3332
// Prevents naming conflict in case of the same Kind name in different groups/versions
3433
typeNameRegistry map[string]string // map[Kind]GroupVersion
3534

@@ -49,9 +48,6 @@ func New(log *logger.Logger, definitions spec.Definitions, resolverProvider reso
4948
typeByCategory: make(map[string][]resolver.TypeByCategory),
5049
}
5150

52-
// Initialize the relation enhancer after gateway is created
53-
g.relationEnhancer = NewRelationEnhancer(g)
54-
5551
err := g.generateGraphqlSchema()
5652

5753
return g, err
@@ -339,7 +335,7 @@ func (g *Gateway) generateGraphQLFields(resourceScheme *spec.Schema, typePrefix
339335
}
340336

341337
// Add relation fields for any *Ref fields in this schema
342-
g.relationEnhancer.AddRelationFields(fields, resourceScheme.Properties)
338+
g.addRelationFields(fields, resourceScheme.Properties)
343339

344340
return fields, inputFields, nil
345341
}

0 commit comments

Comments
 (0)