-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (108 loc) · 2.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"text/template"
"github.com/Natsuki7777/go-graphql-test-generator/pkg"
)
const (
TARGET_DIR = "input_graphqls"
MAX_DEPTH = 6
GRAPHOUTPUT = "outputs/graphql_test.graphql"
GOOUTPUT = "outputs/graohql_test.go"
)
func main() {
targetDir := TARGET_DIR
max_depth := MAX_DEPTH
graph_output := GRAPHOUTPUT
go_output := GOOUTPUT
files, _ := ioutil.ReadDir(targetDir)
targetString := ""
for _, f := range files {
fileName := f.Name()
if strings.HasSuffix(fileName, "graphql") || strings.HasSuffix(fileName, "graphqls") {
filePath := targetDir + "/" + fileName
file, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println(err)
}
fileString := string(file)
targetString += fileString
}
}
tokens := pkg.TokenizeInput(targetString)
query_map, node_map := pkg.QueryNode(tokens)
for k, q := range query_map {
context, variables := pkg.GenerateContextVariables(q, node_map, 0, max_depth)
q.Context = context
q.Variables = variables
query_map[k] = q
}
type Data struct {
Query []pkg.Query
Mutation []pkg.Query
}
graphql_template, err := template.ParseFiles("template/graphql-template.graphqls")
if err != nil {
fmt.Println(err)
}
graphql_data := Data{}
for _, q := range query_map {
gq := pkg.Query{}
gq.Name = q.Name
gq.Context = q.Context
if q.Variables != "" {
gq.Variables = "# Variables\n#" + strings.Replace(q.Variables, "\n", "\n#", -1)
}
if q.Type == pkg.MUTATION {
graphql_data.Mutation = append(graphql_data.Mutation, gq)
} else if q.Type == pkg.QUERY {
graphql_data.Query = append(graphql_data.Query, gq)
}
}
sort.Slice(graphql_data.Query, func(i, j int) bool {
return graphql_data.Query[i].Name < graphql_data.Query[j].Name
})
sort.Slice(graphql_data.Mutation, func(i, j int) bool {
return graphql_data.Mutation[i].Name < graphql_data.Mutation[j].Name
})
f, err := os.Create(graph_output)
if err != nil {
panic(err)
}
graphql_template.Execute(f, graphql_data)
defer f.Close()
go_template, err := template.ParseFiles("template/go-template.go")
if err != nil {
panic(err)
}
go_data := Data{}
for _, q := range query_map {
gq := pkg.Query{}
gq.Name = strings.ToUpper(q.Name[0:1]) + q.Name[1:]
gq.Context = q.Context
if q.Variables != "" {
gq.Variables = pkg.FillQueryVariables(q.Inputs, node_map)
}
if q.Type == pkg.MUTATION {
go_data.Mutation = append(go_data.Mutation, gq)
} else if q.Type == pkg.QUERY {
go_data.Query = append(go_data.Query, gq)
}
}
sort.Slice(go_data.Query, func(i, j int) bool {
return go_data.Query[i].Name < go_data.Query[j].Name
})
sort.Slice(go_data.Mutation, func(i, j int) bool {
return go_data.Mutation[i].Name < go_data.Mutation[j].Name
})
f, err = os.Create(go_output)
if err != nil {
panic(err)
}
go_template.Execute(f, go_data)
defer f.Close()
}