11package cli
22
33import (
4- "errors"
54 "github.com/charmbracelet/log"
65 "github.com/dominikbraun/graph"
76 "github.com/graphql-go/graphql/language/ast"
87 "github.com/graphql-go/graphql/language/kinds"
8+ "strings"
99)
1010
1111type Vertex struct {
1212 Name string
1313 Node ast.Node
1414}
1515
16+ type Describer interface {
17+ GetDescription () * ast.StringValue
18+ }
19+
20+ func sanitizeComment (d Describer ) string {
21+ desc := d .GetDescription ()
22+ if desc == nil {
23+ return ""
24+ }
25+
26+ return strings .ReplaceAll (desc .Value , `"` , `'` )
27+ }
28+
1629func NewVertex (node ast.Node ) Vertex {
1730 var name string
1831 switch node .GetKind () {
1932 case kinds .ScalarDefinition :
2033 obj := node .(* ast.ScalarDefinition )
2134 name = obj .GetName ().Value
35+
36+ // Sanitize description (e.g., remove double-quotes)
37+ if obj .Description != nil {
38+ obj .Description = & ast.StringValue {
39+ Kind : kinds .StringValue ,
40+ Value : sanitizeComment (obj ),
41+ }
42+ }
2243 case kinds .InterfaceDefinition :
2344 obj := node .(* ast.InterfaceDefinition )
2445 name = obj .GetName ().Value
46+
47+ // Sanitize description (e.g., remove double-quotes)
48+ if obj .Description != nil {
49+ obj .Description = & ast.StringValue {
50+ Kind : kinds .StringValue ,
51+ Value : sanitizeComment (obj ),
52+ }
53+ }
2554 case kinds .UnionDefinition :
2655 obj := node .(* ast.UnionDefinition )
2756 name = obj .GetName ().Value
57+
58+ // Sanitize description (e.g., remove double-quotes)
59+ if obj .Description != nil {
60+ obj .Description = & ast.StringValue {
61+ Kind : kinds .StringValue ,
62+ Value : sanitizeComment (obj ),
63+ }
64+ }
2865 case kinds .EnumDefinition :
2966 obj := node .(* ast.EnumDefinition )
3067 name = obj .GetName ().Value
68+
69+ // Sanitize description (e.g., remove double-quotes)
70+ if obj .Description != nil {
71+ obj .Description = & ast.StringValue {
72+ Kind : kinds .StringValue ,
73+ Value : sanitizeComment (obj ),
74+ }
75+ }
3176 case kinds .InputObjectDefinition :
3277 obj := node .(* ast.InputObjectDefinition )
3378 name = obj .GetName ().Value
79+
80+ // Sanitize description (e.g., remove double-quotes)
81+ if obj .Description != nil {
82+ obj .Description = & ast.StringValue {
83+ Kind : kinds .StringValue ,
84+ Value : sanitizeComment (obj ),
85+ }
86+ }
87+
3488 case kinds .ObjectDefinition :
3589 obj := node .(* ast.ObjectDefinition )
3690 name = obj .GetName ().Value
91+
92+ // Sanitize description (e.g., remove double-quotes)
93+ if obj .Description != nil {
94+ obj .Description = & ast.StringValue {
95+ Kind : kinds .StringValue ,
96+ Value : sanitizeComment (obj ),
97+ }
98+ }
3799 default :
38100 panic ("NewVertex: unsupported node kind: " + node .GetKind ())
39101 }
@@ -60,7 +122,7 @@ func buildPrunedGraph(doc *ast.Document) graph.Graph[string, Vertex] {
60122 loadTopLevelDefinitions (g , doc )
61123
62124 // Build edges between vertices.
63- buildEdges (g , doc )
125+ buildEdges (g )
64126
65127 // Prunes any vertices that don't appear in any edges
66128 return prune (g )
@@ -82,57 +144,15 @@ func loadTopLevelDefinitions(g graph.Graph[string, Vertex], doc *ast.Document) {
82144 case kinds .InputObjectDefinition :
83145 v := NewVertex (d )
84146 _ = g .AddVertex (v )
85- log .Debugf ("Adding vertex for definition %d (%s) -- %s" , i , d .GetKind (), v .Name )
147+ log .Debugf ("Adding vertex for definition %d (%s) -- %s" ,
148+ i , d .GetKind (), v .Name )
86149
87150 default :
88151 log .Warnf ("Ignoring definition %d (%s)" , i , d .GetKind ())
89152 }
90153 }
91154}
92155
93- func buildEdges (g graph.Graph [string , Vertex ], doc * ast.Document ) {
94- for _ , desired := range desiredDefinitions {
95- v , err := g .Vertex (desired )
96- if err != nil {
97- if errors .Is (err , graph .ErrVertexNotFound ) {
98- log .Errorf ("unable to find definition for: %s" , desired )
99- } else {
100- log .Fatal ("unable to read vertex" , "err" , err )
101- }
102- }
103-
104- switch v .Node .GetKind () {
105- // TODO support input objects, input values, interfaces, and unions... otherwise we're missing things like AircraftsBoolExp
106- case kinds .ObjectDefinition :
107- obj := v .Node .(* ast.ObjectDefinition )
108- fields := obj .Fields
109- // TODO iterate through node's fields
110- for _ , f := range fields {
111-
112- // Is the field a primitive scalar (e.g., Int, String)?
113- // If so, we can skip it, as it's natively a part of any
114- // GraphQL schema.
115- rootType := getRootTypeNameHelper (f .Type , 0 )
116- if isBasicType (rootType ) {
117- continue
118- }
119-
120- log .Debug ("Found field in object" ,
121- "object" , obj .Name .Value ,
122- "name" , f .Name .Value ,
123- "type" , rootType ,
124- )
125-
126- _ = g .AddEdge (obj .Name .Value , rootType )
127-
128- // TODO Fields also consist of their arguments, which themselves
129- // may be non-primitive dependencies.
130- //litter.Dump(f.Arguments)
131- }
132- }
133- }
134- }
135-
136156func prune (in graph.Graph [string , Vertex ]) graph.Graph [string , Vertex ] {
137157 m , err := in .AdjacencyMap ()
138158 if err != nil {
0 commit comments