-
Notifications
You must be signed in to change notification settings - Fork 121
/
ast_fragment_spread.go
80 lines (72 loc) · 3.51 KB
/
ast_fragment_spread.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
package ast
import (
"github.com/wundergraph/graphql-go-tools/pkg/internal/unsafebytes"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/position"
)
// FragmentSpread
// example:
// ...MyFragment
type FragmentSpread struct {
Spread position.Position // ...
FragmentName ByteSliceReference // Name but not on, e.g. MyFragment
HasDirectives bool
Directives DirectiveList // optional, e.g. @foo
}
func (d *Document) CopyFragmentSpread(ref int) int {
var directives DirectiveList
if d.FragmentSpreads[ref].HasDirectives {
directives = d.CopyDirectiveList(d.FragmentSpreads[ref].Directives)
}
return d.AddFragmentSpread(FragmentSpread{
FragmentName: d.copyByteSliceReference(d.FragmentSpreads[ref].FragmentName),
HasDirectives: d.FragmentSpreads[ref].HasDirectives,
Directives: directives,
})
}
func (d *Document) AddFragmentSpread(spread FragmentSpread) int {
d.FragmentSpreads = append(d.FragmentSpreads, spread)
return len(d.FragmentSpreads) - 1
}
func (d *Document) FragmentSpreadNameBytes(ref int) ByteSlice {
return d.Input.ByteSlice(d.FragmentSpreads[ref].FragmentName)
}
func (d *Document) FragmentSpreadNameString(ref int) string {
return unsafebytes.BytesToString(d.FragmentSpreadNameBytes(ref))
}
// ReplaceFragmentSpread replaces a fragment spread with a given selection set
// attention! this might lead to duplicate field problems because the same field with its unique field reference might be copied into the same selection set
// possible problems: changing directives or sub selections will affect both fields with the same id
// simple solution: run normalization deduplicate fields
// as part of the normalization flow this problem will be handled automatically
// just be careful in case you use this function outside of the normalization package
func (d *Document) ReplaceFragmentSpread(selectionSet int, spreadRef int, replaceWithSelectionSet int) {
for i, j := range d.SelectionSets[selectionSet].SelectionRefs {
if d.Selections[j].Kind == SelectionKindFragmentSpread && d.Selections[j].Ref == spreadRef {
d.SelectionSets[selectionSet].SelectionRefs = append(d.SelectionSets[selectionSet].SelectionRefs[:i], append(d.SelectionSets[replaceWithSelectionSet].SelectionRefs, d.SelectionSets[selectionSet].SelectionRefs[i+1:]...)...)
d.Index.ReplacedFragmentSpreads = append(d.Index.ReplacedFragmentSpreads, spreadRef)
return
}
}
}
// ReplaceFragmentSpreadWithInlineFragment replaces a given fragment spread with a inline fragment
// attention! the same rules apply as for 'ReplaceFragmentSpread', look above!
func (d *Document) ReplaceFragmentSpreadWithInlineFragment(selectionSet int, spreadRef int, replaceWithSelectionSet int, typeCondition TypeCondition) {
d.InlineFragments = append(d.InlineFragments, InlineFragment{
TypeCondition: typeCondition,
SelectionSet: replaceWithSelectionSet,
HasSelections: len(d.SelectionSets[replaceWithSelectionSet].SelectionRefs) != 0,
})
ref := len(d.InlineFragments) - 1
d.Selections = append(d.Selections, Selection{
Kind: SelectionKindInlineFragment,
Ref: ref,
})
selectionRef := len(d.Selections) - 1
for i, j := range d.SelectionSets[selectionSet].SelectionRefs {
if d.Selections[j].Kind == SelectionKindFragmentSpread && d.Selections[j].Ref == spreadRef {
d.SelectionSets[selectionSet].SelectionRefs = append(d.SelectionSets[selectionSet].SelectionRefs[:i], append([]int{selectionRef}, d.SelectionSets[selectionSet].SelectionRefs[i+1:]...)...)
d.Index.ReplacedFragmentSpreads = append(d.Index.ReplacedFragmentSpreads, spreadRef)
return
}
}
}