-
Notifications
You must be signed in to change notification settings - Fork 118
/
abstract_selection_rewriter_helpers.go
352 lines (285 loc) · 11 KB
/
abstract_selection_rewriter_helpers.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
package plan
import (
"slices"
"sort"
"github.com/wundergraph/graphql-go-tools/v2/pkg/ast"
)
func (r *fieldSelectionRewriter) datasourceHasEntitiesWithName(typeNames []string) (entityNames []string, ok bool) {
hasEntities := false
for _, typeName := range typeNames {
if r.dsConfiguration.HasEntity(typeName) {
hasEntities = true
break
}
}
if !hasEntities {
return nil, false
}
entityNames = make([]string, 0, len(typeNames))
for _, typeName := range typeNames {
if r.dsConfiguration.HasRootNodeWithTypename(typeName) {
entityNames = append(entityNames, typeName)
}
}
sort.Strings(entityNames)
return entityNames, true
}
func (r *fieldSelectionRewriter) entitiesImplementingInterface(typesImplementingInterface []string, entityNames []string) (out []string) {
if len(typesImplementingInterface) == 0 {
return nil
}
for _, typeName := range typesImplementingInterface {
if slices.Contains(entityNames, typeName) {
out = append(out, typeName)
}
}
return entityNames
}
func (r *fieldSelectionRewriter) entityNamesWithoutFragments(inlineFragments []inlineFragmentSelection, entityNames []string) []string {
entityNamesNotIncludedInFragments := make([]string, 0, len(entityNames))
for _, typeName := range entityNames {
idx := slices.IndexFunc(inlineFragments, func(fragmentSelection inlineFragmentSelection) bool {
return fragmentSelection.typeName == typeName
})
if idx == -1 {
entityNamesNotIncludedInFragments = append(entityNamesNotIncludedInFragments, typeName)
}
}
if len(entityNamesNotIncludedInFragments) > 0 {
return entityNamesNotIncludedInFragments
}
return nil
}
func (r *fieldSelectionRewriter) entityNamesWithFragments(inlineFragments []inlineFragmentSelection, entityNames []string) []string {
entityNamesWithFragments := make([]string, 0, len(entityNames))
for _, typeName := range entityNames {
idx := slices.IndexFunc(inlineFragments, func(fragmentSelection inlineFragmentSelection) bool {
return fragmentSelection.typeName == typeName
})
if idx != -1 {
entityNamesWithFragments = append(entityNamesWithFragments, typeName)
}
}
if len(entityNamesWithFragments) > 0 {
return entityNamesWithFragments
}
return nil
}
func (r *fieldSelectionRewriter) allEntitiesImplementsInterfaces(inlineFragmentsOnInterfaces []inlineFragmentSelectionOnInterface, entityNames []string) bool {
for _, inlineFragmentsOnInterface := range inlineFragmentsOnInterfaces {
entitiesImplementingInterface := r.entitiesImplementingInterface(inlineFragmentsOnInterface.typeNamesImplementingInterfaceInCurrentDS, entityNames)
if len(entitiesImplementingInterface) == 0 {
continue
}
if !r.allEntitiesHaveFieldsAsRootNode(entitiesImplementingInterface, inlineFragmentsOnInterface.selectionSetInfo.fields) {
return false
}
}
return true
}
func (r *fieldSelectionRewriter) allEntityFragmentsSatisfyInterfaces(inlineFragmentsOnInterfaces []inlineFragmentSelectionOnInterface, inlineFragmentsOnObjects []inlineFragmentSelection, entityNames []string) bool {
for _, inlineFragmentsOnInterface := range inlineFragmentsOnInterfaces {
entitiesImplementingInterface := r.entitiesImplementingInterface(inlineFragmentsOnInterface.typeNamesImplementingInterfaceInCurrentDS, entityNames)
if len(entitiesImplementingInterface) == 0 {
continue
}
entityFragments, _ := r.filterFragmentsByTypeNames(inlineFragmentsOnObjects, entitiesImplementingInterface)
if len(entityFragments) > 0 {
for _, entityFragment := range entityFragments {
satisfies := r.inlineFragmentHasAllFieldsLocalToDatasource(entityFragment, inlineFragmentsOnInterface.selectionSetInfo.fields) ||
r.entityHasFieldsAsRootNode(entityFragment.typeName, inlineFragmentsOnInterface.selectionSetInfo.fields)
if !satisfies {
return false
}
}
}
}
return true
}
func (r *fieldSelectionRewriter) entityHasFieldsAsRootNode(entityName string, fields []fieldSelection) bool {
for _, fieldSelection := range fields {
if !r.dsConfiguration.HasRootNode(entityName, fieldSelection.fieldName) {
return false
}
}
return true
}
func (r *fieldSelectionRewriter) allEntitiesHaveFieldsAsRootNode(entityNames []string, fields []fieldSelection) bool {
for _, entityName := range entityNames {
if !r.entityHasFieldsAsRootNode(entityName, fields) {
return false
}
}
return true
}
func (r *fieldSelectionRewriter) allFragmentTypesExistsOnDatasource(inlineFragments []inlineFragmentSelection) bool {
for _, inlineFragment := range inlineFragments {
if !r.hasTypeOnDataSource(inlineFragment.typeName) {
return false
}
}
return true
}
func (r *fieldSelectionRewriter) interfaceFragmentsRequiresCleanup(inlineFragments []inlineFragmentSelectionOnInterface, parentSelectionValidTypes []string) bool {
for _, fragment := range inlineFragments {
if r.interfaceFragmentNeedCleanup(fragment, parentSelectionValidTypes) {
return true
}
}
return false
}
func (r *fieldSelectionRewriter) objectFragmentNeedCleanup(inlineFragment inlineFragmentSelection) bool {
if !r.hasTypeOnDataSource(inlineFragment.typeName) {
return true
}
for _, fragmentOnInterface := range inlineFragment.selectionSetInfo.inlineFragmentsOnInterfaces {
if r.interfaceFragmentNeedCleanup(fragmentOnInterface, []string{inlineFragment.typeName}) {
return true
}
}
return false
}
func (r *fieldSelectionRewriter) interfaceFragmentNeedCleanup(inlineFragment inlineFragmentSelectionOnInterface, parentSelectionValidTypes []string) bool {
// check that interface type exists on datasource
if !r.hasTypeOnDataSource(inlineFragment.typeName) {
return true
}
if !inlineFragment.selectionSetInfo.hasInlineFragmentsOnInterfaces && !inlineFragment.selectionSetInfo.hasInlineFragmentsOnObjects {
return false
}
// if interface fragment has inline fragments on objects
// check that object type is present within parent selection valid types - e.g. members of union or parent interface
// check each fragment for the presence of other interface fragments
if inlineFragment.selectionSetInfo.hasInlineFragmentsOnObjects {
for _, fragmentOnObject := range inlineFragment.selectionSetInfo.inlineFragmentsOnObjects {
if !slices.Contains(parentSelectionValidTypes, fragmentOnObject.typeName) {
return true
}
if r.objectFragmentNeedCleanup(fragmentOnObject) {
return true
}
}
}
// if interface fragment has inline fragments on interfaces
// recursively check each fragment for the presence of other interface fragments with the same parent selection valid types
if inlineFragment.selectionSetInfo.hasInlineFragmentsOnInterfaces {
for _, fragmentOnInterface := range inlineFragment.selectionSetInfo.inlineFragmentsOnInterfaces {
if r.interfaceFragmentNeedCleanup(fragmentOnInterface, parentSelectionValidTypes) {
return true
}
}
}
if inlineFragment.selectionSetInfo.hasFields {
for _, typeName := range inlineFragment.typeNamesImplementingInterfaceInCurrentDS {
if !r.typeHasAllFieldLocal(typeName, inlineFragment.selectionSetInfo.fields) {
return true
}
}
}
return false
}
func (r *fieldSelectionRewriter) typeHasAllFieldLocal(typeName string, fields []fieldSelection) bool {
for _, field := range fields {
if !r.hasFieldOnDataSource(typeName, field.fieldName) {
return false
}
}
return true
}
func (r *fieldSelectionRewriter) allFragmentTypesImplementsInterfaceTypes(inlineFragments []inlineFragmentSelection, interfaceTypes []string) bool {
for _, inlineFragment := range inlineFragments {
if !slices.Contains(interfaceTypes, inlineFragment.typeName) {
return false
}
}
return true
}
func (r *fieldSelectionRewriter) filterFragmentsByTypeNames(inlineFragments []inlineFragmentSelection, typeNames []string) (fragments []inlineFragmentSelection, missingTypes []string) {
fragments = make([]inlineFragmentSelection, 0, len(typeNames))
for _, typeName := range typeNames {
idx := slices.IndexFunc(inlineFragments, func(fragmentSelection inlineFragmentSelection) bool {
return fragmentSelection.typeName == typeName
})
if idx != -1 {
fragments = append(fragments, inlineFragments[idx])
} else {
missingTypes = append(missingTypes, typeName)
}
}
if len(fragments) > 0 {
return fragments, missingTypes
}
return nil, missingTypes
}
func (r *fieldSelectionRewriter) notSelectedFieldsForInlineFragment(inlineFragmentSelection inlineFragmentSelection, fields []fieldSelection) []fieldSelection {
notSelectedFields := make([]fieldSelection, 0, len(fields))
for _, fieldSelection := range fields {
fieldIsSelected := false
for _, fragmentField := range inlineFragmentSelection.selectionSetInfo.fields {
if fieldSelection.fieldName == fragmentField.fieldName {
fieldIsSelected = true
break
}
}
if !fieldIsSelected {
notSelectedFields = append(notSelectedFields, fieldSelection)
}
}
return notSelectedFields
}
func (r *fieldSelectionRewriter) inlineFragmentHasAllFieldsLocalToDatasource(inlineFragmentSelection inlineFragmentSelection, fields []fieldSelection) bool {
notSelectedFields := r.notSelectedFieldsForInlineFragment(inlineFragmentSelection, fields)
if len(notSelectedFields) == 0 {
return true
}
for _, notSelectedField := range notSelectedFields {
hasField := r.hasFieldOnDataSource(inlineFragmentSelection.typeName, notSelectedField.fieldName)
if !hasField {
return false
}
}
return true
}
func (r *fieldSelectionRewriter) hasTypeOnDataSource(typeName string) bool {
return r.dsConfiguration.HasRootNodeWithTypename(typeName) ||
r.dsConfiguration.HasChildNodeWithTypename(typeName)
}
func (r *fieldSelectionRewriter) hasFieldOnDataSource(typeName string, fieldName string) bool {
return r.dsConfiguration.HasRootNode(typeName, fieldName) ||
r.dsConfiguration.HasChildNode(typeName, fieldName)
}
func (r *fieldSelectionRewriter) createFragmentSelection(typeName string, fields []fieldSelection, addTypeName bool) (selectionRef int) {
selectionRefs := make([]int, 0, len(fields))
for _, sharedField := range fields {
newFieldSelectionRef := r.operation.CopySelection(sharedField.fieldSelectionRef)
selectionRefs = append(selectionRefs, newFieldSelectionRef)
}
if addTypeName {
typeNameSelectionRef, _ := r.typeNameSelection()
selectionRefs = append(selectionRefs, typeNameSelectionRef)
}
selectionSetRef := r.operation.AddSelectionSetToDocument(ast.SelectionSet{
SelectionRefs: selectionRefs,
})
inlineFragment := ast.InlineFragment{
TypeCondition: ast.TypeCondition{
Type: r.operation.AddNamedType([]byte(typeName)),
},
SelectionSet: selectionSetRef,
HasSelections: true,
}
inlineFragmentRef := r.operation.AddInlineFragment(inlineFragment)
return r.operation.AddSelectionToDocument(ast.Selection{
Kind: ast.SelectionKindInlineFragment,
Ref: inlineFragmentRef,
})
}
func (r *fieldSelectionRewriter) typeNameSelection() (selectionRef int, fieldRef int) {
field := r.operation.AddField(ast.Field{
Name: r.operation.Input.AppendInputString("__typename"),
})
return r.operation.AddSelectionToDocument(ast.Selection{
Ref: field.Ref,
Kind: ast.SelectionKindField,
}), field.Ref
}