forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (40 loc) · 977 Bytes
/
main.go
File metadata and controls
47 lines (40 loc) · 977 Bytes
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
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
)
func main() {
if err := mainRun(os.Args, os.Stdout, os.Stderr); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func mainRun(args []string, stdout io.Writer, stderr io.Writer) error {
if len(args) < 3 {
return errors.New("usage: gql-validate <schema-file> <query>")
}
schemaFile := args[1]
bb, err := ioutil.ReadFile(schemaFile)
if err != nil {
return fmt.Errorf("error loading schema file: %w", err)
}
schema, schemaErr := gqlparser.LoadSchema(&ast.Source{
Name: schemaFile,
Input: string(bb),
BuiltIn: true,
})
if schemaErr != nil {
return fmt.Errorf("error loading schema: %w", schemaErr)
}
doc, queryErr := gqlparser.LoadQuery(schema, args[2])
if len(queryErr) > 0 {
return fmt.Errorf("query error: %w", queryErr)
}
fmt.Fprintf(stdout, "doc: %#v\n", doc.Operations)
return nil
}