Skip to content

Commit

Permalink
fixup graphql schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpeterkort committed Dec 11, 2024
1 parent a1646b0 commit 146c50e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10
github.com/bmeg/jsonpath v0.0.0-20210207014051-cca5355553ad
github.com/bmeg/jsonschema/v5 v5.3.4-0.20241111204732-55db82022a92
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210002603-05a78a1c9530
github.com/bmeg/jsonschemagraph v0.0.3-0.20241211000114-7493b10aa2fe
github.com/boltdb/bolt v1.3.1
github.com/casbin/casbin/v2 v2.97.0
github.com/cockroachdb/pebble v1.1.1
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ github.com/bmeg/jsonschemagraph v0.0.3-0.20241209230926-fb19ad7b2e2a h1:DStPPpLl
github.com/bmeg/jsonschemagraph v0.0.3-0.20241209230926-fb19ad7b2e2a/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210002603-05a78a1c9530 h1:2TwiDqHDaL2Myb791dxjaE7qSVDmSXjOkEyIXlphR38=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210002603-05a78a1c9530/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210174749-d2daa11e3817 h1:B1cMiitclYSieW8KI+/ZQUyDS03IV8k4MJ+GdknDKX4=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210174749-d2daa11e3817/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210191325-71585cfb3524 h1:6aOb8PLGaJ+kOifiCCt6USDRt7WKWDDD43hsIBrOnJc=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210191325-71585cfb3524/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210212543-70f212509f5d h1:DDRf+MFZv/vdwHhfOnLDFR/3dIEWcPobb7+w9A+m97I=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210212543-70f212509f5d/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210220235-ad08cccc9c18 h1:q5za50YXfqxXdChJL3IgdqsJY7d1zXYuK1kNmraTPi4=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210220235-ad08cccc9c18/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210221630-a93c8860b606 h1:Oe+89mmgQSD1D5yi4dOXZwloLsV84RU68ueAGtr2n2w=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210221630-a93c8860b606/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210221941-3e919ec517cc h1:L2KBzHO3R370dB9mdyPSQJU67/VC/Vz5+L4Z8w3C5rw=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241210221941-3e919ec517cc/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241211000114-7493b10aa2fe h1:emW8sMq9MWg5aisapvXtA12MBthjnGdJ6nGnm12eoDo=
github.com/bmeg/jsonschemagraph v0.0.3-0.20241211000114-7493b10aa2fe/go.mod h1:uVS5AJySmP/VVuZF5Ux/tVyPWNi9IWP5ZRf3il70F7k=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
Expand Down
49 changes: 40 additions & 9 deletions schema/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,58 @@ import (
"fmt"
"os"
"strings"
"unicode"

"github.com/bmeg/grip/gripql"
)

func IsUpper(s string) bool {
for _, r := range s {
if !unicode.IsUpper(r) && unicode.IsLetter(r) {
return false
}
}
return true
}

func GripGraphqltoGraphql(graph *gripql.Graph, writefile bool) string {
var schemaBuilder strings.Builder
// Write gen3 style boiler plate to mirror thier args
schemaBuilder.WriteString("scalar JSON\n")
schemaBuilder.WriteString("enum Accessibility {\n all\n accessible\n unaccessible\n}\n")
schemaBuilder.WriteString("enum Format {\n json\n tsv\n csv\n}\n")

for _, v := range graph.Vertices {
if strings.HasSuffix(v.Gid, "Type") {
if v.Gid != "Query" {
executedFirstBlock := false
for name, values := range v.Data.AsMap() {
schemaBuilder.WriteString(fmt.Sprintf("enum %s {\n", name))
for _, value := range values.([]any) {
schemaBuilder.WriteString(fmt.Sprintf(" %s\n", value))
listVals, ok := values.([]any)
if ok && IsUpper(listVals[0].(string)) {
executedFirstBlock = true
schemaBuilder.WriteString(fmt.Sprintf("enum %s {\n", name))
for _, value := range listVals {
schemaBuilder.WriteString(fmt.Sprintf(" %s\n", value))
}
schemaBuilder.WriteString("}\n")
} else {
break
}
schemaBuilder.WriteString("}\n\n")
}
if !executedFirstBlock {
schemaBuilder.WriteString(fmt.Sprintf("type %s {\n", v.Gid))
for field, fieldType := range v.Data.AsMap() {
schemaBuilder.WriteString(fmt.Sprintf(" %s: %s\n", field, fieldType))
}
schemaBuilder.WriteString("}\n")
}
} else {
schemaBuilder.WriteString(fmt.Sprintf("type %s {\n", v.Gid))
for field, fieldType := range v.Data.AsMap() {
schemaBuilder.WriteString(fmt.Sprintf(" %s: %s\n", field, fieldType))
for name, values := range v.Data.AsMap() {
schemaBuilder.WriteString(fmt.Sprintf("type %s {\n", name))
for _, value := range values.([]any) {
schemaBuilder.WriteString(fmt.Sprintf(" %s\n", value))
}
schemaBuilder.WriteString("}\n")
}
schemaBuilder.WriteString("}\n")
}
}
if writefile {
Expand Down

0 comments on commit 146c50e

Please sign in to comment.