forked from wundergraph/graphql-go-tools
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ast_interface_type_definition.go
182 lines (152 loc) · 5.68 KB
/
ast_interface_type_definition.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
package ast
import (
"bytes"
"sort"
"github.com/TykTechnologies/graphql-go-tools/v2/internal/pkg/unsafebytes"
"github.com/TykTechnologies/graphql-go-tools/v2/pkg/lexer/position"
)
// InterfaceTypeDefinition
// example:
//
// interface NamedEntity {
// name: String
// }
type InterfaceTypeDefinition struct {
Description Description // optional, describes the interface
InterfaceLiteral position.Position // interface
Name ByteSliceReference // e.g. NamedEntity
ImplementsInterfaces TypeList // e.g implements Bar & Baz
HasDirectives bool
Directives DirectiveList // optional, e.g. @foo
HasFieldDefinitions bool
FieldsDefinition FieldDefinitionList // optional, e.g. { name: String }
}
func (d *Document) InterfaceTypeDefinitionNameBytes(ref int) ByteSlice {
return d.Input.ByteSlice(d.InterfaceTypeDefinitions[ref].Name)
}
func (d *Document) InterfaceTypeDefinitionNameString(ref int) string {
return unsafebytes.BytesToString(d.Input.ByteSlice(d.InterfaceTypeDefinitions[ref].Name))
}
func (d *Document) InterfaceTypeDefinitionDescriptionBytes(ref int) ByteSlice {
if !d.InterfaceTypeDefinitions[ref].Description.IsDefined {
return nil
}
return d.Input.ByteSlice(d.InterfaceTypeDefinitions[ref].Description.Content)
}
func (d *Document) InterfaceTypeDefinitionImplementsInterface(definitionRef int, interfaceName ByteSlice) bool {
for _, iRef := range d.InterfaceTypeDefinitions[definitionRef].ImplementsInterfaces.Refs {
implements := d.ResolveTypeNameBytes(iRef)
if bytes.Equal(interfaceName, implements) {
return true
}
}
return false
}
func (d *Document) InterfaceTypeDefinitionDescriptionString(ref int) string {
return unsafebytes.BytesToString(d.InterfaceTypeDefinitionDescriptionBytes(ref))
}
// InterfaceTypeDefinitionImplementedByRootNodes will return all RootNodes that implement the given interface type (by ref)
func (d *Document) InterfaceTypeDefinitionImplementedByRootNodes(ref int) []Node {
interfaceTypeName := d.InterfaceTypeDefinitionNameBytes(ref)
implementingRootNodes := make(map[Node]bool)
for i := 0; i < len(d.RootNodes); i++ {
if d.RootNodes[i].Kind == NodeKindInterfaceTypeDefinition && d.RootNodes[i].Ref == ref {
continue
}
var rootNodeInterfaceRefs []int
switch d.RootNodes[i].Kind {
case NodeKindObjectTypeDefinition:
if len(d.ObjectTypeDefinitions[d.RootNodes[i].Ref].ImplementsInterfaces.Refs) == 0 {
continue
}
rootNodeInterfaceRefs = d.ObjectTypeDefinitions[d.RootNodes[i].Ref].ImplementsInterfaces.Refs
case NodeKindInterfaceTypeDefinition:
if len(d.InterfaceTypeDefinitions[d.RootNodes[i].Ref].ImplementsInterfaces.Refs) == 0 {
continue
}
rootNodeInterfaceRefs = d.InterfaceTypeDefinitions[d.RootNodes[i].Ref].ImplementsInterfaces.Refs
default:
continue
}
for j := 0; j < len(rootNodeInterfaceRefs); j++ {
implementedInterfaceTypeName := d.TypeNameBytes(rootNodeInterfaceRefs[j])
if !interfaceTypeName.Equals(implementedInterfaceTypeName) {
continue
}
var typeName ByteSlice
switch d.RootNodes[i].Kind {
case NodeKindObjectTypeDefinition:
typeName = d.ObjectTypeDefinitionNameBytes(d.RootNodes[i].Ref)
case NodeKindInterfaceTypeDefinition:
typeName = d.InterfaceTypeDefinitionNameBytes(d.RootNodes[i].Ref)
}
node, exists := d.Index.FirstNodeByNameBytes(typeName)
if !exists {
continue
}
_, isAlreadyAdded := implementingRootNodes[node]
if isAlreadyAdded {
continue
}
implementingRootNodes[node] = true
}
}
var nodes []Node
for mapNode := range implementingRootNodes {
nodes = append(nodes, mapNode)
}
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].Ref < nodes[j].Ref
})
return nodes
}
func (d *Document) AddInterfaceTypeDefinition(definition InterfaceTypeDefinition) (ref int) {
d.InterfaceTypeDefinitions = append(d.InterfaceTypeDefinitions, definition)
return len(d.InterfaceTypeDefinitions) - 1
}
func (d *Document) ImportInterfaceTypeDefinition(name, description string, fieldRefs []int) (ref int) {
return d.ImportInterfaceTypeDefinitionWithDirectives(name, description, fieldRefs, nil, nil)
}
func (d *Document) ImportInterfaceTypeDefinitionWithDirectives(name, description string, fieldRefs []int, iRefs []int, directiveRefs []int) (ref int) {
definition := InterfaceTypeDefinition{
Name: d.Input.AppendInputString(name),
Description: d.ImportDescription(description),
FieldsDefinition: FieldDefinitionList{
Refs: fieldRefs,
},
ImplementsInterfaces: TypeList{
Refs: iRefs,
},
HasFieldDefinitions: len(fieldRefs) > 0,
HasDirectives: len(directiveRefs) > 0,
Directives: DirectiveList{
Refs: directiveRefs,
},
}
ref = d.AddInterfaceTypeDefinition(definition)
d.ImportRootNode(ref, NodeKindInterfaceTypeDefinition)
return
}
func (d *Document) InterfaceTypeDefinitionFieldWithName(ref int, fieldName []byte) (fieldDefRef int, ok bool) {
for _, fieldDefinitionRef := range d.InterfaceTypeDefinitions[ref].FieldsDefinition.Refs {
currentFieldName := d.FieldDefinitionNameBytes(fieldDefinitionRef)
if currentFieldName.Equals(fieldName) {
return fieldDefinitionRef, true
}
}
return InvalidRef, false
}
func (d *Document) InterfaceTypeDefinitionImplementedByObjectWithNames(interfaceDefRef int) (typeNames []string, ok bool) {
implementedByNodes := d.InterfaceTypeDefinitionImplementedByRootNodes(interfaceDefRef)
typeNames = make([]string, 0, len(implementedByNodes))
for _, implementedByNode := range implementedByNodes {
if implementedByNode.Kind != NodeKindObjectTypeDefinition {
continue
}
typeNames = append(typeNames, implementedByNode.NameString(d))
}
if len(typeNames) > 0 {
return typeNames, true
}
return nil, false
}