Skip to content

Commit c1c30bb

Browse files
feat: Graph to SDL conversion (#2)
1 parent 61d8610 commit c1c30bb

File tree

5 files changed

+69
-214
lines changed

5 files changed

+69
-214
lines changed

internal/cli/cli_pick.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ func fnPick(cmd *cobra.Command, args []string) {
3131
// The most crucial step is to map out the AST.
3232
// The AST object itself is clunky and nested.
3333
// We need something flatter and more ergonomic: like a couple of maps!
34-
g := buildSchemaGraph(astDoc)
34+
g := buildPrunedGraph(astDoc)
3535

3636
// Build a visual diagram of the new SDL
3737
file, _ := os.Create("./simple.gv")
3838
_ = draw.DOT(g, file)
3939

40-
// TODO convert the GRAPH to an SDL
40+
// Convert the GRAPH back to an SDL
41+
newSDL := convertGraphToSDL(g)
42+
43+
// Print the new SDL to file
44+
printSDL(newSDL)
4145
}

internal/cli/graph.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func VertexHash(v Vertex) string {
5252
//
5353
// This will provide constant-time access to a map of type names to their
5454
// definitions, as well as definitions to their dependencies (other types).
55-
func buildSchemaGraph(doc *ast.Document) graph.Graph[string, Vertex] {
55+
func buildPrunedGraph(doc *ast.Document) graph.Graph[string, Vertex] {
5656
g := graph.New(VertexHash)
5757

5858
// Load the important definitions:
@@ -102,7 +102,7 @@ func buildEdges(g graph.Graph[string, Vertex], doc *ast.Document) {
102102
}
103103

104104
switch v.Node.GetKind() {
105-
// TODO support input objects, input values, interfaces, and unions
105+
// TODO support input objects, input values, interfaces, and unions... otherwise we're missing things like AircraftsBoolExp
106106
case kinds.ObjectDefinition:
107107
obj := v.Node.(*ast.ObjectDefinition)
108108
fields := obj.Fields

internal/cli/graph_to_sdl.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cli
2+
3+
import (
4+
"github.com/charmbracelet/log"
5+
"github.com/dominikbraun/graph"
6+
"github.com/graphql-go/graphql/language/ast"
7+
"github.com/graphql-go/graphql/language/kinds"
8+
)
9+
10+
func convertGraphToSDL(g graph.Graph[string, Vertex]) *ast.Document {
11+
out := &ast.Document{
12+
Kind: kinds.Document,
13+
Loc: nil,
14+
Definitions: nil,
15+
}
16+
17+
var defs []ast.Node
18+
19+
adj, err := g.AdjacencyMap()
20+
if err != nil {
21+
log.Fatal("unable to retrieve AdjacencyMap", "err", err)
22+
}
23+
24+
for defName := range adj {
25+
def := must(g.Vertex(defName))
26+
defs = append(defs, def.Node)
27+
}
28+
29+
out.Definitions = defs
30+
31+
return out
32+
}

internal/cli/print.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cli
2+
3+
import (
4+
"bufio"
5+
"github.com/charmbracelet/log"
6+
"github.com/graphql-go/graphql/language/ast"
7+
"github.com/graphql-go/graphql/language/printer"
8+
"os"
9+
)
10+
11+
func printSDL(doc *ast.Document) {
12+
sdl := printer.Print(doc)
13+
14+
// Create a new file where we'll write the new SDL
15+
// TODO make configurable
16+
f, err := os.Create("output.sdl.graphqls")
17+
if err != nil {
18+
log.Fatal("unable to create new SDL file for writing", "err", err)
19+
}
20+
21+
defer f.Close()
22+
23+
w := bufio.NewWriter(f)
24+
25+
_, err = w.WriteString(sdl.(string))
26+
if err != nil {
27+
log.Fatal("unable to produce new SDL file", "err", err)
28+
}
29+
}

output.sdl.graphqls

Lines changed: 0 additions & 210 deletions
This file was deleted.

0 commit comments

Comments
 (0)