-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional_schema.go
55 lines (48 loc) · 1.26 KB
/
optional_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 optionalschema
import (
"github.com/gqlgo/gqlanalysis"
"strings"
)
var excludes []string
func isExcludeField(filedName string) bool {
for _, exclude := range excludes {
if exclude == filedName {
return true
}
}
return false
}
func Analyzer(excludes string) *gqlanalysis.Analyzer {
return &gqlanalysis.Analyzer{
Name: "optionalschema",
Doc: "optionalschema finds a optionalschema in your GraphQL query files",
Run: run(excludes),
}
}
func run(excludesStr string) func(pass *gqlanalysis.Pass) (interface{}, error) {
excludes = strings.Split(excludesStr, ",")
return func(pass *gqlanalysis.Pass) (interface{}, error) {
for _, t := range pass.Schema.Types {
if t.BuiltIn {
continue
}
for _, field := range t.Fields {
if field != nil && field.Type != nil {
if !field.Type.NonNull && !isExcludeField(field.Name) {
if field.Position != nil {
pass.Reportf(field.Position, "%s is optional", field.Name)
}
}
for _, argument := range field.Arguments {
if argument != nil && argument.Type != nil {
if !argument.Type.NonNull && !isExcludeField(argument.Name) {
pass.Reportf(argument.Position, "%s is optional", argument.Name)
}
}
}
}
}
}
return nil, nil
}
}