-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (71 loc) · 2.04 KB
/
Copy pathmain.go
File metadata and controls
93 lines (71 loc) · 2.04 KB
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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"modosuite/graphql-codegen-go/gofmt"
"os"
"path/filepath"
"strings"
)
type headers []string
func (i *headers) String() string {
return "my string representation"
}
func (i *headers) Set(value string) error {
*i = append(*i, value)
return nil
}
var (
packageName = flag.String("package", "main", "Name of the package to output")
schemaPath = flag.String("schema", "", "Path to locate the graphql schema")
operationsGlob = flag.String("operations", "", "Glob to locate the graphql operations")
endpoint = flag.String("E", "", "Endpoint of the api")
fullSchema = flag.Bool("full", false, "Include full schema types")
)
var headerList headers
func main() {
flag.Var(&headerList, "H", "")
flag.Parse()
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf(`
package %s
import (
"encoding/json"
)
`, *packageName))
headerMap := make(map[string][]string)
for _, h := range headerList {
slices := strings.Split(h, ":")
headerMap[strings.TrimSpace(slices[0])] = []string{strings.TrimSpace(slices[1])}
}
schema, err := Introspect(*endpoint, headerMap, false)
if err != nil {
fmt.Println(err)
}
err = generateInputs(schema, &buf)
if err != nil { panic(err) }
if *fullSchema {
err = generateSchema(schema, &buf)
if err != nil { panic(err) }
}
operationFiles, err := filepath.Glob(*operationsGlob)
if err != nil { panic(err) }
queryDocs := []string{}
for _, file := range operationFiles {
opFile, err := ioutil.ReadFile(file)
if err != nil { panic(err) }
queryDocs = append(queryDocs, string(opFile))
}
queryDoc, err := parseQueryDocuments(schema, queryDocs)
if err != nil { panic(err) }
err = generateOperations(schema, queryDoc, &buf)
if err != nil { panic(err) }
file, err := os.Create("schema.go")
if err != nil { panic(err) }
defer file.Close()
err = gofmt.ProcessFile("schema.go", &buf, file, false)
if err != nil { panic(err) }
fmt.Println("Successfully generated schema file!")
}