File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package validator
22
33import (
4+ "sort"
5+
46 . "github.com/dgraph-io/gqlparser/v2/ast"
57 "github.com/dgraph-io/gqlparser/v2/gqlerror"
68)
@@ -11,7 +13,9 @@ type ruleFunc func(observers *Events, addError AddErrFunc)
1113
1214type rule struct {
1315 name string
14- rule ruleFunc
16+ // rules will be called in the ascending order
17+ order int
18+ rule ruleFunc
1519}
1620
1721var rules []rule
@@ -22,10 +26,19 @@ func AddRule(name string, f ruleFunc) {
2226 rules = append (rules , rule {name : name , rule : f })
2327}
2428
29+ // AddRuleWithOrder to rule set with an order.
30+ // f is called once each time `Validate` is executed.
31+ func AddRuleWithOrder (name string , order int , f ruleFunc ) {
32+ rules = append (rules , rule {name : name , order : order , rule : f })
33+ }
34+
2535func Validate (schema * Schema , doc * QueryDocument , variables map [string ]interface {}) gqlerror.List {
2636 var errs gqlerror.List
2737
2838 observers := & Events {}
39+ sort .Slice (rules , func (i , j int ) bool {
40+ return rules [i ].order < rules [j ].order
41+ })
2942 for i := range rules {
3043 rule := rules [i ]
3144 rule .rule (observers , func (options ... ErrorOption ) {
You can’t perform that action at this time.
0 commit comments