-
Notifications
You must be signed in to change notification settings - Fork 5
/
introspection_filter.go
167 lines (151 loc) · 3.79 KB
/
introspection_filter.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package introspectionfilter
import (
"context"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/introspection"
"github.com/vektah/gqlparser/v2/ast"
)
type Plugin struct {
Schema *ast.Schema
FieldFilter FieldFilter
TypeFilter TypeFilter
DirectiveFilter DirectiveFilter
EnumFilter EnumFilter
}
type FieldFilter func(*ast.FieldDefinition) bool
type TypeFilter func(*ast.Definition) bool
type DirectiveFilter func(*ast.DirectiveDefinition) bool
type InputFieldFilter func(*ast.FieldDefinition) bool
type EnumFilter func(*ast.EnumValueDefinition) bool
func (Plugin) ExtensionName() string {
return "IntrospectionFilter"
}
func (Plugin) Validate(schema graphql.ExecutableSchema) error {
return nil
}
func (p Plugin) InterceptField(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
res, err = next(ctx)
if err != nil {
return
}
fc := graphql.GetFieldContext(ctx)
schema := p.Schema
switch fc.Object {
case "__Schema":
switch fc.Field.Name {
case "types":
res = p.filterTypes(res.([]introspection.Type))
case "directives":
res = p.filterDirectives(res.([]introspection.Directive))
}
case "__Type":
iType := fc.Parent.Result.(*introspection.Type)
tName := iType.Name()
if tName == nil {
return
}
astType := schema.Types[*tName]
if astType == nil {
return
}
switch fc.Field.Name {
case "fields":
res = p.filterFields(res.([]introspection.Field), astType)
case "inputFields":
res = p.filterInputFields(res.([]introspection.InputValue), astType)
case "possibleTypes":
res = p.filterTypes(res.([]introspection.Type))
case "enumValues":
res = p.filterEnumValues(res.([]introspection.EnumValue), astType)
}
}
return res, err
}
func (filter Plugin) filterTypes(list []introspection.Type) []introspection.Type {
if filter.TypeFilter == nil {
return list
}
fList := make([]introspection.Type, 0, len(list))
for _, t := range list {
tName := t.Name()
if tName != nil {
astType := filter.Schema.Types[*tName]
if astType == nil {
continue
}
if !filter.TypeFilter(astType) {
continue
}
}
fList = append(fList, t)
}
return fList
}
func (p Plugin) filterDirectives(list []introspection.Directive) []introspection.Directive {
if p.DirectiveFilter == nil {
return list
}
fList := make([]introspection.Directive, 0, len(list))
for _, x := range list {
astDirective := p.Schema.Directives[x.Name]
if astDirective == nil {
continue
}
if !p.DirectiveFilter(astDirective) {
continue
}
fList = append(fList, x)
}
return fList
}
func (p Plugin) filterFields(list []introspection.Field, astType *ast.Definition) []introspection.Field {
if p.FieldFilter == nil {
return list
}
fList := make([]introspection.Field, 0, len(list))
for _, x := range list {
astField := astType.Fields.ForName(x.Name)
if astField == nil {
continue
}
if !p.FieldFilter(astField) {
continue
}
fList = append(fList, x)
}
return fList
}
func (p Plugin) filterInputFields(list []introspection.InputValue, astType *ast.Definition) []introspection.InputValue {
if p.FieldFilter == nil {
return list
}
fList := make([]introspection.InputValue, 0, len(list))
for _, x := range list {
astField := astType.Fields.ForName(x.Name)
if astField == nil {
continue
}
if !p.FieldFilter(astField) {
continue
}
fList = append(fList, x)
}
return fList
}
func (p Plugin) filterEnumValues(list []introspection.EnumValue, astType *ast.Definition) []introspection.EnumValue {
if p.EnumFilter == nil {
return list
}
fList := make([]introspection.EnumValue, 0, len(list))
for _, x := range list {
astEnum := astType.EnumValues.ForName(x.Name)
if astEnum == nil {
continue
}
if !p.EnumFilter(astEnum) {
continue
}
fList = append(fList, x)
}
return fList
}