-
Notifications
You must be signed in to change notification settings - Fork 0
/
typesystem.go
297 lines (263 loc) · 5.61 KB
/
typesystem.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package ast
import (
"encoding/json"
"strings"
)
type DefinitionKind uint
const (
SchemaKind DefinitionKind = iota
ScalarKind
ObjectKind
InterfaceKind
UnionKind
EnumKind
InputObjectKind
DirectiveKind
)
type Definition interface {
Kind() DefinitionKind
String() string
}
type SchemaDefinition struct {
Name string
Directives []*Directive
RootOperations map[OperationType]*NamedType
}
func (d *SchemaDefinition) Kind() DefinitionKind {
return SchemaKind
}
func (d *SchemaDefinition) String() string {
out := "schema "
for _, dir := range d.Directives {
out += dir.String() + " "
}
out += "{\n"
if nt, ok := d.RootOperations[Query]; ok && nt != nil {
out += "\tquery: " + nt.Name + "\n"
}
if nt, ok := d.RootOperations[Mutation]; ok && nt != nil {
out += "\tmutation: " + nt.Name + "\n"
}
if nt, ok := d.RootOperations[Subscription]; ok && nt != nil {
out += "\tsubscription: " + nt.Name + "\n"
}
out += "}\n"
return out
}
type ScalarDefinition struct {
Description string
Name string
Directives []*Directive
}
func (d *ScalarDefinition) Kind() DefinitionKind {
return ScalarKind
}
func (d *ScalarDefinition) String() string {
out := ""
if d.Description != "" {
out += `"""` + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "scalar " + d.Name + " "
for _, dir := range d.Directives {
out += dir.String() + " "
}
out += "\n"
return out
}
type ObjectDefinition struct {
Description string
Name string
Implements []*NamedType
Directives []*Directive
Fields []*FieldDefinition
}
func (d *ObjectDefinition) Kind() DefinitionKind {
return ObjectKind
}
func (d *ObjectDefinition) String() string {
out := ""
if d.Description != "" {
out += `"""` + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "type " + d.Name + " "
for _, dir := range d.Directives {
out += dir.String() + " "
}
out += "{\n"
for i, f := range d.Fields {
if i != 0 {
out += "\n"
}
out += f.String()
}
out += "}\n"
return out
}
type FieldDefinition struct {
Description string
Name string
Arguments []*InputValueDefinition
Type Type
Directives []*Directive
}
func (d *FieldDefinition) String() string {
out := ""
if d.Description != "" {
out += "\t\"\"\"" + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "\t" + d.Name
if d.Arguments != nil && len(d.Arguments) != 0 {
out += "(\n\t\t"
for i, a := range d.Arguments {
out += a.String()
if i+1 == len(d.Arguments) {
out += "\n\t)"
} else {
out += "\n\t\t"
}
}
}
out += ": " + d.Type.String()
for _, dir := range d.Directives {
out += " " + dir.String()
}
out += "\n"
return out
}
type InputValueDefinition struct {
Description string
Name string
Type Type
DefaultValue Value
Directives []*Directive
}
func (d *InputValueDefinition) String() string {
return d.Name + ": " + d.Type.String()
}
type InterfaceDefinition struct {
Description string
Name string
Directives []*Directive
Fields []*FieldDefinition
}
func (d *InterfaceDefinition) Kind() DefinitionKind {
return InterfaceKind
}
func (d *InterfaceDefinition) String() string {
out := ""
if d.Description != "" {
out += `"""` + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "interface " + d.Name + " "
for _, dir := range d.Directives {
out += dir.String() + " "
}
out += "{\n"
for i, f := range d.Fields {
if i != 0 {
out += "\n"
}
out += f.String()
}
out += "}\n"
return out
}
type UnionDefinition struct {
Description string
Name string
Directives []*Directive
Members []*NamedType
}
func (d *UnionDefinition) Kind() DefinitionKind {
return UnionKind
}
func (d *UnionDefinition) String() string {
out := ""
if d.Description != "" {
out += `"""` + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "union " + d.Name + " "
for _, dir := range d.Directives {
out += dir.String() + " "
}
out += "= "
for i, m := range d.Members {
if i != 0 {
out += " | "
}
out += m.Name
}
return out + "\n"
}
type EnumDefinition struct {
Description string
Name string
Directives []*Directive
Values []*EnumValueDefinition
}
func (d *EnumDefinition) Kind() DefinitionKind {
return EnumKind
}
func (d *EnumDefinition) String() string {
out := ""
if d.Description != "" {
out += `"""` + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "enum " + d.Name + " "
for _, dir := range d.Directives {
out += dir.String() + " "
}
out += "{\n}\n"
return out
}
type EnumValueDefinition struct {
Description string
Value *EnumValue
Directives []*Directive
}
type InputObjectDefinition struct {
Description string
Name string
Directives []*Directive
Fields []*InputValueDefinition
}
func (d *InputObjectDefinition) Kind() DefinitionKind {
return InputObjectKind
}
func (d *InputObjectDefinition) String() string {
out := ""
if d.Description != "" {
out += `"""` + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "input " + d.Name + " "
for _, dir := range d.Directives {
out += dir.String() + " "
}
out += "{\n}\n"
return out
}
type DirectiveDefinition struct {
Description string
Name string
Locations []string
Arguments []*InputValueDefinition
}
func (d *DirectiveDefinition) Kind() DefinitionKind {
return DirectiveKind
}
func (d *DirectiveDefinition) String() string {
out := ""
if d.Description != "" {
out += `"""` + jsonEscape(d.Description) + "\"\"\"\n"
}
out += "directive @" + d.Name + " on " + strings.Join(d.Locations, " | ") + "\n"
return out
}
func jsonEscape(i string) string {
b, err := json.Marshal(i)
if err != nil {
panic(err)
}
s := string(b)
return s[1 : len(s)-1]
}