/
get_graphql_schema.go
83 lines (70 loc) · 2.32 KB
/
get_graphql_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
package main
import (
"context"
"flag"
"fmt"
"github.com/Yamashou/gqlgenc/client"
"github.com/Yamashou/gqlgenc/introspection"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/formatter"
"log"
"net/http"
"os"
"sort"
"strings"
)
func main() {
if err := getGraphQLSchemaMain(); err != nil {
log.Fatalf("error: %+v", err)
}
}
func getGraphQLSchemaMain() error {
var headerOption string
flag.StringVar(&headerOption, "h", "", "HTTP request header 'HEADER1=Hello HEADER2=Hi'")
flag.Parse()
args := flag.Args()
if len(args) != 1 {
return fmt.Errorf("get-graphql-schema [OPTIONS] ENDPOINT: endpoint not found")
}
endpoint := args[0]
header, err := parseHeaderOption(headerOption)
if err != nil {
return fmt.Errorf("validation: %w", err)
}
schemaDocument, err := getGraphQLSchemaDocument(endpoint, header)
if err != nil {
return fmt.Errorf("getGraphQLSchema: %w", err)
}
sort.Slice(schemaDocument.Directives, func(i, j int) bool { return schemaDocument.Directives[i].Name < schemaDocument.Directives[j].Name })
sort.Slice(schemaDocument.Definitions, func(i, j int) bool { return schemaDocument.Definitions[i].Name < schemaDocument.Definitions[j].Name })
sort.Slice(schemaDocument.Extensions, func(i, j int) bool { return schemaDocument.Extensions[i].Name < schemaDocument.Extensions[j].Name })
astFormatter := formatter.NewFormatter(os.Stdout)
astFormatter.FormatSchemaDocument(schemaDocument)
return nil
}
func parseHeaderOption(headers string) (http.Header, error) {
header := http.Header{}
header.Add("Content-Type", "application/json")
if headers == "" {
return header, nil
}
for _, h := range strings.Split(headers, ",") {
key, value, found := strings.Cut(h, "=")
if !found {
return nil, fmt.Errorf("invalid header: %s", h)
}
header.Add(key, value)
}
return header, nil
}
func getGraphQLSchemaDocument(endpoint string, header http.Header) (*ast.SchemaDocument, error) {
addHeader := func(req *http.Request) {
req.Header = header
}
gqlclient := client.NewClient(http.DefaultClient, endpoint, addHeader)
var res introspection.Query
if err := gqlclient.Post(context.Background(), "Query", introspection.Introspection, &res, nil); err != nil {
return nil, fmt.Errorf("introspection query failed: %w", err)
}
return introspection.ParseIntrospectionQuery(endpoint, res), nil
}