-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessor.go
312 lines (285 loc) · 7.81 KB
/
preprocessor.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package graphqlapi
import (
"fmt"
"reflect"
"runtime/debug"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
)
type Conditional struct {
OfType graphql.Type
Suffix string
Condition func(*PreprocessorConfig) bool
}
func (b *Conditional) Name() string {
return b.OfType.Name() + b.Suffix
}
func (b *Conditional) Description() string {
return b.OfType.Description()
}
func (b *Conditional) String() string {
return b.OfType.String() + b.Suffix
}
func (b *Conditional) Error() error {
return b.OfType.Error()
}
func Beta(ofType graphql.Type) *Conditional {
return &Conditional{
OfType: ofType,
Suffix: "β",
Condition: func(cfg *PreprocessorConfig) bool {
return cfg.BetaFeaturesEnabled
},
}
}
func BetaEnum(value *graphql.EnumValueConfig) *graphql.EnumValueConfig {
return &graphql.EnumValueConfig{
Value: &conditionalEnum{
Value: value,
Condition: func(cfg *PreprocessorConfig) bool {
return cfg.BetaFeaturesEnabled
},
},
}
}
type conditionalEnum struct {
Value *graphql.EnumValueConfig
Condition func(*PreprocessorConfig) bool
}
type PreprocessorConfig struct {
BetaFeaturesEnabled bool
}
type preprocessor struct {
Config *PreprocessorConfig
PreprocessedTypes map[string]graphql.Type
}
func PreprocessSchemaConfig(input graphql.SchemaConfig, config *PreprocessorConfig) graphql.SchemaConfig {
p := &preprocessor{
Config: config,
PreprocessedTypes: make(map[string]graphql.Type),
}
result := input
if obj := input.Query; obj != nil {
result.Query = p.preprocessObject(obj)
}
if obj := input.Mutation; obj != nil {
result.Mutation = p.preprocessObject(obj)
}
if obj := input.Subscription; obj != nil {
result.Subscription = p.preprocessObject(obj)
}
result.Types = nil
for _, t := range input.Types {
if newType, ok := p.preprocessType(t); ok {
result.Types = append(result.Types, newType)
}
}
return result
}
// Workaround for https://github.com/graphql-go/graphql/issues/250
var fixedDateTime = graphql.NewScalar(graphql.ScalarConfig{
Name: graphql.DateTime.Name(),
Description: graphql.DateTime.Description(),
Serialize: graphql.DateTime.Serialize,
ParseValue: graphql.DateTime.ParseValue,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return graphql.DateTime.ParseValue(valueAST.Value)
}
return nil
},
})
func (p *preprocessor) preprocessType(t graphql.Type) (result graphql.Type, ok bool) {
if result, ok := p.PreprocessedTypes[t.String()]; ok {
return result, result != nil
}
defer func() {
p.PreprocessedTypes[t.String()] = result
}()
switch t := t.(type) {
case *graphql.List:
ofType, ok := p.preprocessType(t.OfType)
if !ok {
return nil, false
}
return graphql.NewList(ofType), true
case *graphql.NonNull:
ofType, ok := p.preprocessType(t.OfType)
if !ok {
return nil, false
}
return graphql.NewNonNull(ofType), true
case *graphql.InputObject:
return p.preprocessInputObject(t), true
case *graphql.Object:
return p.preprocessObject(t), true
case *Conditional:
if t.Condition(p.Config) {
return p.preprocessType(t.OfType)
}
return nil, false
case *graphql.Scalar:
if t.Name() == "DateTime" {
return fixedDateTime, true
}
return t, true
case *graphql.Enum:
return p.preprocessEnum(t), true
case *graphql.Interface:
return p.preprocessInterface(t), true
case *graphql.Union:
return p.preprocessUnion(t), true
}
panic(fmt.Errorf("unknown graphql type %T", t))
}
func resolveWrapper(resolve graphql.FieldResolveFn) graphql.FieldResolveFn {
if resolve == nil {
return nil
}
return func(p graphql.ResolveParams) (v interface{}, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v\n%v", r, string(debug.Stack()))
}
}()
v, err = resolve(p)
// graphql-go interprets typed nil as non-null. that makes things messy and error-prone, so
// let's just fix that for all our resolve functions here
if v != nil {
if vValue := reflect.ValueOf(v); vValue.Kind() == reflect.Ptr && vValue.IsNil() {
v = nil
}
}
return v, err
}
}
func (p *preprocessor) preprocessEnum(enum *graphql.Enum) *graphql.Enum {
config := graphql.EnumConfig{
Name: enum.Name(),
Description: enum.Description(),
Values: make(map[string]*graphql.EnumValueConfig),
}
for _, value := range enum.Values() {
if Conditional, ok := value.Value.(*conditionalEnum); ok {
if Conditional.Condition(p.Config) {
config.Values[value.Name] = Conditional.Value
}
} else {
config.Values[value.Name] = &graphql.EnumValueConfig{
Value: value.Value,
Description: value.Description,
DeprecationReason: value.DeprecationReason,
}
}
}
return graphql.NewEnum(config)
}
func (p *preprocessor) preprocessField(def *graphql.FieldDefinition) (*graphql.Field, bool) {
newType, ok := p.preprocessType(def.Type)
if !ok {
return nil, false
}
f := &graphql.Field{
Name: def.Name,
Type: newType,
Resolve: resolveWrapper(def.Resolve),
DeprecationReason: def.DeprecationReason,
Description: def.Description,
}
if len(def.Args) > 0 {
f.Args = make(graphql.FieldConfigArgument)
for _, arg := range def.Args {
if newType, ok := p.preprocessType(arg.Type); ok {
f.Args[arg.Name()] = &graphql.ArgumentConfig{
Type: newType,
DefaultValue: arg.DefaultValue,
Description: arg.Description(),
}
}
}
}
return f, true
}
func (p *preprocessor) preprocessInputObject(obj *graphql.InputObject) *graphql.InputObject {
return graphql.NewInputObject(graphql.InputObjectConfig{
Name: obj.Name(),
Fields: graphql.InputObjectConfigFieldMapThunk(func() graphql.InputObjectConfigFieldMap {
fields := graphql.InputObjectConfigFieldMap{}
for name, f := range obj.Fields() {
newType, ok := p.preprocessType(f.Type)
if !ok {
continue
}
fields[name] = &graphql.InputObjectFieldConfig{
Type: newType,
DefaultValue: f.DefaultValue,
Description: f.Description(),
}
}
return fields
}),
Description: obj.Description(),
})
}
func (p *preprocessor) preprocessUnion(u *graphql.Union) *graphql.Union {
config := graphql.UnionConfig{
Description: u.Description(),
Name: u.Name(),
ResolveType: u.ResolveType,
}
for _, obj := range u.Types() {
config.Types = append(config.Types, p.preprocessObject(obj))
}
return graphql.NewUnion(config)
}
func (p *preprocessor) preprocessObject(obj *graphql.Object) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: obj.Name(),
Interfaces: graphql.InterfacesThunk(func() []*graphql.Interface {
ifaces := []*graphql.Interface{}
for _, iface := range obj.Interfaces() {
if newType, ok := p.preprocessType(iface); ok {
ifaces = append(ifaces, newType.(*graphql.Interface))
}
}
return ifaces
}),
Fields: graphql.FieldsThunk(func() graphql.Fields {
fields := graphql.Fields{}
for name, def := range obj.Fields() {
f, ok := p.preprocessField(def)
if !ok {
continue
}
fields[name] = f
}
if obj.Error() != nil {
panic(obj.Error().Error())
}
return fields
}),
IsTypeOf: obj.IsTypeOf,
Description: obj.Description(),
})
}
func (p *preprocessor) preprocessInterface(iface *graphql.Interface) *graphql.Interface {
return graphql.NewInterface(graphql.InterfaceConfig{
Name: iface.Name(),
Fields: graphql.FieldsThunk(func() graphql.Fields {
fields := graphql.Fields{}
for name, def := range iface.Fields() {
f, ok := p.preprocessField(def)
if !ok {
continue
}
fields[name] = f
}
return fields
}),
ResolveType: func(params graphql.ResolveTypeParams) *graphql.Object {
return p.preprocessObject(iface.ResolveType(params))
},
Description: iface.Description(),
})
}