-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.go
97 lines (81 loc) · 2.06 KB
/
schema.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
// Copyright (c) 2020, Sylabs, Inc. All rights reserved.
// +build mage
package main
import (
"bytes"
"compress/zlib"
"io"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/graph-gophers/graphql-go"
)
var (
schemaInputDir = filepath.Join("api", "graphql-schema")
schemaTemplatePath = filepath.Join("internal", "pkg", "schema", "bindata.go.tmpl")
schemaOutputPath = filepath.Join("internal", "pkg", "schema", "bindata.go")
)
// readFiles walks the directory specified by root, writing the contents of each file to w.
func readFiles(root string, w io.Writer) error {
return filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// Add terminating newline, if not present.
if nl := []byte("\n"); !bytes.HasSuffix(b, nl) {
b = append(b, nl...)
}
_, err = w.Write(b)
return err
})
}
// writeCompressedSchema validates the schema, compresses it and writes it to w.
func writeCompressedSchema(w io.Writer) error {
// Read the schema into buffer.
b := &bytes.Buffer{}
if err := readFiles(schemaInputDir, b); err != nil {
return err
}
// Validate the schema.
if _, err := graphql.ParseSchema(b.String(), nil, graphql.UseStringDescriptions()); err != nil {
return err
}
// Write schema in compressed format.
zw := zlib.NewWriter(w)
defer zw.Close()
// Write out schema.
_, err := io.Copy(zw, b)
return err
}
// generateSchema generates a Go file that exposes the schema.
func generateSchema() error {
// Get schema.
b := &bytes.Buffer{}
if err := writeCompressedSchema(b); err != nil {
return err
}
// Open file to write generated Go code into.
f, err := os.OpenFile(schemaOutputPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
// Write schema template.
t, err := template.ParseFiles(schemaTemplatePath)
if err != nil {
return err
}
args := struct {
Data []byte
GeneratedBy string
}{
b.Bytes(),
"mage",
}
return t.Execute(f, args)
}