This repository has been archived by the owner on Feb 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.go
123 lines (100 loc) · 1.98 KB
/
ast.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
package prisma
import (
"fmt"
"strings"
)
type operationType uint8
const (
opQuery operationType = iota + 1
opMutation
opSubscription
)
type fielder interface {
addField(field)
}
type argumentList []argument
func (l argumentList) format(b *strings.Builder) {
if len(l) == 0 {
return
}
b.WriteByte('(')
for i, arg := range l {
if i != 0 {
b.WriteString(", ")
}
b.WriteString(arg.name)
b.WriteString(": ")
b.WriteString(arg.value)
}
b.WriteByte(')')
}
type fieldList []field
func (l fieldList) format(b *strings.Builder) {
for _, f := range l {
f.format(b)
b.WriteByte('\n')
}
}
type operation struct {
typ operationType
name string
arguments argumentList
fields fieldList
}
func (op *operation) addField(f field) {
op.fields = append(op.fields, f)
}
type argument struct {
name string
value string
}
func formatOperation(op *operation) string {
// TODO(dh): verify that all names are valid (e.g. don't contain spaces)
b := &strings.Builder{}
switch op.typ {
case opQuery:
b.WriteString("query")
case opMutation:
b.WriteString("mutation")
case opSubscription:
b.WriteString("subscription")
default:
panic(fmt.Sprintf("invalid operation type %q", op.typ))
}
b.WriteByte(' ')
b.WriteString(op.name)
op.arguments.format(b)
b.WriteString(" {\n")
op.fields.format(b)
b.WriteByte('}')
return b.String()
}
type field interface {
format(b *strings.Builder)
isField()
}
type scalarField struct {
name string
arguments argumentList
}
func (f scalarField) format(b *strings.Builder) {
b.WriteString(f.name)
f.arguments.format(b)
}
func (scalarField) isField() {}
type objectField struct {
name string
arguments argumentList
fields fieldList
}
func (of *objectField) addField(f field) {
of.fields = append(of.fields, f)
}
func (of objectField) format(b *strings.Builder) {
b.WriteString(of.name)
of.arguments.format(b)
b.WriteString(" {\n")
of.fields.format(b)
b.WriteString("}")
}
func (objectField) isField() {}