-
Notifications
You must be signed in to change notification settings - Fork 55
/
format.go
249 lines (214 loc) · 6.83 KB
/
format.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
package bramble
import (
"bytes"
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/formatter"
)
func indentPrefix(sb *strings.Builder, level int, suffix ...string) (int, error) {
sb.WriteString("\n")
var err error
total, count := 0, 0
for i := 0; i <= level; i++ {
count, err = sb.WriteString(" ")
total += count
if err != nil {
return total, err
}
}
for _, str := range suffix {
count, err = sb.WriteString(str)
total += count
if err != nil {
return total, err
}
}
return total, nil
}
func formatDocument(ctx context.Context, schema *ast.Schema, operationType string, selectionSet ast.SelectionSet) (string, map[string]interface{}) {
operation, vars := formatOperation(ctx, selectionSet)
return strings.ToLower(operationType) + " " + operation + formatSelectionSet(ctx, schema, selectionSet), vars
}
func formatOperation(ctx context.Context, selection ast.SelectionSet) (string, map[string]interface{}) {
sb := strings.Builder{}
if !graphql.HasOperationContext(ctx) {
return "", nil
}
operationCtx := graphql.GetOperationContext(ctx)
variables := selectionSetVariables(selection)
variableNames := map[string]struct{}{}
for _, s := range variables {
variableNames[s] = struct{}{}
}
var arguments []string
usedVariables := map[string]interface{}{}
for _, variableDefinition := range operationCtx.Operation.VariableDefinitions {
if _, exists := variableNames[variableDefinition.Variable]; !exists {
continue
}
for varName, varValue := range operationCtx.Variables {
if varName == variableDefinition.Variable {
usedVariables[varName] = varValue
}
}
argument := fmt.Sprintf("$%s: %s", variableDefinition.Variable, variableDefinition.Type.String())
arguments = append(arguments, argument)
}
sb.WriteString(operationCtx.OperationName)
if len(arguments) == 0 {
return sb.String(), nil
}
sb.WriteString("(")
sb.WriteString(strings.Join(arguments, ","))
sb.WriteString(")")
return sb.String(), usedVariables
}
func selectionSetVariables(selectionSet ast.SelectionSet) []string {
var vars []string
for _, s := range selectionSet {
switch selection := s.(type) {
case *ast.Field:
vars = append(vars, directiveListVariables(selection.Directives)...)
vars = append(vars, argumentListVariables(selection.Arguments)...)
vars = append(vars, selectionSetVariables(selection.SelectionSet)...)
case *ast.InlineFragment:
vars = append(vars, directiveListVariables(selection.Directives)...)
vars = append(vars, selectionSetVariables(selection.SelectionSet)...)
case *ast.FragmentSpread:
vars = append(vars, directiveListVariables(selection.Directives)...)
}
}
return vars
}
func directiveListVariables(directives ast.DirectiveList) []string {
var output []string
for _, d := range directives {
output = append(output, argumentListVariables(d.Arguments)...)
}
return output
}
func argumentListVariables(arguments ast.ArgumentList) []string {
var output []string
for _, a := range arguments {
output = append(output, valueVariables(a.Value)...)
}
return output
}
func valueVariables(a *ast.Value) []string {
var output []string
switch a.Kind {
case ast.Variable:
output = append(output, a.Raw)
default:
for _, child := range a.Children {
output = append(output, valueVariables(child.Value)...)
}
}
return output
}
func formatSelectionSelectionSet(sb *strings.Builder, schema *ast.Schema, vars map[string]interface{}, level int, selectionSet ast.SelectionSet) {
sb.WriteString(" {")
formatSelection(sb, schema, vars, level+1, selectionSet)
indentPrefix(sb, level, "}")
}
func formatSelection(sb *strings.Builder, schema *ast.Schema, vars map[string]interface{}, level int, selectionSet ast.SelectionSet) {
for _, selection := range selectionSet {
indentPrefix(sb, level)
switch selection := selection.(type) {
case *ast.Field:
if selection.Alias != selection.Name {
sb.WriteString(selection.Alias)
sb.WriteString(": ")
sb.WriteString(selection.Name)
} else {
sb.WriteString(selection.Alias)
}
formatArgumentList(sb, schema, vars, selection.Arguments)
for _, d := range selection.Directives {
sb.WriteString(" @")
sb.WriteString(d.Name)
formatArgumentList(sb, schema, vars, d.Arguments)
}
if len(selection.SelectionSet) > 0 {
formatSelectionSelectionSet(sb, schema, vars, level, selection.SelectionSet)
}
case *ast.InlineFragment:
fmt.Fprintf(sb, "... on %v", selection.TypeCondition)
formatSelectionSelectionSet(sb, schema, vars, level, selection.SelectionSet)
case *ast.FragmentSpread:
sb.WriteString("...")
sb.WriteString(selection.Name)
}
}
}
func formatArgumentList(sb *strings.Builder, schema *ast.Schema, vars map[string]interface{}, args ast.ArgumentList) {
if len(args) > 0 {
sb.WriteString("(")
for i, arg := range args {
if i != 0 {
sb.WriteString(", ")
}
fmt.Fprintf(sb, "%s: %s", arg.Name, formatArgument(schema, arg.Value, vars))
}
sb.WriteString(")")
}
}
func formatSelectionSet(ctx context.Context, schema *ast.Schema, selection ast.SelectionSet) string {
vars := map[string]interface{}{}
if reqctx := graphql.GetOperationContext(ctx); reqctx != nil {
vars = reqctx.Variables
}
sb := strings.Builder{}
sb.WriteString("{")
formatSelection(&sb, schema, vars, 0, selection)
sb.WriteString("\n}")
return sb.String()
}
var multipleSpacesRegex = regexp.MustCompile(`\s+`)
func formatSelectionSetSingleLine(ctx context.Context, schema *ast.Schema, selection ast.SelectionSet) string {
return multipleSpacesRegex.ReplaceAllString(formatSelectionSet(ctx, schema, selection), " ")
}
func formatArgument(schema *ast.Schema, v *ast.Value, vars map[string]interface{}) string {
if schema == nil {
// this is to allow tests to pass to due the MarshalJSON comparator not having access
// to the schema
return v.String()
}
// this is a mix between v.String() and v.Raw(vars) as we need a string value with variables replaced
if v == nil {
return "<nil>"
}
switch v.Kind {
case ast.Variable:
return "$" + v.Raw
case ast.IntValue, ast.FloatValue, ast.EnumValue, ast.BooleanValue, ast.NullValue:
return v.Raw
case ast.StringValue, ast.BlockValue:
return strconv.Quote(v.Raw)
case ast.ListValue:
var val []string
for _, elem := range v.Children {
val = append(val, formatArgument(schema, elem.Value, vars))
}
return "[" + strings.Join(val, ",") + "]"
case ast.ObjectValue:
var val []string
for _, elem := range v.Children {
val = append(val, elem.Name+":"+formatArgument(schema, elem.Value, vars))
}
return "{" + strings.Join(val, ",") + "}"
default:
panic(fmt.Errorf("unknown value kind %d", v.Kind))
}
}
func formatSchema(schema *ast.Schema) string {
buf := bytes.NewBufferString("")
f := formatter.NewFormatter(buf)
f.FormatSchema(schema)
return buf.String()
}