-
Notifications
You must be signed in to change notification settings - Fork 19
/
schema.go
54 lines (43 loc) · 1.26 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
package schema
import (
"bytes"
"embed"
"fmt"
"io/fs"
"strings"
)
// content holds all the SDL file content.
//go:embed *.graphql type/*.graphql
var content embed.FS
// String reads the .graphql schema files from the embed.FS, concatenating the
// files together into one string.
//
// If this method complains about not finding functions AssetNames() or MustAsset(),
// run `go generate` against this package to generate the functions.
func String() (string, error) {
var buf bytes.Buffer
fn := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("walking dir: %w", err)
}
// Only add files with the .graphql extension.
if !strings.HasSuffix(path, ".graphql") {
return nil
}
b, err := content.ReadFile(path)
if err != nil {
return fmt.Errorf("reading file %q: %w", path, err)
}
// Add a newline to separate each file.
b = append(b, []byte("\n")...)
if _, err := buf.Write(b); err != nil {
return fmt.Errorf("writing %q bytes to buffer: %w", path, err)
}
return nil
}
// Recursively walk this directory and append all the file contents together.
if err := fs.WalkDir(content, ".", fn); err != nil {
return buf.String(), fmt.Errorf("walking content directory: %w", err)
}
return buf.String(), nil
}