Skip to content

Commit

Permalink
Implement node type checker
Browse files Browse the repository at this point in the history
  • Loading branch information
bannzai committed Dec 21, 2021
1 parent cd65c44 commit 10afdbf
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package nodecheck

import (
"flag"
"fmt"
"regexp"
"strings"

"github.com/vektah/gqlparser/v2/ast"

"github.com/gqlgo/gqlanalysis"
)

var excludes string
var Analyzer = &gqlanalysis.Analyzer{
Name: "nodecheck",
Doc: "nodecheck finds invalid GraphQL type that type does not conform Node interface",
Flags: func() flag.FlagSet {
f := flag.NewFlagSet("node check", flag.ExitOnError)
f.StringVar(&excludes, "exclude", "", "exclude GraphQL types for node check. it can specify multiple values separated by `,` and it can use regex(e.g *Connection")
return *f
}(),
Run: run,
}

func run(pass *gqlanalysis.Pass) (interface{}, error) {
allTypes := map[string]*ast.Definition{}
for _, def := range pass.Schema.Types {
if def.Kind == ast.Object {
allTypes[def.Name] = def
}
}

allNodeImplements := map[string]*ast.Definition{}
for _, t := range pass.Schema.Implements["Node"] {
allNodeImplements[t.Name] = t
}

unconformedTypes := map[string]*ast.Definition{}
for k, v := range allTypes {
if _, ok := allNodeImplements[k]; !ok {
unconformedTypes[v.Name] = v
}
}

needToNodeTypes := []*ast.Definition{}

for k, v := range unconformedTypes {
ok := false
for _, rules := range strings.Split(excludes, ",") {
regex := regexp.MustCompile(rules)

if ok {
break
}
if regex.MatchString(k) {
ok = true
}
}
if !ok {
needToNodeTypes = append(needToNodeTypes, v)
}
}

if len(needToNodeTypes) > 0 {
return nil, fmt.Errorf("GraphQL types need to conform to Node type %s", needToNodeTypes)
}

return nil, nil
}

0 comments on commit 10afdbf

Please sign in to comment.