-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
schema.go
55 lines (44 loc) · 1.04 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
// Package schema is used to read schema files
// go:generate go-bindata -ignore=\.go -pkg=schema -o=bindata.go ./...
package schema
import (
"bytes"
"embed"
"fmt"
)
//go:embed *.graphql type/*.graphql
var fs embed.FS
// GetRootSchema reads the schema files and combines them into a single schema.
func GetRootSchema() (string, error) {
b, err := fs.ReadFile("schema.graphql")
if err != nil {
return "", err
}
buf := bytes.Buffer{}
buf.Write(b)
types, err := fs.ReadDir("type")
if err != nil {
return "", err
}
for _, t := range types {
b, err = fs.ReadFile(fmt.Sprintf("type/%s", t.Name()))
if err != nil {
return "", err
}
buf.Write(b)
// Add a newline if the file does not end in a newline.
if len(b) > 0 && b[len(b)-1] != '\n' {
buf.WriteByte('\n')
}
}
return buf.String(), nil
}
// MustGetRootSchema reads the schema files and combines them into a single
// schema. It panics if there are any errors.
func MustGetRootSchema() string {
s, err := GetRootSchema()
if err != nil {
panic(err)
}
return s
}