-
Notifications
You must be signed in to change notification settings - Fork 55
/
plan.go
443 lines (403 loc) · 13.9 KB
/
plan.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package bramble
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
// QueryPlanStep is a single execution step
type QueryPlanStep struct {
ServiceURL string
ServiceName string
ParentType string
SelectionSet ast.SelectionSet
InsertionPoint []string
Then []*QueryPlanStep
executionResult *executionStepResult
}
type executionStepResult struct {
executed bool
error error
timeTaken time.Duration
}
func (e *executionStepResult) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Executed bool
Error error `json:",omitempty"`
TimeTaken string
}{
Executed: e.executed,
TimeTaken: e.timeTaken.String(),
Error: e.error,
})
}
// MarshalJSON marshals the step the JSON
func (s *QueryPlanStep) MarshalJSON() ([]byte, error) {
ctx := graphql.WithOperationContext(context.Background(), &graphql.OperationContext{
Variables: map[string]interface{}{},
})
return json.Marshal(&struct {
ServiceURL string
ParentType string
SelectionSet string
InsertionPoint []string
ExecutionStepResult *executionStepResult `json:",omitempty"`
Then []*QueryPlanStep
}{
ServiceURL: s.ServiceURL,
ParentType: s.ParentType,
SelectionSet: formatSelectionSetSingleLine(ctx, nil, s.SelectionSet),
InsertionPoint: s.InsertionPoint,
Then: s.Then,
ExecutionStepResult: s.executionResult,
})
}
// QueryPlan is a query execution plan
type QueryPlan struct {
RootSteps []*QueryPlanStep
}
// PlanningContext contains the necessary information used to plan a query.
type PlanningContext struct {
Operation *ast.OperationDefinition
Schema *ast.Schema
Locations FieldURLMap
IsBoundary map[string]bool
Services map[string]*Service
}
// Plan returns a query plan from the given planning context
func Plan(ctx *PlanningContext) (*QueryPlan, error) {
var parentType string
switch ctx.Operation.Operation {
case ast.Query:
parentType = queryObjectName
case ast.Mutation:
parentType = mutationObjectName
default:
return nil, fmt.Errorf("not implemented")
}
steps, err := createSteps(ctx, nil, parentType, "", ctx.Operation.SelectionSet)
if err != nil {
return nil, err
}
return &QueryPlan{
RootSteps: steps,
}, nil
}
func createSteps(ctx *PlanningContext, insertionPoint []string, parentType string, parentLocation string, selectionSet ast.SelectionSet) ([]*QueryPlanStep, error) {
var result []*QueryPlanStep
routedSelectionSet, err := routeSelectionSet(ctx, parentType, parentLocation, selectionSet)
if err != nil {
return nil, err
}
for location, selectionSet := range routedSelectionSet {
selectionSetForLocation, childrenSteps, err := extractSelectionSet(ctx, insertionPoint, parentType, selectionSet, location)
if err != nil {
return nil, err
}
name := "unknown"
if service, ok := ctx.Services[location]; ok {
name = service.Name
}
// the insertionPoint slice can be modified later as we're appending
// values to it while recursively traversing the selection set, so we
// need to make a copy
var insertionPointCopy []string
if len(insertionPoint) > 0 {
insertionPointCopy = make([]string, len(insertionPoint))
copy(insertionPointCopy, insertionPoint)
}
result = append(result, &QueryPlanStep{
InsertionPoint: insertionPointCopy,
Then: childrenSteps,
ServiceURL: location,
ServiceName: name,
ParentType: parentType,
SelectionSet: selectionSetForLocation,
})
}
return result, nil
}
var reservedAliases = map[string]string{
"_bramble__typename": "__typename",
"_bramble_id": IdFieldName,
}
func extractSelectionSet(ctx *PlanningContext, insertionPoint []string, parentType string, input ast.SelectionSet, location string) (ast.SelectionSet, []*QueryPlanStep, error) {
var selectionSetResult []ast.Selection
var childrenStepsResult []*QueryPlanStep
var remoteSelections []ast.Selection
for _, selection := range input {
switch selection := selection.(type) {
case *ast.Field:
for reservedAlias, requiredName := range reservedAliases {
if selection.Alias == reservedAlias && selection.Name != requiredName {
return nil, nil, gqlerror.Errorf("%s.%s: alias \"%s\" is reserved for system use", strings.Join(insertionPoint, "."), reservedAlias, reservedAlias)
}
}
if parentType != queryObjectName && parentType != mutationObjectName && ctx.IsBoundary[parentType] && selection.Name == IdFieldName {
selectionSetResult = append(selectionSetResult, selection)
continue
}
loc, err := ctx.Locations.URLFor(parentType, location, selection.Name)
// Errors are returned for unmapped namespace/interface locations (needs refactor)
if err == nil && loc != location {
// field transitions to another service location
remoteSelections = append(remoteSelections, selection)
} else if selection.SelectionSet == nil {
// field is a leaf type in the current service
selectionSetResult = append(selectionSetResult, selection)
} else {
// field is a composite type in the current service
selectionSet, childrenSteps, err := extractSelectionSet(
ctx,
append(insertionPoint, selection.Alias),
selection.Definition.Type.Name(),
selection.SelectionSet,
location,
)
if err != nil {
return nil, nil, err
}
newField := *selection
newField.SelectionSet = selectionSet
selectionSetResult = append(selectionSetResult, &newField)
childrenStepsResult = append(childrenStepsResult, childrenSteps...)
}
case *ast.InlineFragment:
selectionSet, childrenSteps, err := extractSelectionSet(
ctx,
insertionPoint,
selection.TypeCondition,
selection.SelectionSet,
location,
)
if err != nil {
return nil, nil, err
}
inlineFragment := *selection
inlineFragment.SelectionSet = selectionSet
selectionSetResult = append(selectionSetResult, &inlineFragment)
childrenStepsResult = append(childrenStepsResult, childrenSteps...)
case *ast.FragmentSpread:
selectionSet, childrenSteps, err := extractSelectionSet(
ctx,
insertionPoint,
selection.Definition.TypeCondition,
selection.Definition.SelectionSet,
location,
)
if err != nil {
return nil, nil, err
}
inlineFragment := ast.InlineFragment{
TypeCondition: selection.Definition.TypeCondition,
SelectionSet: selectionSet,
}
selectionSetResult = append(selectionSetResult, &inlineFragment)
childrenStepsResult = append(childrenStepsResult, childrenSteps...)
default:
return nil, nil, fmt.Errorf("unexpected %T in SelectionSet", selection)
}
}
if len(remoteSelections) > 0 {
// Create child steps for all remote field selections
childrenSteps, err := createSteps(ctx, insertionPoint, parentType, location, remoteSelections)
if err != nil {
return nil, nil, err
}
childrenStepsResult = append(childrenStepsResult, childrenSteps...)
}
if len(childrenStepsResult) > 1 {
// Merge steps targeting distinct service/path locations
mergedSteps := []*QueryPlanStep{}
mergedStepsMap := map[string]*QueryPlanStep{}
for _, step := range childrenStepsResult {
key := strings.Join(append([]string{step.ServiceURL}, step.InsertionPoint...), "/")
if existingStep, ok := mergedStepsMap[key]; ok {
existingStep.SelectionSet = append(existingStep.SelectionSet, step.SelectionSet...)
existingStep.Then = append(existingStep.Then, step.Then...)
} else {
mergedStepsMap[key] = step
mergedSteps = append(mergedSteps, step)
}
}
childrenStepsResult = mergedSteps
}
parentDef := ctx.Schema.Types[parentType]
if parentDef == nil {
return nil, nil, fmt.Errorf("definition is nil for parentType %v", parentType)
}
if parentDef.IsAbstractType() {
// For abstract types, add an id fragment for all possible boundary
// implementations. This assures that abstract boundaries always return
// with an id, even if they didn't make a selection on the returned type
for implementationName, abstractTypes := range ctx.Schema.Implements {
if !ctx.IsBoundary[implementationName] {
continue
}
for _, abstractType := range abstractTypes {
if abstractType.Name != parentType {
continue
}
implementationType := ctx.Schema.Types[implementationName]
if idDef := implementationType.Fields.ForName(IdFieldName); idDef != nil {
possibleId := &ast.InlineFragment{
TypeCondition: implementationName,
SelectionSet: []ast.Selection{&ast.Field{Alias: "_bramble_id", Name: IdFieldName, Definition: idDef}},
ObjectDefinition: implementationType,
}
selectionSetResult = append(selectionSetResult, possibleId)
}
break
}
}
selectionSetResult = append(selectionSetResult, &ast.Field{
Alias: "_bramble__typename",
Name: "__typename",
Definition: &ast.FieldDefinition{Name: "__typename", Type: ast.NamedType("String", nil)},
})
} else if parentType != queryObjectName && parentType != mutationObjectName && ctx.IsBoundary[parentType] {
// Otherwise, add an id selection to all boundary types
if idDef := parentDef.Fields.ForName(IdFieldName); idDef != nil {
selectionSetResult = append(selectionSetResult,
&ast.Field{Alias: "_bramble_id", Name: IdFieldName, Definition: idDef},
&ast.Field{Alias: "_bramble__typename", Name: "__typename", Definition: &ast.FieldDefinition{Name: "__typename", Type: ast.NamedType("String", nil)}},
)
}
}
return selectionSetResult, childrenStepsResult, nil
}
func routeSelectionSet(ctx *PlanningContext, parentType string, parentLocation string, input ast.SelectionSet) (map[string]ast.SelectionSet, error) {
result := map[string]ast.SelectionSet{}
if parentLocation == "" {
// if we're at the root, we extract the selection set for each service
for _, svc := range ctx.Services {
loc := svc.ServiceURL
if parentLocation != "" && loc != parentLocation {
continue
}
ss := filterSelectionSetByLoc(ctx, input, loc, parentType)
if len(ss) > 0 {
result[loc] = ss
}
}
// filter fields living only on the gateway
if ss := filterSelectionSetByLoc(ctx, input, internalServiceName, parentType); len(ss) > 0 {
result[internalServiceName] = ss
}
return result, nil
}
for _, selection := range input {
switch selection := selection.(type) {
case *ast.Field:
if isGraphQLBuiltinName(selection.Name) && parentLocation == "" {
continue
}
loc, err := ctx.Locations.URLFor(parentType, parentLocation, selection.Name)
if err != nil {
return nil, err
}
result[loc] = append(result[loc], selection)
case *ast.InlineFragment:
inner, err := routeSelectionSet(ctx, parentType, parentLocation, selection.SelectionSet)
if err != nil {
return nil, err
}
for loc, selectionSet := range inner {
inlineFragment := *selection
inlineFragment.SelectionSet = selectionSet
result[loc] = append(result[loc], &inlineFragment)
}
case *ast.FragmentSpread:
inner, err := routeSelectionSet(ctx, parentType, parentLocation, selection.Definition.SelectionSet)
if err != nil {
return nil, err
}
for loc, selectionSet := range inner {
result[loc] = append(result[loc], selectionSet...)
}
}
}
return result, nil
}
func filterSelectionSetByLoc(ctx *PlanningContext, ss ast.SelectionSet, loc, parentType string) ast.SelectionSet {
var res ast.SelectionSet
for _, selection := range selectionSetToFields(ss) {
fieldLocation, err := ctx.Locations.URLFor(parentType, "", selection.Name)
if err != nil {
// Namespace
subSS := filterSelectionSetByLoc(ctx, selection.SelectionSet, loc, selection.Definition.Type.Name())
if len(subSS) == 0 {
continue
}
s := *selection
s.SelectionSet = subSS
res = append(res, &s)
} else if fieldLocation == loc {
res = append(res, selection)
} else if loc == internalServiceName && selection.Name == "__typename" {
// __typename fields on namespaces
res = append(res, selection)
}
}
return res
}
// FieldURLMap maps fields to service URLs
type FieldURLMap map[string]string
// URLFor returns the URL for the given field
func (m FieldURLMap) URLFor(parent, parentLocation, field string) (string, error) {
if field == "__typename" {
return parentLocation, nil
}
key := m.keyFor(parent, field)
value, exists := m[key]
if !exists {
return "", fmt.Errorf("could not find location for %q", key)
}
return value, nil
}
// RegisterURL registers the location for the given field
func (m FieldURLMap) RegisterURL(parent string, field string, location string) {
key := m.keyFor(parent, field)
m[key] = location
}
func (m FieldURLMap) keyFor(parent string, field string) string {
return fmt.Sprintf("%s.%s", parent, field)
}
// BoundaryField contains the name and format for a boundary query
type BoundaryField struct {
Field string
// Name of the received id argument
Argument string
// Whether the query is in the array format
Array bool
}
// BoundaryFieldsMap is a mapping service -> type -> boundary query
type BoundaryFieldsMap map[string]map[string]BoundaryField
// RegisterField registers a boundary field
func (m BoundaryFieldsMap) RegisterField(serviceURL, typeName string, field string, argument string, array bool) {
if _, ok := m[serviceURL]; !ok {
m[serviceURL] = make(map[string]BoundaryField)
}
// We prefer to use the array based boundary lookup
_, exists := m[serviceURL][typeName]
if exists && !array {
return
}
m[serviceURL][typeName] = BoundaryField{Field: field, Argument: argument, Array: array}
}
// Query returns the boundary field for the given service and type
func (m BoundaryFieldsMap) Field(serviceURL, typeName string) (BoundaryField, error) {
serviceMap, ok := m[serviceURL]
if !ok {
return BoundaryField{}, fmt.Errorf("could not find BoundaryFieldsMap entry for service %s", serviceURL)
}
field, ok := serviceMap[typeName]
if !ok {
return BoundaryField{}, fmt.Errorf("could not find BoundaryFieldsMap entry for typeName %s", typeName)
}
return field, nil
}