-
Notifications
You must be signed in to change notification settings - Fork 840
/
type_info.go
270 lines (254 loc) · 7.64 KB
/
type_info.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
package graphql
import (
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
)
// TODO: can move TypeInfo to a utils package if there ever is one
/**
* TypeInfo is a utility class which, given a GraphQL schema, can keep track
* of the current field and type definitions at any point in a GraphQL document
* AST during a recursive descent by calling `enter(node)` and `leave(node)`.
*/
type fieldDefFn func(schema *Schema, parentType Type, fieldAST *ast.Field) *FieldDefinition
type TypeInfo struct {
schema *Schema
typeStack []Output
parentTypeStack []Composite
inputTypeStack []Input
fieldDefStack []*FieldDefinition
directive *Directive
argument *Argument
getFieldDef fieldDefFn
}
type TypeInfoConfig struct {
Schema *Schema
// NOTE: this experimental optional second parameter is only needed in order
// to support non-spec-compliant codebases. You should never need to use it.
// It may disappear in the future.
FieldDefFn fieldDefFn
}
func NewTypeInfo(opts *TypeInfoConfig) *TypeInfo {
getFieldDef := opts.FieldDefFn
if getFieldDef == nil {
getFieldDef = DefaultTypeInfoFieldDef
}
return &TypeInfo{
schema: opts.Schema,
getFieldDef: getFieldDef,
}
}
func (ti *TypeInfo) Type() Output {
if len(ti.typeStack) > 0 {
return ti.typeStack[len(ti.typeStack)-1]
}
return nil
}
func (ti *TypeInfo) ParentType() Composite {
if len(ti.parentTypeStack) > 0 {
return ti.parentTypeStack[len(ti.parentTypeStack)-1]
}
return nil
}
func (ti *TypeInfo) InputType() Input {
if len(ti.inputTypeStack) > 0 {
return ti.inputTypeStack[len(ti.inputTypeStack)-1]
}
return nil
}
func (ti *TypeInfo) FieldDef() *FieldDefinition {
if len(ti.fieldDefStack) > 0 {
return ti.fieldDefStack[len(ti.fieldDefStack)-1]
}
return nil
}
func (ti *TypeInfo) Directive() *Directive {
return ti.directive
}
func (ti *TypeInfo) Argument() *Argument {
return ti.argument
}
func (ti *TypeInfo) Enter(node ast.Node) {
schema := ti.schema
var ttype Type
switch node := node.(type) {
case *ast.SelectionSet:
namedType := GetNamed(ti.Type())
var compositeType Composite
if IsCompositeType(namedType) {
compositeType, _ = namedType.(Composite)
}
ti.parentTypeStack = append(ti.parentTypeStack, compositeType)
case *ast.Field:
parentType := ti.ParentType()
var fieldDef *FieldDefinition
if parentType != nil {
fieldDef = ti.getFieldDef(schema, parentType.(Type), node)
}
ti.fieldDefStack = append(ti.fieldDefStack, fieldDef)
if fieldDef != nil {
ti.typeStack = append(ti.typeStack, fieldDef.Type)
} else {
ti.typeStack = append(ti.typeStack, nil)
}
case *ast.Directive:
nameVal := ""
if node.Name != nil {
nameVal = node.Name.Value
}
ti.directive = schema.Directive(nameVal)
case *ast.OperationDefinition:
if node.Operation == ast.OperationTypeQuery {
ttype = schema.QueryType()
} else if node.Operation == ast.OperationTypeMutation {
ttype = schema.MutationType()
} else if node.Operation == ast.OperationTypeSubscription {
ttype = schema.SubscriptionType()
}
ti.typeStack = append(ti.typeStack, ttype)
case *ast.InlineFragment:
typeConditionAST := node.TypeCondition
if typeConditionAST != nil {
ttype, _ = typeFromAST(*schema, node.TypeCondition)
ti.typeStack = append(ti.typeStack, ttype)
} else {
ti.typeStack = append(ti.typeStack, ti.Type())
}
case *ast.FragmentDefinition:
typeConditionAST := node.TypeCondition
if typeConditionAST != nil {
ttype, _ = typeFromAST(*schema, typeConditionAST)
ti.typeStack = append(ti.typeStack, ttype)
} else {
ti.typeStack = append(ti.typeStack, ti.Type())
}
case *ast.VariableDefinition:
ttype, _ = typeFromAST(*schema, node.Type)
ti.inputTypeStack = append(ti.inputTypeStack, ttype)
case *ast.Argument:
nameVal := ""
if node.Name != nil {
nameVal = node.Name.Value
}
var argType Input
var argDef *Argument
directive := ti.Directive()
fieldDef := ti.FieldDef()
if directive != nil {
for _, arg := range directive.Args {
if arg.Name() == nameVal {
argDef = arg
}
}
} else if fieldDef != nil {
for _, arg := range fieldDef.Args {
if arg.Name() == nameVal {
argDef = arg
}
}
}
if argDef != nil {
argType = argDef.Type
}
ti.argument = argDef
ti.inputTypeStack = append(ti.inputTypeStack, argType)
case *ast.ListValue:
listType := GetNullable(ti.InputType())
if list, ok := listType.(*List); ok {
ti.inputTypeStack = append(ti.inputTypeStack, list.OfType)
} else {
ti.inputTypeStack = append(ti.inputTypeStack, nil)
}
case *ast.ObjectField:
var fieldType Input
objectType := GetNamed(ti.InputType())
if objectType, ok := objectType.(*InputObject); ok {
nameVal := ""
if node.Name != nil {
nameVal = node.Name.Value
}
if inputField, ok := objectType.Fields()[nameVal]; ok {
fieldType = inputField.Type
}
}
ti.inputTypeStack = append(ti.inputTypeStack, fieldType)
}
}
func (ti *TypeInfo) Leave(node ast.Node) {
kind := node.GetKind()
switch kind {
case kinds.SelectionSet:
// pop ti.parentTypeStack
if len(ti.parentTypeStack) > 0 {
_, ti.parentTypeStack = ti.parentTypeStack[len(ti.parentTypeStack)-1], ti.parentTypeStack[:len(ti.parentTypeStack)-1]
}
case kinds.Field:
// pop ti.fieldDefStack
if len(ti.fieldDefStack) > 0 {
_, ti.fieldDefStack = ti.fieldDefStack[len(ti.fieldDefStack)-1], ti.fieldDefStack[:len(ti.fieldDefStack)-1]
}
// pop ti.typeStack
if len(ti.typeStack) > 0 {
_, ti.typeStack = ti.typeStack[len(ti.typeStack)-1], ti.typeStack[:len(ti.typeStack)-1]
}
case kinds.Directive:
ti.directive = nil
case kinds.OperationDefinition, kinds.InlineFragment, kinds.FragmentDefinition:
// pop ti.typeStack
if len(ti.typeStack) > 0 {
_, ti.typeStack = ti.typeStack[len(ti.typeStack)-1], ti.typeStack[:len(ti.typeStack)-1]
}
case kinds.VariableDefinition:
// pop ti.inputTypeStack
if len(ti.inputTypeStack) > 0 {
_, ti.inputTypeStack = ti.inputTypeStack[len(ti.inputTypeStack)-1], ti.inputTypeStack[:len(ti.inputTypeStack)-1]
}
case kinds.Argument:
ti.argument = nil
// pop ti.inputTypeStack
if len(ti.inputTypeStack) > 0 {
_, ti.inputTypeStack = ti.inputTypeStack[len(ti.inputTypeStack)-1], ti.inputTypeStack[:len(ti.inputTypeStack)-1]
}
case kinds.ListValue, kinds.ObjectField:
// pop ti.inputTypeStack
if len(ti.inputTypeStack) > 0 {
_, ti.inputTypeStack = ti.inputTypeStack[len(ti.inputTypeStack)-1], ti.inputTypeStack[:len(ti.inputTypeStack)-1]
}
}
}
// DefaultTypeInfoFieldDef Not exactly the same as the executor's definition of FieldDef, in this
// statically evaluated environment we do not always have an Object type,
// and need to handle Interface and Union types.
func DefaultTypeInfoFieldDef(schema *Schema, parentType Type, fieldAST *ast.Field) *FieldDefinition {
name := ""
if fieldAST.Name != nil {
name = fieldAST.Name.Value
}
if name == SchemaMetaFieldDef.Name &&
schema.QueryType() == parentType {
return SchemaMetaFieldDef
}
if name == TypeMetaFieldDef.Name &&
schema.QueryType() == parentType {
return TypeMetaFieldDef
}
if name == TypeNameMetaFieldDef.Name && parentType != nil {
if t, ok := parentType.(*Object); ok && t != nil {
return TypeNameMetaFieldDef
}
if t, ok := parentType.(*Interface); ok && t != nil {
return TypeNameMetaFieldDef
}
if t, ok := parentType.(*Union); ok && t != nil {
return TypeNameMetaFieldDef
}
}
if parentType, ok := parentType.(*Object); ok && parentType != nil {
field, _ := parentType.Fields()[name]
return field
}
if parentType, ok := parentType.(*Interface); ok && parentType != nil {
field, _ := parentType.Fields()[name]
return field
}
return nil
}