Skip to content

Commit eecb848

Browse files
Merge pull request #12 from bmeg/feature/jsonschema-to-graphql
[WIP] Feature/jsonschema to graphql
2 parents 5545ec0 + f3418be commit eecb848

File tree

9 files changed

+429
-16
lines changed

9 files changed

+429
-16
lines changed

cmd/gen_dir/main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
var extraArgs string
16-
var gzip_files bool
15+
var extraArgs string = ""
16+
var gzip_files bool = false
1717

1818
// https://github.com/bmeg/sifter/blob/51a67b0de852e429d30b9371d9975dbefe3a8df9/transform/graph_build.go#L86
1919
var Cmd = &cobra.Command{
@@ -31,10 +31,12 @@ var Cmd = &cobra.Command{
3131
}
3232

3333
var mapstringArgs map[string]any
34-
err = json.Unmarshal([]byte(extraArgs), &mapstringArgs)
35-
if err != nil {
36-
log.Fatal("Error unmarshaling JSON:", err)
37-
return nil
34+
if extraArgs != "" {
35+
err = json.Unmarshal([]byte(extraArgs), &mapstringArgs)
36+
if err != nil {
37+
log.Fatal("Error unmarshaling JSON:", err)
38+
return nil
39+
}
3840
}
3941

4042
if out, err = graph.Load(args[0]); err != nil {

cmd/gen_graphql/main.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package gen_graphql
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
8+
schema "github.com/bmeg/jsonschemagraph/graphql"
9+
"github.com/spf13/cobra"
10+
"gopkg.in/yaml.v3"
11+
)
12+
13+
var jsonSchemaFile string
14+
var yamlSchemaDir string
15+
var graphName string
16+
var configPath string
17+
var writeIntermediateFile bool = false
18+
19+
type Config struct {
20+
DependencyOrder []string `yaml:"dependency_order"`
21+
}
22+
23+
var Cmd = &cobra.Command{
24+
Use: "gen-graphql",
25+
Short: "Load graph schemas",
26+
Long: ``,
27+
Args: cobra.NoArgs,
28+
RunE: func(cmd *cobra.Command, args []string) error {
29+
if jsonSchemaFile == "" && yamlSchemaDir == "" {
30+
return fmt.Errorf("No schema file was provided")
31+
}
32+
33+
config := Config{DependencyOrder: []string{}}
34+
if configPath != "" {
35+
data, err := ioutil.ReadFile(configPath)
36+
if err != nil {
37+
log.Fatalf("Failed to read YAML file: %v", err)
38+
}
39+
err = yaml.Unmarshal(data, &config)
40+
if err != nil {
41+
log.Fatalf("Failed to parse YAML file: %v", err)
42+
}
43+
} else {
44+
fmt.Printf("Warning: No config file was provided, all vertices will be rendered has queries")
45+
}
46+
47+
if jsonSchemaFile != "" && graphName != "" {
48+
log.Printf("Loading Json Schema file: %s", jsonSchemaFile)
49+
graphs, err := schema.ParseGraphFile(jsonSchemaFile, "jsonSchema", graphName, config.DependencyOrder, writeIntermediateFile)
50+
if err != nil {
51+
return err
52+
}
53+
_ = schema.GripGraphqltoGraphql(graphs[0])
54+
}
55+
if yamlSchemaDir != "" && graphName != "" {
56+
log.Printf("Loading Yaml Schema dir: %s", yamlSchemaDir)
57+
graphs, err := schema.ParseGraphFile(yamlSchemaDir, "yamlSchema", graphName, config.DependencyOrder, writeIntermediateFile)
58+
if err != nil {
59+
return err
60+
}
61+
_ = schema.GripGraphqltoGraphql(graphs[0])
62+
}
63+
64+
return nil
65+
},
66+
}
67+
68+
func init() {
69+
gqlflags := Cmd.Flags()
70+
gqlflags.BoolVar(&writeIntermediateFile, "writeIntermediateFile", false, "Write writeIntermediateFile file to disk")
71+
gqlflags.StringVar(&jsonSchemaFile, "jsonSchema", "", "Json Schema")
72+
gqlflags.StringVar(&yamlSchemaDir, "yamlSchemaDir", "", "Name of YAML schemas dir")
73+
gqlflags.StringVar(&configPath, "configPath", "", "Path of Config file for determining the subset of ")
74+
gqlflags.StringVar(&graphName, "graphName", "", "Name of schemaGraph")
75+
}

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"github.com/bmeg/jsonschemagraph/cmd/data_validate"
77
"github.com/bmeg/jsonschemagraph/cmd/gen_dir"
88
"github.com/bmeg/jsonschemagraph/cmd/gen_graph"
9+
"github.com/bmeg/jsonschemagraph/cmd/gen_graphql"
910
"github.com/bmeg/jsonschemagraph/cmd/schema_graph"
1011
"github.com/bmeg/jsonschemagraph/cmd/schema_lint"
12+
1113
"github.com/spf13/cobra"
1214
)
1315

@@ -24,6 +26,7 @@ func init() {
2426
RootCmd.AddCommand(schema_lint.Cmd)
2527
RootCmd.AddCommand(schema_graph.Cmd)
2628
RootCmd.AddCommand(data_validate.Cmd)
29+
RootCmd.AddCommand(gen_graphql.Cmd)
2730

2831
}
2932

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ go 1.22.5
44

55
require (
66
github.com/bmeg/golib v0.0.0-20200725232156-e799a31439fc
7+
github.com/bmeg/grip v0.0.0-20231102165002-fa720cf43d53
8+
github.com/bmeg/jsonschema/v5 v5.3.4-0.20241111204732-55db82022a92
79
github.com/google/uuid v1.6.0
810
github.com/spf13/cobra v1.8.1
11+
google.golang.org/appengine v1.6.7
912
google.golang.org/protobuf v1.34.2
1013
sigs.k8s.io/yaml v1.4.0
1114
)
1215

1316
require (
1417
github.com/akuity/grpc-gateway-client v0.0.0-20230321170839-38ca1b4b439c // indirect
1518
github.com/alevinval/sse v1.0.1 // indirect
16-
github.com/bmeg/grip v0.0.0-20231102165002-fa720cf43d53 // indirect
17-
github.com/bmeg/jsonschema/v5 v5.3.4-0.20241111204732-55db82022a92 // indirect
1819
github.com/go-resty/resty/v2 v2.7.0 // indirect
1920
github.com/golang/protobuf v1.5.4 // indirect
2021
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
@@ -32,4 +33,5 @@ require (
3233
golang.org/x/text v0.7.0 // indirect
3334
google.golang.org/genproto v0.0.0-20230303212802-e74f57abe488 // indirect
3435
google.golang.org/grpc v1.53.0 // indirect
36+
gopkg.in/yaml.v3 v3.0.1 // indirect
3537
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1616
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1717
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
1818
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
19+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1920
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
2021
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
2122
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
@@ -49,11 +50,14 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
4950
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
5051
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5152
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
53+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5254
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
5355
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
56+
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
5457
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
5558
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
5659
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
60+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5761
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5862
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5963
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -62,10 +66,14 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6266
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
6367
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
6468
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
69+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
70+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
6571
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
6672
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
6773
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
6874
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
75+
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
76+
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
6977
google.golang.org/genproto v0.0.0-20230303212802-e74f57abe488 h1:QQF+HdiI4iocoxUjjpLgvTYDHKm99C/VtTBFnfiCJos=
7078
google.golang.org/genproto v0.0.0-20230303212802-e74f57abe488/go.mod h1:TvhZT5f700eVlTNwND1xoEZQeWTB2RY/65kplwl/bFA=
7179
google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc=
@@ -75,6 +83,7 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
7583
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
7684
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7785
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
86+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7887
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7988
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
8089
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=

graph/generate.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func (s GraphSchema) Generate(classID string, data map[string]any, clean bool, e
5858
namespaceDNS = nms
5959
delete(extraArgs, "namespace")
6060
}
61-
log.Println("Using namespace ", namespaceDNS)
6261
namespace := uuid.NewMD5(uuid.NameSpaceDNS, []byte(namespaceDNS))
6362
if class := s.GetClass(classID); class != nil {
6463
if clean {
@@ -75,16 +74,13 @@ func (s GraphSchema) Generate(classID string, data map[string]any, clean bool, e
7574
}
7675
out := make([]gripql.GraphElement, 0, 1)
7776
if id, nerr := util.GetObjectID(data, class); nerr == nil {
78-
var ListOfRels []string
7977
vData := map[string]any{}
8078
if ext, ok := class.Extensions[compile.GraphExtensionTag]; ok {
8179
gext := ext.(compile.GraphExtension)
8280
for _, target := range gext.Targets {
83-
ListOfRels = append(ListOfRels, target.Rel)
8481
if target.TemplatePointers.Id == "" {
8582
continue
8683
}
87-
//log.Println(" TARGET TEMPLATE POINTER ID: ", target.TemplatePointers.Id )
8884
splitted_pointer := strings.Split(target.TemplatePointers.Id, "/")[1:]
8985
items, err := resolveItem(splitted_pointer, data)
9086
// if pointer miss continue
@@ -93,11 +89,12 @@ func (s GraphSchema) Generate(classID string, data map[string]any, clean bool, e
9389
}
9490
// if invalid pointer structure in data, error
9591
if err != nil {
96-
log.Fatal("ERROR: ", err)
92+
log.Fatal("Resolve item Error: ", err)
9793
}
9894
for _, elem := range items {
9995
split_list := strings.Split(elem.(string), "/")
100-
if target.TargetHints.RegexMatch != nil && target.TargetHints.RegexMatch[0] == (split_list[0]+"/*") {
96+
regex_match := target.TargetHints.RegexMatch[0]
97+
if target.TargetHints.RegexMatch != nil && (regex_match == (split_list[0]+"/*") || regex_match == "Resource/*") {
10198
elem := split_list[1]
10299
edgeOut := gripql.Edge{
103100
To: elem,
@@ -131,14 +128,14 @@ func (s GraphSchema) Generate(classID string, data map[string]any, clean bool, e
131128
}
132129
dataPB, err := structpb.NewStruct(vData)
133130
if err != nil {
134-
log.Println("ERROR: ", err)
131+
log.Printf("Error when creating structpb with data: %#v: %s\n", err)
135132
return nil, err
136133
}
137134
vert := gripql.Vertex{Gid: id, Label: classID, Data: dataPB}
138135
out = append(out, gripql.GraphElement{Vertex: &vert})
139136

140137
} else if nerr != nil {
141-
log.Println("ERROR: ", nerr)
138+
log.Println("Error: ", nerr)
142139
return nil, nerr
143140
}
144141
return out, nil

0 commit comments

Comments
 (0)