forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize.go
282 lines (240 loc) · 8.05 KB
/
normalize.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
package federation
import (
"fmt"
"reflect"
"sort"
"github.com/denkhaus/thunder/graphql"
)
// CollectTypes finds all types reachable from typ and stores them in types as a
// map from type to name.
//
// TODO: Stick this in an internal package.
func CollectTypes(typ graphql.Type, types map[graphql.Type]string) error {
if _, ok := types[typ]; ok {
return nil
}
switch typ := typ.(type) {
case *graphql.NonNull:
CollectTypes(typ.Type, types)
case *graphql.List:
CollectTypes(typ.Type, types)
case *graphql.Object:
types[typ] = typ.Name
for _, field := range typ.Fields {
CollectTypes(field.Type, types)
}
case *graphql.Union:
types[typ] = typ.Name
for _, obj := range typ.Types {
CollectTypes(obj, types)
}
case *graphql.Enum:
types[typ] = typ.Type
case *graphql.Scalar:
types[typ] = typ.Type
default:
return fmt.Errorf("bad typ %v", typ)
}
return nil
}
func makeTypeNameMap(schema *graphql.Schema) (map[string]graphql.Type, error) {
allTypes := make(map[graphql.Type]string)
if err := CollectTypes(schema.Query, allTypes); err != nil {
return nil, err
}
if err := CollectTypes(schema.Mutation, allTypes); err != nil {
return nil, err
}
reversedTypes := make(map[string]graphql.Type)
for typ, name := range allTypes {
reversedTypes[name] = typ
}
return reversedTypes, nil
}
// flattener flattens queries into a normalized form that's easier to wrangle
// for the query planner and executor.
//
// A normalized query has almost all ambiguity removed from the query: Selection
// sets for objects contain each alias exactly once, and have no fragments.
// Selection sets for unions (or interfaces) contain exactly one inline fragment
// with an inner normalized query for each possible type.
type flattener struct {
// types is a map from all type names to the actual type, used to check if a
// fragment matches an object type.
types map[string]graphql.Type
}
// newFlattener creates a new flattener.
func newFlattener(schema *graphql.Schema) (*flattener, error) {
types, err := makeTypeNameMap(schema)
if err != nil {
return nil, err
}
return &flattener{
types: types,
}, nil
}
// applies checks if obj matches fragment.
func (f *flattener) applies(obj *graphql.Object, fragment *graphql.Fragment) (bool, error) {
switch typ := f.types[fragment.On].(type) {
case *graphql.Object:
// An object matches if the name matches.
return typ.Name == obj.Name, nil
case *graphql.Union:
// A union matches if the object is part of the union.
_, ok := typ.Types[obj.Name]
return ok, nil
default:
return false, fmt.Errorf("unknown fragment type %s", fragment.On)
}
}
// flattenFragments flattens all fragments at the current level. It inlines the
// selections of each fragment, but does not descend down recursively into those
// selections.
func (f *flattener) flattenFragments(selectionSet *graphql.SelectionSet, typ *graphql.Object, target *[]*graphql.Selection) error {
// Start with the non-fragment selections.
*target = append(*target, selectionSet.Selections...)
// Descend into fragments matching the current type.
for _, fragment := range selectionSet.Fragments {
ok, err := f.applies(typ, fragment)
if err != nil {
return err
}
if ok {
if err := f.flattenFragments(fragment.SelectionSet, typ, target); err != nil {
return err
}
}
}
return nil
}
// mergeSameAlias combines selections with same alias, verifying their
// arguments and field are identical.
func mergeSameAlias(selections []*graphql.Selection) ([]*graphql.Selection, error) {
sort.Slice(selections, func(i, j int) bool {
return selections[i].Alias < selections[j].Alias
})
newSelections := selections[:0]
var last *graphql.Selection
for _, selection := range selections {
if last == nil || selection.Alias != last.Alias {
// Make a copy of the selection so we can modify it below
// or when we flatten recursively later.
copy := *selection
selection = ©
newSelections = append(newSelections, selection)
last = selection
continue
}
if selection.Name != last.Name {
return nil, fmt.Errorf("two selections with same alias (%s) have different names (%s and %s)",
selection.Alias, selection.Name, last.Name)
}
if !reflect.DeepEqual(selection.UnparsedArgs, last.UnparsedArgs) {
return nil, fmt.Errorf("two selections with same alias (%s) have different arguments (%v and %v)",
selection.Alias, selection.UnparsedArgs, last.UnparsedArgs)
}
if selection.SelectionSet != nil {
if last.SelectionSet == nil {
return nil, fmt.Errorf("one selection with alias %s has subselections and one does not",
selection.Alias)
}
last.SelectionSet.Selections = append(last.SelectionSet.Selections,
selection.SelectionSet.Selections...)
last.SelectionSet.Fragments = append(last.SelectionSet.Fragments,
selection.SelectionSet.Fragments...)
}
}
return newSelections, nil
}
// flatten recursively normalizes a query.
func (f *flattener) flatten(selectionSet *graphql.SelectionSet, typ graphql.Type) (*graphql.SelectionSet, error) {
switch typ := typ.(type) {
// For non-null and list types, flatten using the inner type.
case *graphql.NonNull:
return f.flatten(selectionSet, typ.Type)
case *graphql.List:
return f.flatten(selectionSet, typ.Type)
case *graphql.Enum, *graphql.Scalar:
// For enum and scalar types, check that there is no selection set.
if selectionSet != nil {
return nil, fmt.Errorf("unexpected selection on enum or scalar")
}
return selectionSet, nil
case *graphql.Object:
if selectionSet == nil {
return nil, fmt.Errorf("object %s needs selection set", typ.Name)
}
// To normalize an object query, first flatten all fragments and combine
// their selections.
//
// Then, after collecting the full set of sub-selections for each alias,
// recursively normalize the resulting query.
// Collect all selections on this object and merge selections
// with the same alias.
selections := make([]*graphql.Selection, 0, len(selectionSet.Selections))
if err := f.flattenFragments(selectionSet, typ, &selections); err != nil {
return nil, err
}
selections, err := mergeSameAlias(selections)
if err != nil {
return nil, err
}
// Recursively flatten.
for _, selection := range selections {
// Get the type of the field.
var fieldTyp graphql.Type
if selection.Name == "__typename" {
fieldTyp = &graphql.Scalar{Type: "string"}
} else {
field, ok := typ.Fields[selection.Name]
if !ok {
return nil, fmt.Errorf("unknown field %s on typ %s", selection.Name, typ.Name)
}
fieldTyp = field.Type
}
selectionSet, err := f.flatten(selection.SelectionSet, fieldTyp)
if err != nil {
return nil, err
}
selection.SelectionSet = selectionSet
}
return &graphql.SelectionSet{
Selections: selections,
}, nil
case *graphql.Union:
// To normalize a union query, consider all possible union types and
// build an inline fragment for each them by recursively normalize the
// query for the concrete object types.
// Create a fragment for every possible type.
fragments := make([]*graphql.Fragment, 0, len(typ.Types))
for _, obj := range typ.Types {
plan, err := f.flatten(selectionSet, obj)
if err != nil {
return nil, err
}
// Don't bother if there are no selections. There will be no
// fragments.
if len(plan.Selections) > 0 {
fragments = append(fragments, &graphql.Fragment{
On: obj.Name,
SelectionSet: plan,
})
}
}
// Sort fragments on name for deterministic ordering.
sort.Slice(fragments, func(a, b int) bool {
return fragments[a].On < fragments[b].On
})
return &graphql.SelectionSet{
Fragments: fragments,
}, nil
default:
return nil, fmt.Errorf("bad typ %v", typ)
}
}
// TODO: When adding types to a union, the normalizer might not know about all
// types. Fields like __typename should be appropriately kept at the top-level,
// instead of (or in addition to?) inlined for every possible type in a
// fragment.
// TODO: Add some limit to the expansion logic above for adversarial inputs.
// TODO: Use Normalize in the normal execution codepath.