|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Tests\Language; |
|
3
|
|
|
|
|
4
|
|
|
use GraphQL\Language\AST\DocumentNode; |
|
5
|
|
|
use GraphQL\Language\AST\EnumValueNode; |
|
6
|
|
|
use GraphQL\Language\AST\FieldNode; |
|
7
|
|
|
use GraphQL\Language\AST\NameNode; |
|
8
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode; |
|
9
|
|
|
use GraphQL\Language\AST\SelectionSetNode; |
|
10
|
|
|
use GraphQL\Language\AST\StringValueNode; |
|
11
|
|
|
use GraphQL\Language\AST\VariableNode; |
|
12
|
|
|
use GraphQL\Language\AST\VariableDefinitionNode; |
|
13
|
|
|
use GraphQL\Language\Parser; |
|
14
|
|
|
use GraphQL\Language\Printer; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class PrinterTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @it does not alter ast |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testDoesntAlterAST() |
|
23
|
|
|
{ |
|
24
|
|
|
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql'); |
|
25
|
|
|
$ast = Parser::parse($kitchenSink); |
|
26
|
|
|
|
|
27
|
|
|
$astCopy = $ast->cloneDeep(); |
|
28
|
|
|
$this->assertEquals($astCopy, $ast); |
|
29
|
|
|
|
|
30
|
|
|
Printer::doPrint($ast); |
|
31
|
|
|
$this->assertEquals($astCopy, $ast); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @it prints minimal ast |
|
36
|
|
|
*/ |
|
37
|
|
|
public function testPrintsMinimalAst() |
|
38
|
|
|
{ |
|
39
|
|
|
$ast = new FieldNode(['name' => new NameNode(['value' => 'foo'])]); |
|
40
|
|
|
$this->assertEquals('foo', Printer::doPrint($ast)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @it produces helpful error messages |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testProducesHelpfulErrorMessages() |
|
47
|
|
|
{ |
|
48
|
|
|
$badAst1 = new \ArrayObject(['random' => 'Data']); |
|
49
|
|
|
$this->expectException(\Throwable::class); |
|
50
|
|
|
$this->expectExceptionMessage('Invalid AST Node: {"random":"Data"}'); |
|
51
|
|
|
Printer::doPrint($badAst1); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @it correctly prints non-query operations without name |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testCorrectlyPrintsOpsWithoutName() |
|
58
|
|
|
{ |
|
59
|
|
|
$queryAstShorthanded = Parser::parse('query { id, name }'); |
|
60
|
|
|
|
|
61
|
|
|
$expected = '{ |
|
62
|
|
|
id |
|
63
|
|
|
name |
|
64
|
|
|
} |
|
65
|
|
|
'; |
|
66
|
|
|
$this->assertEquals($expected, Printer::doPrint($queryAstShorthanded)); |
|
67
|
|
|
|
|
68
|
|
|
$mutationAst = Parser::parse('mutation { id, name }'); |
|
69
|
|
|
$expected = 'mutation { |
|
70
|
|
|
id |
|
71
|
|
|
name |
|
72
|
|
|
} |
|
73
|
|
|
'; |
|
74
|
|
|
$this->assertEquals($expected, Printer::doPrint($mutationAst)); |
|
75
|
|
|
|
|
76
|
|
|
$queryAstWithArtifacts = Parser::parse( |
|
77
|
|
|
'query ($foo: TestType) @testDirective { id, name }' |
|
78
|
|
|
); |
|
79
|
|
|
$expected = 'query ($foo: TestType) @testDirective { |
|
80
|
|
|
id |
|
81
|
|
|
name |
|
82
|
|
|
} |
|
83
|
|
|
'; |
|
84
|
|
|
$this->assertEquals($expected, Printer::doPrint($queryAstWithArtifacts)); |
|
85
|
|
|
|
|
86
|
|
|
$mutationAstWithArtifacts = Parser::parse( |
|
87
|
|
|
'mutation ($foo: TestType) @testDirective { id, name }' |
|
88
|
|
|
); |
|
89
|
|
|
$expected = 'mutation ($foo: TestType) @testDirective { |
|
90
|
|
|
id |
|
91
|
|
|
name |
|
92
|
|
|
} |
|
93
|
|
|
'; |
|
94
|
|
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @it correctly prints single-line with leading space |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testCorrectlyPrintsSingleLineBlockStringsWithLeadingSpace() |
|
101
|
|
|
{ |
|
102
|
|
|
$mutationAstWithArtifacts = Parser::parse( |
|
103
|
|
|
'{ field(arg: """ space-led value""") }' |
|
104
|
|
|
); |
|
105
|
|
|
$expected = '{ |
|
106
|
|
|
field(arg: """ space-led value""") |
|
107
|
|
|
} |
|
108
|
|
|
'; |
|
109
|
|
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @it correctly prints string with a first line indentation |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testCorrectlyPrintsBlockStringsWithAFirstLineIndentation() |
|
116
|
|
|
{ |
|
117
|
|
|
$mutationAstWithArtifacts = Parser::parse( |
|
118
|
|
|
'{ |
|
119
|
|
|
field(arg: """ |
|
120
|
|
|
first |
|
121
|
|
|
line |
|
122
|
|
|
indentation |
|
123
|
|
|
""") |
|
124
|
|
|
}' |
|
125
|
|
|
); |
|
126
|
|
|
$expected = '{ |
|
127
|
|
|
field(arg: """ |
|
128
|
|
|
first |
|
129
|
|
|
line |
|
130
|
|
|
indentation |
|
131
|
|
|
""") |
|
132
|
|
|
} |
|
133
|
|
|
'; |
|
134
|
|
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @it correctly prints single-line with leading space and quotation |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testCorrectlyPrintsSingleLineWithLeadingSpaceAndQuotation() |
|
141
|
|
|
{ |
|
142
|
|
|
$mutationAstWithArtifacts = Parser::parse(' |
|
143
|
|
|
{ |
|
144
|
|
|
field(arg: """ space-led value "quoted string" |
|
145
|
|
|
""") |
|
146
|
|
|
} |
|
147
|
|
|
'); |
|
148
|
|
|
$expected = <<<END |
|
149
|
|
|
{ |
|
150
|
|
|
field(arg: """ space-led value "quoted string" |
|
151
|
|
|
""") |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
END; |
|
155
|
|
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @it Experimental: correctly prints fragment defined variables |
|
160
|
|
|
*/ |
|
161
|
|
|
public function testExperimentalCorrectlyPrintsFragmentDefinedVariables() |
|
162
|
|
|
{ |
|
163
|
|
|
$fragmentWithVariable = Parser::parse(' |
|
164
|
|
|
fragment Foo($a: ComplexType, $b: Boolean = false) on TestType { |
|
165
|
|
|
id |
|
166
|
|
|
} |
|
167
|
|
|
', |
|
168
|
|
|
['experimentalFragmentVariables' => true] |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals( |
|
172
|
|
|
Printer::doPrint($fragmentWithVariable), |
|
173
|
|
|
'fragment Foo($a: ComplexType, $b: Boolean = false) on TestType { |
|
174
|
|
|
id |
|
175
|
|
|
} |
|
176
|
|
|
' |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @it correctly prints single-line with leading space and quotation |
|
182
|
|
|
*/ |
|
183
|
|
|
public function testCorrectlyPrintsSingleLineStringsWithLeadingSpaceAndQuotation() |
|
184
|
|
|
{ |
|
185
|
|
|
$mutationAstWithArtifacts = Parser::parse( |
|
186
|
|
|
'{ |
|
187
|
|
|
field(arg: """ space-led value "quoted string" |
|
188
|
|
|
""") |
|
189
|
|
|
}' |
|
190
|
|
|
); |
|
191
|
|
|
$expected = '{ |
|
192
|
|
|
field(arg: """ space-led value "quoted string" |
|
193
|
|
|
""") |
|
194
|
|
|
} |
|
195
|
|
|
'; |
|
196
|
|
|
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @it prints kitchen sink |
|
201
|
|
|
*/ |
|
202
|
|
|
public function testPrintsKitchenSink() |
|
203
|
|
|
{ |
|
204
|
|
|
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql'); |
|
205
|
|
|
$ast = Parser::parse($kitchenSink); |
|
206
|
|
|
|
|
207
|
|
|
$printed = Printer::doPrint($ast); |
|
208
|
|
|
|
|
209
|
|
|
$expected = <<<'EOT' |
|
210
|
|
|
query queryName($foo: ComplexType, $site: Site = MOBILE) { |
|
211
|
|
|
whoever123is: node(id: [123, 456]) { |
|
212
|
|
|
id |
|
213
|
|
|
... on User @defer { |
|
214
|
|
|
field2 { |
|
215
|
|
|
id |
|
216
|
|
|
alias: field1(first: 10, after: $foo) @include(if: $foo) { |
|
217
|
|
|
id |
|
218
|
|
|
...frag |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
... @skip(unless: $foo) { |
|
223
|
|
|
id |
|
224
|
|
|
} |
|
225
|
|
|
... { |
|
226
|
|
|
id |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
mutation likeStory { |
|
232
|
|
|
like(story: 123) @defer { |
|
233
|
|
|
story { |
|
234
|
|
|
id |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) { |
|
240
|
|
|
storyLikeSubscribe(input: $input) { |
|
241
|
|
|
story { |
|
242
|
|
|
likers { |
|
243
|
|
|
count |
|
244
|
|
|
} |
|
245
|
|
|
likeSentence { |
|
246
|
|
|
text |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
fragment frag on Friend { |
|
253
|
|
|
foo(size: $size, bar: $b, obj: {key: "value", block: """ |
|
254
|
|
|
block string uses \""" |
|
255
|
|
|
"""}) |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
{ |
|
259
|
|
|
unnamed(truthy: true, falsey: false, nullish: null) |
|
260
|
|
|
query |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
EOT; |
|
264
|
|
|
$this->assertEquals($expected, $printed); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|