-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdumper_test.go
317 lines (257 loc) · 4.8 KB
/
dumper_test.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package ast_test
import (
"strings"
"testing"
"github.com/bucketd/go-graphqlparser/ast"
"github.com/bucketd/go-graphqlparser/language"
"github.com/stretchr/testify/assert"
)
var (
shorthandTest = strings.TrimSpace(`
{
hello
world
}
`)
schemaTest = strings.TrimSpace(`
type Query {
dog: Dog
}
enum DogCommand {
SIT
DOWN
HEEL
}
type Dog implements Pet {
name: String!
nickname: String
barkVolume: Int
doesKnowCommand(dogCommand: DogCommand!): Boolean!
isHousetrained(atOtherHomes: Boolean): Boolean!
owner: Human
}
interface Sentient {
name: String!
}
interface Pet {
name: String!
}
type Alien implements Sentient {
name: String!
homePlanet: String
}
type Human implements Sentient {
name: String!
}
enum CatCommand {
JUMP
}
type Cat implements Pet {
name: String!
nickname: String
doesKnowCommand(catCommand: CatCommand!): Boolean!
meowVolume: Int
}
union CatOrDog = Cat | Dog
union DogOrHuman = Dog | Human
union HumanOrAlien = Human | Alien
`)
exhaustiveTest = strings.TrimSpace(`
query Var($v: Int! = $var) {
selection
}
query Vars($v: Int! = $var, $i: Int! = 123, $f: Float! = 1.23e+10, $s: String! = "string") {
selection
}
query Vars2($b: Boolean! = true, $b2: Boolean! = false, $n: Int = null, $e: Enum = ENUM_VALUE) {
selection
}
query Vars3($l: [Int!]! = [1, 2, 3], $o: Point2D = { x: 1.2, y: 3.4 }) {
selection
}
query Directive @foo {
selection
}
query Directives @foo(bar: $baz, baz: "qux") @bar @baz(foo: 123) {
selection
}
query Selection {
selection
}
query Selections {
selection1
selection2
selection3 @foo
selection4 @bar(baz: "qux")
selection5 @baz(qux: 123) @foo @bar {
nested {
aliased: selections
}
}
}
query Frags {
...userFields @testytesty
... on User @exclude(not: $noInfo) {
pals: friends {
count
}
}
... @include(if: $expandedInfo) {
firstName
lastName
birthday
}
}
mutation second($variable: String = "test") {
sendEmail(message: """
Hello,
World! \"""
\u0080 \u754c 😀 \"
Yours,
GraphQL
""")
sendEmail2(message: "Hello, World!\tYours, \u0080 \u754c 😀 \" \\ \/ \b \f \t GraphQL.")
intVal(foo: 12345)
floatVal(bar: 123.456)
floatVal2(bar: 1.23456e+10)
boolVal(bool: false)
listVal(list: [1, 2, 3])
variableVal(var: $variable)
}
fragment userFields on User {
firstName
lastName
title
company {
name
slug
}
email
mobile
}
fragment postFields on Subscription @skip(if: $bar, do: $not) {
title
subtitle
slug
category {
name
slug
}
content
}
query first($foo: Boolean = true, $foo: Boolean = true) {
user(id: "3931a3fc-d4f9-4faa-bcf5-882022617376") {
...userFields
}
post(id: "489c9250-50b9-4612-b930-56dc4e1ae44e") {
...postFields
}
fooa: foo @include(if: $foo)
bara: bar @skip(if: $bar)
baza: baz @gt(val: $baz)
... @include(if: $expandedInfo) {
firstName
lastName
birthday
}
}
schema @foo @bar {
query: Query
mutation: Mutation
}
"""
Documentation blocks should support
...multiple lines
And gaps in lines
"""
directive @foo on QUERY
scalar JSON
type Person implements NamedEntity {
name: String
weight(equipmentWeight: Int, ropeStrength: Int): Int
}
type Business implements NamedEntity & ValuedEntity {
name: String
value: Int
employeeCount: Int
}
type Climbing implements
& Ropes
& Rocks
& Chalk {
name: String
weight: Int
}
interface NamedEntity {
name: String
}
interface ValuedEntity {
value: Int
}
union SmallestResult = DriftKing
union SmallerResult = Donkey | Kong
union BiggerResult =
| Orange
| Black
| RX7
enum Direction {
NORTH
EAST
SOUTH
WEST
}
input Point2D {
x: Float
y: Float
}
directive @example on FIELD
directive @example on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @example on
| FIELD
| FRAGMENT_SPREAD
| INLINE_FRAGMENT
extend schema @tonight
extend schema {
query: TheBrain
mutation: Pinky
}
extend scalar IntyThing @add
extend type Car implements Transport {
"""
Should the quotes around this be indented the same as the fields below?
Indentation is not preserved.
"""
upgrade(component: CarComponent): Cost
}
extend interface NamedEntity @addedDirective
extend interface Transport {
cost: Int
}
extend union SmallestResult = Nissan350z
extend enum Direction {
NORTHWEST
NORTHEAST
}
extend input Point2D {
distanceFromOrigin: Length
}
`)
)
func TestSdump(t *testing.T) {
tt := []struct {
descr string
query string
}{
{descr: "shorthand test", query: shorthandTest},
{descr: "schema test", query: schemaTest},
{descr: "exhaustive test", query: exhaustiveTest},
}
for _, tc := range tt {
psr := language.NewParser([]byte(tc.query))
doc, err := psr.Parse()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.query, ast.Sdump(doc))
}
}