-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgqlcost.go
45 lines (36 loc) · 1.01 KB
/
gqlcost.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
package gqlcost
import (
"sync"
"github.com/graphql-go/graphql"
)
// AnalysisOptions provides options for cost analysis.
type AnalysisOptions struct {
MaximumCost int
DefaultCost int
Valiables map[string]interface{}
CostMap CostMap
ComplexityRange ComplexityRange
}
var addRule sync.Once
// AddCostAnalysisRule adds a rule of cost analysis to
// graphql.SpecifiedRules.
func AddCostAnalysisRule(opts AnalysisOptions) {
addRule.Do(func() {
r := AnalysisRule(opts)
graphql.SpecifiedRules = append(graphql.SpecifiedRules, r)
})
}
// AnalysisRule provides cost analysis rule (function)
func AnalysisRule(opts AnalysisOptions) graphql.ValidationRuleFn {
r := &costAnalysisRule{
opts: opts,
}
return r.validationRule
}
type costAnalysisRule struct {
opts AnalysisOptions
}
func (r *costAnalysisRule) validationRule(context *graphql.ValidationContext) *graphql.ValidationRuleInstance {
ca := newCostAnalysis(context, r.opts)
return &graphql.ValidationRuleInstance{VisitorOpts: ca.visitorOptions()}
}