Skip to content

Commit f704ad1

Browse files
committed
review comments
Signed-off-by: grokspawn <jordan@nimblewidget.com>
1 parent 374e69e commit f704ad1

3 files changed

Lines changed: 67 additions & 15 deletions

File tree

internal/catalogd/graphql/graphql.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ type CatalogSchema struct {
4949

5050
// serializableFieldInfo is a JSON-friendly representation of FieldInfo
5151
type serializableFieldInfo struct {
52-
Name string `json:"name"`
53-
OriginalName string `json:"originalName"`
54-
JSONType string `json:"jsonType"`
55-
IsArray bool `json:"isArray"`
56-
NestedFields map[string]*serializableFieldInfo `json:"nestedFields,omitempty"`
52+
Name string `json:"name"`
53+
OriginalName string `json:"originalName"`
54+
JSONType string `json:"jsonType"`
55+
IsArray bool `json:"isArray"`
56+
GraphQLTypeName string `json:"graphqlTypeName,omitempty"`
57+
NestedFields map[string]*serializableFieldInfo `json:"nestedFields,omitempty"`
5758
}
5859

5960
// serializableSchemaInfo is a JSON-friendly representation of SchemaInfo
@@ -101,12 +102,38 @@ func stringToKind(s string) reflect.Kind {
101102
}
102103
}
103104

105+
func graphqlTypeName(t graphql.Type) string {
106+
if list, ok := t.(*graphql.List); ok {
107+
return graphqlTypeName(list.OfType)
108+
}
109+
return t.Name()
110+
}
111+
112+
func graphqlTypeFromName(name string, isArray bool) graphql.Type {
113+
var base graphql.Type
114+
switch name {
115+
case "Int":
116+
base = graphql.Int
117+
case "Float":
118+
base = graphql.Float
119+
case "Boolean":
120+
base = graphql.Boolean
121+
default:
122+
base = graphql.String
123+
}
124+
if isArray {
125+
return graphql.NewList(base)
126+
}
127+
return base
128+
}
129+
104130
func fieldInfoToSerializable(fi *FieldInfo) *serializableFieldInfo {
105131
sfi := &serializableFieldInfo{
106-
Name: fi.Name,
107-
OriginalName: fi.OriginalName,
108-
JSONType: kindToString(fi.JSONType),
109-
IsArray: fi.IsArray,
132+
Name: fi.Name,
133+
OriginalName: fi.OriginalName,
134+
JSONType: kindToString(fi.JSONType),
135+
IsArray: fi.IsArray,
136+
GraphQLTypeName: graphqlTypeName(fi.GraphQLType),
110137
}
111138
if len(fi.NestedFields) > 0 {
112139
sfi.NestedFields = make(map[string]*serializableFieldInfo)
@@ -119,12 +146,18 @@ func fieldInfoToSerializable(fi *FieldInfo) *serializableFieldInfo {
119146

120147
func serializableToFieldInfo(sfi *serializableFieldInfo) *FieldInfo {
121148
k := stringToKind(sfi.JSONType)
149+
var gqlType graphql.Type
150+
if sfi.GraphQLTypeName != "" {
151+
gqlType = graphqlTypeFromName(sfi.GraphQLTypeName, sfi.IsArray)
152+
} else {
153+
gqlType = jsonTypeToGraphQL(k, sfi.IsArray, nil)
154+
}
122155
fi := &FieldInfo{
123156
Name: sfi.Name,
124157
OriginalName: sfi.OriginalName,
125158
JSONType: k,
126159
IsArray: sfi.IsArray,
127-
GraphQLType: jsonTypeToGraphQL(k, sfi.IsArray, nil),
160+
GraphQLType: gqlType,
128161
}
129162
if len(sfi.NestedFields) > 0 {
130163
fi.NestedFields = make(map[string]*FieldInfo)

internal/catalogd/graphql/validation.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ const (
1414
)
1515

1616
type queryComplexity struct {
17-
aliases int
18-
fields int
17+
aliases int
18+
fields int
19+
fragments map[string]*ast.FragmentDefinition
20+
visited map[string]bool
1921
}
2022

2123
// ValidateQueryComplexity parses the query AST and rejects it if it exceeds
@@ -26,7 +28,15 @@ func ValidateQueryComplexity(query string) error {
2628
return fmt.Errorf("query parse error: %w", err)
2729
}
2830

29-
c := &queryComplexity{}
31+
c := &queryComplexity{
32+
fragments: make(map[string]*ast.FragmentDefinition),
33+
visited: make(map[string]bool),
34+
}
35+
for _, def := range doc.Definitions {
36+
if frag, ok := def.(*ast.FragmentDefinition); ok {
37+
c.fragments[frag.Name.Value] = frag
38+
}
39+
}
3040
for _, def := range doc.Definitions {
3141
if op, ok := def.(*ast.OperationDefinition); ok {
3242
if err := c.walkSelectionSet(op.SelectionSet, 1); err != nil {
@@ -66,7 +76,16 @@ func (c *queryComplexity) walkSelectionSet(ss *ast.SelectionSet, depth int) erro
6676
return err
6777
}
6878
case *ast.FragmentSpread:
69-
c.fields++
79+
name := s.Name.Value
80+
if c.visited[name] {
81+
continue
82+
}
83+
c.visited[name] = true
84+
if frag, ok := c.fragments[name]; ok {
85+
if err := c.walkSelectionSet(frag.SelectionSet, depth+1); err != nil {
86+
return err
87+
}
88+
}
7089
}
7190
}
7291
return nil

internal/catalogd/storage/localdir.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (s *LocalDirV1) Store(ctx context.Context, catalog string, fsys fs.FS) erro
9292
if s.graphqlSvc != nil {
9393
s.graphqlSvc.InvalidateCache(catalog)
9494

95-
if _, err := s.graphqlSvc.GetSchema(context.Background(), catalog); err != nil {
95+
if _, err := s.graphqlSvc.GetSchema(ctx, catalog); err != nil {
9696
// Schema build failed — remove the catalog to maintain consistency.
9797
// Re-acquire the write lock for the rollback since it touches shared filesystem state.
9898
s.m.Lock()

0 commit comments

Comments
 (0)