1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Type; |
6
|
|
|
|
7
|
|
|
use GraphQL\GraphQL; |
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
9
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
10
|
|
|
use GraphQL\Type\Definition\Type; |
11
|
|
|
use GraphQL\Type\Schema; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class ResolveInfoTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testFieldSelection() : void |
17
|
|
|
{ |
18
|
|
|
$image = new ObjectType([ |
19
|
|
|
'name' => 'Image', |
20
|
|
|
'fields' => [ |
21
|
|
|
'url' => ['type' => Type::string()], |
22
|
|
|
'width' => ['type' => Type::int()], |
23
|
|
|
'height' => ['type' => Type::int()], |
24
|
|
|
], |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
$article = null; |
28
|
|
|
|
29
|
|
|
$author = new ObjectType([ |
30
|
|
|
'name' => 'Author', |
31
|
|
|
'fields' => static function () use ($image, &$article) { |
32
|
|
|
return [ |
33
|
|
|
'id' => ['type' => Type::string()], |
34
|
|
|
'name' => ['type' => Type::string()], |
35
|
|
|
'pic' => [ |
36
|
|
|
'type' => $image, |
37
|
|
|
'args' => [ |
38
|
|
|
'width' => ['type' => Type::int()], |
39
|
|
|
'height' => ['type' => Type::int()], |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
'recentArticle' => ['type' => $article], |
43
|
|
|
]; |
44
|
|
|
}, |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$reply = new ObjectType([ |
48
|
|
|
'name' => 'Reply', |
49
|
|
|
'fields' => [ |
50
|
|
|
'author' => ['type' => $author], |
51
|
|
|
'body' => ['type' => Type::string()], |
52
|
|
|
], |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$article = new ObjectType([ |
56
|
|
|
'name' => 'Article', |
57
|
|
|
'fields' => [ |
58
|
|
|
'id' => ['type' => Type::string()], |
59
|
|
|
'isPublished' => ['type' => Type::boolean()], |
60
|
|
|
'author' => ['type' => $author], |
61
|
|
|
'title' => ['type' => Type::string()], |
62
|
|
|
'body' => ['type' => Type::string()], |
63
|
|
|
'image' => ['type' => $image], |
64
|
|
|
'replies' => ['type' => Type::listOf($reply)], |
65
|
|
|
], |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$doc = ' |
69
|
|
|
query Test { |
70
|
|
|
article { |
71
|
|
|
author { |
72
|
|
|
name |
73
|
|
|
pic { |
74
|
|
|
url |
75
|
|
|
width |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
image { |
79
|
|
|
width |
80
|
|
|
height |
81
|
|
|
...MyImage |
82
|
|
|
} |
83
|
|
|
replies { |
84
|
|
|
body |
85
|
|
|
author { |
86
|
|
|
id |
87
|
|
|
name |
88
|
|
|
pic { |
89
|
|
|
url |
90
|
|
|
width |
91
|
|
|
... on Image { |
92
|
|
|
height |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
recentArticle { |
96
|
|
|
id |
97
|
|
|
title |
98
|
|
|
body |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
fragment MyImage on Image { |
105
|
|
|
url |
106
|
|
|
} |
107
|
|
|
'; |
108
|
|
|
$expectedDefaultSelection = [ |
109
|
|
|
'author' => true, |
110
|
|
|
'image' => true, |
111
|
|
|
'replies' => true, |
112
|
|
|
]; |
113
|
|
|
$expectedDeepSelection = [ |
114
|
|
|
'author' => [ |
115
|
|
|
'name' => true, |
116
|
|
|
'pic' => [ |
117
|
|
|
'url' => true, |
118
|
|
|
'width' => true, |
119
|
|
|
], |
120
|
|
|
], |
121
|
|
|
'image' => [ |
122
|
|
|
'width' => true, |
123
|
|
|
'height' => true, |
124
|
|
|
'url' => true, |
125
|
|
|
], |
126
|
|
|
'replies' => [ |
127
|
|
|
'body' => true, |
128
|
|
|
'author' => [ |
129
|
|
|
'id' => true, |
130
|
|
|
'name' => true, |
131
|
|
|
'pic' => [ |
132
|
|
|
'url' => true, |
133
|
|
|
'width' => true, |
134
|
|
|
'height' => true, |
135
|
|
|
], |
136
|
|
|
'recentArticle' => [ |
137
|
|
|
'id' => true, |
138
|
|
|
'title' => true, |
139
|
|
|
'body' => true, |
140
|
|
|
], |
141
|
|
|
], |
142
|
|
|
], |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$hasCalled = false; |
146
|
|
|
$actualDefaultSelection = null; |
147
|
|
|
$actualDeepSelection = null; |
148
|
|
|
|
149
|
|
|
$blogQuery = new ObjectType([ |
150
|
|
|
'name' => 'Query', |
151
|
|
|
'fields' => [ |
152
|
|
|
'article' => [ |
153
|
|
|
'type' => $article, |
154
|
|
|
'resolve' => static function ( |
155
|
|
|
$value, |
156
|
|
|
$args, |
157
|
|
|
$context, |
158
|
|
|
ResolveInfo $info |
159
|
|
|
) use ( |
160
|
|
|
&$hasCalled, |
161
|
|
|
& |
162
|
|
|
$actualDefaultSelection, |
163
|
|
|
&$actualDeepSelection |
164
|
|
|
) { |
165
|
|
|
$hasCalled = true; |
166
|
|
|
$actualDefaultSelection = $info->getFieldSelection(); |
167
|
|
|
$actualDeepSelection = $info->getFieldSelection(5); |
168
|
|
|
|
169
|
|
|
return null; |
170
|
|
|
}, |
171
|
|
|
], |
172
|
|
|
], |
173
|
|
|
]); |
174
|
|
|
|
175
|
|
|
$schema = new Schema(['query' => $blogQuery]); |
176
|
|
|
$result = GraphQL::executeQuery($schema, $doc)->toArray(); |
177
|
|
|
|
178
|
|
|
self::assertTrue($hasCalled); |
179
|
|
|
self::assertEquals(['data' => ['article' => null]], $result); |
180
|
|
|
self::assertEquals($expectedDefaultSelection, $actualDefaultSelection); |
181
|
|
|
self::assertEquals($expectedDeepSelection, $actualDeepSelection); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testFieldSelectionOnScalarTypes() : void |
185
|
|
|
{ |
186
|
|
|
$query = ' |
187
|
|
|
query Ping { |
188
|
|
|
ping |
189
|
|
|
} |
190
|
|
|
'; |
191
|
|
|
|
192
|
|
|
$pingPongQuery = new ObjectType([ |
193
|
|
|
'name' => 'Query', |
194
|
|
|
'fields' => [ |
195
|
|
|
'ping' => [ |
196
|
|
|
'type' => Type::string(), |
197
|
|
|
'resolve' => static function ($value, $args, $context, ResolveInfo $info) : string { |
198
|
|
|
self::assertEquals([], $info->getFieldSelection()); |
199
|
|
|
|
200
|
|
|
return 'pong'; |
201
|
|
|
}, |
202
|
|
|
], |
203
|
|
|
], |
204
|
|
|
]); |
205
|
|
|
|
206
|
|
|
$schema = new Schema(['query' => $pingPongQuery]); |
207
|
|
|
$result = GraphQL::executeQuery($schema, $query)->toArray(); |
208
|
|
|
|
209
|
|
|
self::assertEquals(['data' => ['ping' => 'pong']], $result); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function testMergedFragmentsFieldSelection() : void |
213
|
|
|
{ |
214
|
|
|
$image = new ObjectType([ |
215
|
|
|
'name' => 'Image', |
216
|
|
|
'fields' => [ |
217
|
|
|
'url' => ['type' => Type::string()], |
218
|
|
|
'width' => ['type' => Type::int()], |
219
|
|
|
'height' => ['type' => Type::int()], |
220
|
|
|
], |
221
|
|
|
]); |
222
|
|
|
|
223
|
|
|
$article = null; |
224
|
|
|
|
225
|
|
|
$author = new ObjectType([ |
226
|
|
|
'name' => 'Author', |
227
|
|
|
'fields' => static function () use ($image, &$article) { |
228
|
|
|
return [ |
229
|
|
|
'id' => ['type' => Type::string()], |
230
|
|
|
'name' => ['type' => Type::string()], |
231
|
|
|
'pic' => [ |
232
|
|
|
'type' => $image, |
233
|
|
|
'args' => [ |
234
|
|
|
'width' => ['type' => Type::int()], |
235
|
|
|
'height' => ['type' => Type::int()], |
236
|
|
|
], |
237
|
|
|
], |
238
|
|
|
'recentArticle' => ['type' => $article], |
239
|
|
|
]; |
240
|
|
|
}, |
241
|
|
|
]); |
242
|
|
|
|
243
|
|
|
$reply = new ObjectType([ |
244
|
|
|
'name' => 'Reply', |
245
|
|
|
'fields' => [ |
246
|
|
|
'author' => ['type' => $author], |
247
|
|
|
'body' => ['type' => Type::string()], |
248
|
|
|
], |
249
|
|
|
]); |
250
|
|
|
|
251
|
|
|
$article = new ObjectType([ |
252
|
|
|
'name' => 'Article', |
253
|
|
|
'fields' => [ |
254
|
|
|
'id' => ['type' => Type::string()], |
255
|
|
|
'isPublished' => ['type' => Type::boolean()], |
256
|
|
|
'author' => ['type' => $author], |
257
|
|
|
'title' => ['type' => Type::string()], |
258
|
|
|
'body' => ['type' => Type::string()], |
259
|
|
|
'image' => ['type' => $image], |
260
|
|
|
'replies' => ['type' => Type::listOf($reply)], |
261
|
|
|
], |
262
|
|
|
]); |
263
|
|
|
|
264
|
|
|
$doc = ' |
265
|
|
|
query Test { |
266
|
|
|
article { |
267
|
|
|
author { |
268
|
|
|
name |
269
|
|
|
pic { |
270
|
|
|
url |
271
|
|
|
width |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
image { |
275
|
|
|
width |
276
|
|
|
height |
277
|
|
|
...MyImage |
278
|
|
|
} |
279
|
|
|
...Replies01 |
280
|
|
|
...Replies02 |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
fragment MyImage on Image { |
284
|
|
|
url |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
fragment Replies01 on Article { |
288
|
|
|
_replies012: replies { |
289
|
|
|
body |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
fragment Replies02 on Article { |
293
|
|
|
_replies012: replies { |
294
|
|
|
author { |
295
|
|
|
id |
296
|
|
|
name |
297
|
|
|
pic { |
298
|
|
|
url |
299
|
|
|
width |
300
|
|
|
... on Image { |
301
|
|
|
height |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
recentArticle { |
305
|
|
|
id |
306
|
|
|
title |
307
|
|
|
body |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
'; |
313
|
|
|
|
314
|
|
|
$expectedDeepSelection = [ |
315
|
|
|
'author' => [ |
316
|
|
|
'name' => true, |
317
|
|
|
'pic' => [ |
318
|
|
|
'url' => true, |
319
|
|
|
'width' => true, |
320
|
|
|
], |
321
|
|
|
], |
322
|
|
|
'image' => [ |
323
|
|
|
'width' => true, |
324
|
|
|
'height' => true, |
325
|
|
|
'url' => true, |
326
|
|
|
], |
327
|
|
|
'replies' => [ |
328
|
|
|
'body' => true, //this would be missing if not for the fix https://github.com/webonyx/graphql-php/pull/98 |
329
|
|
|
'author' => [ |
330
|
|
|
'id' => true, |
331
|
|
|
'name' => true, |
332
|
|
|
'pic' => [ |
333
|
|
|
'url' => true, |
334
|
|
|
'width' => true, |
335
|
|
|
'height' => true, |
336
|
|
|
], |
337
|
|
|
'recentArticle' => [ |
338
|
|
|
'id' => true, |
339
|
|
|
'title' => true, |
340
|
|
|
'body' => true, |
341
|
|
|
], |
342
|
|
|
], |
343
|
|
|
], |
344
|
|
|
]; |
345
|
|
|
|
346
|
|
|
$hasCalled = false; |
347
|
|
|
$actualDefaultSelection = null; |
|
|
|
|
348
|
|
|
$actualDeepSelection = null; |
349
|
|
|
|
350
|
|
|
$blogQuery = new ObjectType([ |
351
|
|
|
'name' => 'Query', |
352
|
|
|
'fields' => [ |
353
|
|
|
'article' => [ |
354
|
|
|
'type' => $article, |
355
|
|
|
'resolve' => static function ( |
356
|
|
|
$value, |
357
|
|
|
$args, |
358
|
|
|
$context, |
359
|
|
|
ResolveInfo $info |
360
|
|
|
) use ( |
361
|
|
|
&$hasCalled, |
362
|
|
|
& |
363
|
|
|
$actualDeepSelection |
364
|
|
|
) { |
365
|
|
|
$hasCalled = true; |
366
|
|
|
$actualDeepSelection = $info->getFieldSelection(5); |
367
|
|
|
|
368
|
|
|
return null; |
369
|
|
|
}, |
370
|
|
|
], |
371
|
|
|
], |
372
|
|
|
]); |
373
|
|
|
|
374
|
|
|
$schema = new Schema(['query' => $blogQuery]); |
375
|
|
|
$result = GraphQL::executeQuery($schema, $doc)->toArray(); |
376
|
|
|
|
377
|
|
|
self::assertTrue($hasCalled); |
378
|
|
|
self::assertEquals(['data' => ['article' => null]], $result); |
379
|
|
|
self::assertEquals($expectedDeepSelection, $actualDeepSelection); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|