-
Notifications
You must be signed in to change notification settings - Fork 128
/
ast_operation_definition.go
126 lines (108 loc) · 3.99 KB
/
ast_operation_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
package ast
import (
"math"
"github.com/wundergraph/graphql-go-tools/internal/pkg/unsafebytes"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/position"
)
type OperationType int
const (
OperationTypeUnknown OperationType = iota
OperationTypeQuery
OperationTypeMutation
OperationTypeSubscription
)
type OperationDefinition struct {
OperationType OperationType // one of query, mutation, subscription
OperationTypeLiteral position.Position // position of the operation type literal, if present
Name ByteSliceReference // optional, user defined name of the operation
HasVariableDefinitions bool
VariableDefinitions VariableDefinitionList // optional, e.g. ($devicePicSize: Int)
HasDirectives bool
Directives DirectiveList // optional, e.g. @foo
SelectionSet int // e.g. {field}
HasSelections bool
}
func (d *Document) OperationDefinitionHasVariableDefinition(ref int, variableName string) bool {
for _, i := range d.OperationDefinitions[ref].VariableDefinitions.Refs {
value := d.VariableDefinitions[i].VariableValue.Ref
if variableName == d.VariableValueNameString(value) {
return true
}
}
return false
}
func (d *Document) OperationDefinitionNameBytes(ref int) ByteSlice {
return d.Input.ByteSlice(d.OperationDefinitions[ref].Name)
}
func (d *Document) OperationDefinitionNameString(ref int) string {
return unsafebytes.BytesToString(d.Input.ByteSlice(d.OperationDefinitions[ref].Name))
}
func (d *Document) AddOperationDefinitionToRootNodes(definition OperationDefinition) Node {
d.OperationDefinitions = append(d.OperationDefinitions, definition)
node := Node{Kind: NodeKindOperationDefinition, Ref: len(d.OperationDefinitions) - 1}
d.RootNodes = append(d.RootNodes, node)
return node
}
func (d *Document) AddVariableDefinitionToOperationDefinition(operationDefinitionRef, variableValueRef, typeRef int) {
if !d.OperationDefinitions[operationDefinitionRef].HasVariableDefinitions {
d.OperationDefinitions[operationDefinitionRef].HasVariableDefinitions = true
d.OperationDefinitions[operationDefinitionRef].VariableDefinitions.Refs = d.Refs[d.NextRefIndex()][:0]
}
variableDefinition := VariableDefinition{
VariableValue: Value{
Kind: ValueKindVariable,
Ref: variableValueRef,
},
Type: typeRef,
}
d.VariableDefinitions = append(d.VariableDefinitions, variableDefinition)
ref := len(d.VariableDefinitions) - 1
d.OperationDefinitions[operationDefinitionRef].VariableDefinitions.Refs =
append(d.OperationDefinitions[operationDefinitionRef].VariableDefinitions.Refs, ref)
}
func (d *Document) AddImportedVariableDefinitionToOperationDefinition(operationDefinition, variableDefinition int) {
if !d.OperationDefinitions[operationDefinition].HasVariableDefinitions {
d.OperationDefinitions[operationDefinition].HasVariableDefinitions = true
d.OperationDefinitions[operationDefinition].VariableDefinitions.Refs = d.Refs[d.NextRefIndex()][:0]
}
d.OperationDefinitions[operationDefinition].VariableDefinitions.Refs =
append(d.OperationDefinitions[operationDefinition].VariableDefinitions.Refs, variableDefinition)
}
func (d *Document) OperationNameExists(operationName string) bool {
for i := range d.RootNodes {
if d.RootNodes[i].Kind != NodeKindOperationDefinition {
continue
}
if d.OperationDefinitionNameString(d.RootNodes[i].Ref) == operationName {
return true
}
}
return false
}
func (d *Document) NumOfOperationDefinitions() (n int) {
for i := range d.RootNodes {
if d.RootNodes[i].Kind == NodeKindOperationDefinition {
n++
}
}
return
}
const (
alphabet = `abcdefghijklmnopqrstuvwxyz`
)
func (d *Document) GenerateUnusedVariableDefinitionName(operationDefinition int) []byte {
var i, k int64
for i = 1; i < math.MaxInt64; i++ {
out := make([]byte, i)
for j := range alphabet {
for k = 0; k < i; k++ {
out[k] = alphabet[j]
}
_, exists := d.VariableDefinitionByNameAndOperation(operationDefinition, out)
if !exists {
return out
}
}
}
return nil
}