forked from wundergraph/graphql-go-tools
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.go
174 lines (150 loc) · 4.25 KB
/
index.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
package ast
import (
"bytes"
"github.com/cespare/xxhash/v2"
"github.com/TykTechnologies/graphql-go-tools/v2/internal/pkg/unsafebytes"
)
// Index is a struct to easily look up objects in a document, e.g. find Nodes (type/interface/union definitions) by name
type Index struct {
// QueryTypeName is the name of the query type on the schema Node
// schema { query: Query }
QueryTypeName ByteSlice
// MutationTypeName is the name of the mutation type on the schema Node
// schema { mutation: Mutation }
MutationTypeName ByteSlice
// SubscriptionTypeName is the name of the subscription type on the schema Node
// schema { subscription: Subscription }
SubscriptionTypeName ByteSlice
// nodes is a list of all root nodes in a schema definition
// The map key is the result of the xxhash algorithm from the Node name.
nodes map[uint64][]Node
// ReplacedFragmentSpreads is a list of references (slice indices) of all FragmentSpreads that got replaced during normalization.
ReplacedFragmentSpreads []int
// MergedTypeExtensions is a list of Nodes (Node kind + reference) that got merged during type extension merging.
MergedTypeExtensions []Node
}
// Reset empties the Index
func (i *Index) Reset() {
i.QueryTypeName = i.QueryTypeName[:0]
i.MutationTypeName = i.MutationTypeName[:0]
i.SubscriptionTypeName = i.SubscriptionTypeName[:0]
i.ReplacedFragmentSpreads = i.ReplacedFragmentSpreads[:0]
i.MergedTypeExtensions = i.MergedTypeExtensions[:0]
for j := range i.nodes {
delete(i.nodes, j)
}
}
func (i *Index) AddNodeStr(name string, node Node) {
hash := xxhash.Sum64String(name)
_, exists := i.nodes[hash]
if !exists {
i.nodes[hash] = []Node{node}
return
}
i.nodes[hash] = append(i.nodes[hash], node)
}
func (i *Index) AddNodeBytes(name []byte, node Node) {
hash := xxhash.Sum64(name)
_, exists := i.nodes[hash]
if !exists {
i.nodes[hash] = []Node{node}
return
}
i.nodes[hash] = append(i.nodes[hash], node)
}
func (i *Index) NodesByNameStr(name string) ([]Node, bool) {
hash := xxhash.Sum64String(name)
node, exists := i.nodes[hash]
return node, exists
}
func (i *Index) FirstNodeByNameStr(name string) (Node, bool) {
hash := xxhash.Sum64String(name)
node, exists := i.nodes[hash]
if !exists || len(node) == 0 {
return InvalidNode, false
}
return node[0], true
}
func (i *Index) NodesByNameBytes(name []byte) ([]Node, bool) {
hash := xxhash.Sum64(name)
node, exists := i.nodes[hash]
return node, exists
}
func (i *Index) FirstNodeByNameBytes(name []byte) (Node, bool) {
hash := xxhash.Sum64(name)
node, exists := i.nodes[hash]
if !exists || len(node) == 0 {
return InvalidNode, false
}
return node[0], true
}
func (i *Index) FirstNonExtensionNodeByNameBytes(name []byte) (Node, bool) {
hash := xxhash.Sum64(name)
nodes, exists := i.nodes[hash]
if !exists || len(nodes) == 0 {
return InvalidNode, false
}
for j := range nodes {
if nodes[j].IsExtensionKind() {
continue
}
return nodes[j], true
}
return InvalidNode, false
}
func (i *Index) RemoveNodeByName(name []byte) {
hash := xxhash.Sum64(name)
delete(i.nodes, hash)
if bytes.Equal(i.QueryTypeName, name) {
i.QueryTypeName = nil
}
if bytes.Equal(i.MutationTypeName, name) {
i.MutationTypeName = nil
}
if bytes.Equal(i.SubscriptionTypeName, name) {
i.SubscriptionTypeName = nil
}
}
func (i *Index) ReplaceNode(name []byte, oldNode Node, newNode Node) {
nodes, ok := i.nodes[xxhash.Sum64(name)]
if !ok {
return
}
for i := range nodes {
if nodes[i].Kind != oldNode.Kind || nodes[i].Ref != oldNode.Ref {
continue
}
nodes[i].Kind = newNode.Kind
nodes[i].Ref = newNode.Ref
}
}
func (i *Index) IsRootOperationTypeNameBytes(typeName []byte) bool {
if len(typeName) == 0 {
return false
}
if bytes.Equal(i.QueryTypeName, typeName) {
return true
}
if bytes.Equal(i.MutationTypeName, typeName) {
return true
}
if bytes.Equal(i.SubscriptionTypeName, typeName) {
return true
}
return false
}
func (i *Index) IsRootOperationTypeNameString(typeName string) bool {
if typeName == "" {
return false
}
if unsafebytes.BytesToString(i.QueryTypeName) == typeName {
return true
}
if unsafebytes.BytesToString(i.MutationTypeName) == typeName {
return true
}
if unsafebytes.BytesToString(i.SubscriptionTypeName) == typeName {
return true
}
return false
}