-
Notifications
You must be signed in to change notification settings - Fork 55
/
merge.go
457 lines (394 loc) · 11.4 KB
/
merge.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package bramble
import (
"fmt"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
)
// MergeSchemas merges the provided schemas together
func MergeSchemas(schemas ...*ast.Schema) (*ast.Schema, error) {
if len(schemas) < 1 {
return nil, fmt.Errorf("no source schemas")
}
if len(schemas) == 1 {
// if we have only one schema we append a minimal schema so that we can
// still go through the merging logic and prune special types (e.g.
// Service)
schemas = append(schemas, gqlparser.MustLoadSchema(&ast.Source{Name: "empty schema", Input: `
type Service {
name: String!
version: String!
schema: String!
}
type Query {
service: Service!
}
`}))
}
merged := ast.Schema{
Types: make(map[string]*ast.Definition),
Directives: make(map[string]*ast.DirectiveDefinition),
PossibleTypes: make(map[string][]*ast.Definition),
}
merged.Types = schemas[0].Types
for _, schema := range schemas[1:] {
mergedTypes, err := mergeTypes(merged.Types, schema.Types)
if err != nil {
return nil, err
}
merged.Types = mergedTypes
}
merged.Implements = mergeImplements(schemas)
merged.PossibleTypes = mergePossibleTypes(schemas, merged.Types)
merged.Directives = mergeDirectives(schemas)
merged.Query = merged.Types[queryObjectName]
merged.Mutation = merged.Types[mutationObjectName]
merged.Subscription = merged.Types[subscriptionObjectName]
return &merged, nil
}
func buildFieldURLMap(services ...*Service) FieldURLMap {
result := FieldURLMap{}
for _, rs := range services {
for _, t := range rs.Schema.Types {
if !t.IsCompositeType() || isGraphQLBuiltinName(t.Name) || t.Name == serviceObjectName {
continue
}
for _, f := range mergeableFields(t) {
if isBoundaryObject(t) && isIDField(f) {
continue
}
// namespace objects live only on the graph
fieldType := rs.Schema.Types[f.Type.Name()]
if isNamespaceObject(fieldType) {
continue
}
if isBoundaryField(f) {
continue
}
result.RegisterURL(t.Name, f.Name, rs.ServiceURL)
}
}
}
return result
}
func buildIsBoundaryMap(services ...*Service) map[string]bool {
result := map[string]bool{}
for _, rs := range services {
for _, t := range rs.Schema.Types {
if t.Kind != ast.Object || isGraphQLBuiltinName(t.Name) || t.Name == serviceObjectName {
continue
}
result[t.Name] = isBoundaryObject(t)
}
}
return result
}
func buildBoundaryFieldsMap(services ...*Service) BoundaryFieldsMap {
result := make(BoundaryFieldsMap)
for _, rs := range services {
for _, f := range rs.Schema.Query.Fields {
if isBoundaryField(f) {
typeName := f.Type.Name()
array := false
if f.Type.Elem != nil {
typeName = f.Type.Elem.Name()
array = true
}
result.RegisterField(rs.ServiceURL, typeName, f.Name, f.Arguments[0].Name, array)
}
}
}
return result
}
func mergeTypes(a, b map[string]*ast.Definition) (map[string]*ast.Definition, error) {
result := make(map[string]*ast.Definition)
for k, v := range a {
if k == nodeInterfaceName || k == serviceObjectName {
continue
}
newV := *v
newV.Interfaces = cleanInterfaces(v.Interfaces)
newV.Directives = cleanDirectives(v.Directives)
newV.Fields = cleanFields(v.Fields)
result[k] = &newV
}
if b == nil {
return result, nil
}
for k, vb := range b {
if isGraphQLBuiltinName(k) || k == nodeInterfaceName || k == serviceObjectName {
continue
}
newVB := *vb
newVB.Interfaces = cleanInterfaces(vb.Interfaces)
newVB.Directives = cleanDirectives(vb.Directives)
newVB.Fields = cleanFields(vb.Fields)
va, found := result[k]
if !found {
result[k] = &newVB
continue
}
if newVB.Kind != va.Kind {
return nil, fmt.Errorf("name collision: %s(%s) conflicts with %s(%s)", newVB.Name, newVB.Kind, va.Name, va.Kind)
}
if newVB.Kind == ast.Scalar {
result[k] = &newVB
continue
}
if !hasFederationDirectives(&newVB) || !hasFederationDirectives(va) {
if k != queryObjectName && k != mutationObjectName {
if newVB.Kind == ast.Interface {
return nil, fmt.Errorf("conflicting interface: %s (interfaces may not span multiple services)", k)
}
return nil, fmt.Errorf("conflicting non boundary type: %s", k)
}
}
if isBoundaryObject(va) != isBoundaryObject(&newVB) || isNamespaceObject(va) != isNamespaceObject(&newVB) {
return nil, fmt.Errorf("conflicting object directives, merged objects %q should both be boundary or namespaces", newVB.Name)
}
// now, either it's boundary type, namespace type or the Query/Mutation type
if va.Kind != ast.Object {
return nil, fmt.Errorf("non object boundary type")
}
if isNamespaceObject(&newVB) || k == queryObjectName || k == mutationObjectName || k == subscriptionObjectName {
mergedObject, err := mergeNamespaceObjects(a, b, &newVB, va)
if err != nil {
return nil, err
}
result[k] = mergedObject
continue
}
mergedBoundaryObject, err := mergeBoundaryObjects(&newVB, va)
if err != nil {
return nil, err
}
var newInterfaces []string
for _, i := range mergedBoundaryObject.Interfaces {
if i == nodeInterfaceName {
continue
}
newInterfaces = append(newInterfaces, i)
}
mergedBoundaryObject.Interfaces = newInterfaces
result[k] = mergedBoundaryObject
}
return result, nil
}
func mergeImplements(sources []*ast.Schema) map[string][]*ast.Definition {
result := map[string][]*ast.Definition{}
for _, schema := range sources {
for typeName, interfaces := range schema.Implements {
for _, i := range interfaces {
if i.Name != nodeInterfaceName {
result[typeName] = append(result[typeName], i)
}
}
}
}
return result
}
func mergeDirectives(sources []*ast.Schema) map[string]*ast.DirectiveDefinition {
result := map[string]*ast.DirectiveDefinition{}
for _, schema := range sources {
for directive, definition := range schema.Directives {
if allowedDirective(directive) {
result[directive] = definition
}
}
}
return result
}
func mergePossibleTypes(sources []*ast.Schema, mergedTypes map[string]*ast.Definition) map[string][]*ast.Definition {
result := map[string][]*ast.Definition{}
for _, schema := range sources {
for typeName, possibleTypes := range schema.PossibleTypes {
if typeName != serviceObjectName && typeName != nodeInterfaceName {
if _, ok := mergedTypes[typeName]; !ok {
continue
}
for _, possibleType := range possibleTypes {
if possibleType.Name != nodeInterfaceName {
if ast.DefinitionList(result[typeName]).ForName(possibleType.Name) == nil {
result[typeName] = append(result[typeName], mergedTypes[possibleType.Name])
}
}
}
}
}
}
return result
}
func mergeNamespaceObjects(aTypes, bTypes map[string]*ast.Definition, a, b *ast.Definition) (*ast.Definition, error) {
var fields ast.FieldList
for _, f := range a.Fields {
if isQueryType(a) && (isNodeField(f) || isServiceField(f)) {
continue
}
fields = append(fields, f)
}
for _, f := range mergeableFields(b) {
if rf := fields.ForName(f.Name); rf != nil {
if f.Type.String() == rf.Type.String() && f.Type.NonNull &&
isNamespaceObject(aTypes[rf.Type.Name()]) && isNamespaceObject(bTypes[f.Type.Name()]) &&
!hasIDField(aTypes[rf.Type.Name()]) && !hasIDField(bTypes[f.Type.Name()]) &&
len(f.Arguments) == 0 && len(rf.Arguments) == 0 {
continue
}
return nil, fmt.Errorf("overlapping namespace fields %s : %s", a.Name, f.Name)
}
fields = append(fields, f)
}
return &ast.Definition{
Kind: ast.Object,
Description: mergeDescriptions(a, b),
Name: a.Name,
Directives: a.Directives.ForNames(namespaceDirectiveName),
Interfaces: append(a.Interfaces, b.Interfaces...),
Fields: fields,
}, nil
}
func mergeBoundaryObjects(a, b *ast.Definition) (*ast.Definition, error) {
mergedFields, err := mergeBoundaryObjectFields(a, b)
if err != nil {
return nil, err
}
return &ast.Definition{
Kind: ast.Object,
Description: mergeDescriptions(a, b),
Name: a.Name,
Directives: a.Directives.ForNames(boundaryDirectiveName),
Interfaces: append(a.Interfaces, b.Interfaces...),
Fields: mergedFields,
}, nil
}
func mergeBoundaryObjectFields(a, b *ast.Definition) (ast.FieldList, error) {
var result ast.FieldList
for _, f := range a.Fields {
if isQueryType(a) && (isNodeField(f) || isServiceField(f)) {
continue
}
result = append(result, f)
}
for _, f := range mergeableFields(b) {
if isIDField(f) {
continue
}
if rf := result.ForName(f.Name); rf != nil {
return nil, fmt.Errorf("overlapping fields %s : %s", a.Name, f.Name)
}
result = append(result, f)
}
return result, nil
}
func mergeableFields(t *ast.Definition) ast.FieldList {
result := ast.FieldList{}
for _, f := range t.Fields {
if isGraphQLBuiltinName(f.Name) {
continue
}
if isQueryType(t) && (isNodeField(f) || isServiceField(f)) {
continue
}
result = append(result, f)
}
return result
}
func mergeDescriptions(a, b *ast.Definition) string {
if a.Description == "" {
return b.Description
}
if b.Description == "" {
return a.Description
}
return a.Description + "\n\n" + b.Description
}
func cleanInterfaces(interfaces []string) []string {
var res []string
for _, i := range interfaces {
if i == nodeInterfaceName {
continue
}
res = append(res, i)
}
return res
}
func cleanDirectives(directives ast.DirectiveList) ast.DirectiveList {
var res ast.DirectiveList
for _, d := range directives {
if allowedDirective(d.Name) {
res = append(res, d)
}
}
return res
}
func cleanFields(fields ast.FieldList) ast.FieldList {
var res ast.FieldList
for _, f := range fields {
if isBoundaryField(f) {
continue
}
f.Directives = cleanDirectives(f.Directives)
res = append(res, f)
}
return res
}
func allowedDirective(name string) bool {
switch name {
case boundaryDirectiveName, namespaceDirectiveName, "skip", "include", "deprecated":
return true
default:
return false
}
}
func hasIDField(t *ast.Definition) bool {
for _, f := range t.Fields {
if isIDField(f) {
return true
}
}
return false
}
func isNodeField(f *ast.FieldDefinition) bool {
if f.Name != nodeRootFieldName || len(f.Arguments) != 1 {
return false
}
arg := f.Arguments[0]
return arg.Name == IdFieldName &&
isIDType(arg.Type) &&
isNullableTypeNamed(f.Type, nodeInterfaceName)
}
func isIDField(f *ast.FieldDefinition) bool {
return f.Name == IdFieldName && len(f.Arguments) == 0 && isIDType(f.Type)
}
func isServiceField(f *ast.FieldDefinition) bool {
return f.Name == serviceRootFieldName &&
len(f.Arguments) == 0 &&
isNonNullableTypeNamed(f.Type, serviceObjectName)
}
func isQueryType(a *ast.Definition) bool {
return a.Name == queryObjectName
}
func isBoundaryObject(a *ast.Definition) bool {
return a.Directives.ForName(boundaryDirectiveName) != nil
}
func isBoundaryField(f *ast.FieldDefinition) bool {
return f.Directives.ForName(boundaryDirectiveName) != nil
}
func isNamespaceObject(a *ast.Definition) bool {
return a.Directives.ForName(namespaceDirectiveName) != nil
}
func hasFederationDirectives(o *ast.Definition) bool {
return isBoundaryObject(o) || isNamespaceObject(o)
}
func hasBoundaryDirective(f *ast.FieldDefinition) bool {
return f.Directives.ForName(boundaryDirectiveName) != nil
}
func filterBuiltinFields(fields ast.FieldList) ast.FieldList {
var res ast.FieldList
for _, f := range fields {
if isGraphQLBuiltinName(f.Name) {
continue
}
res = append(res, f)
}
return res
}