Skip to content

Commit 0e1929f

Browse files
committed
Merge pull request #39 from mcg-web/fix_resolve_info_field_selection_when_using_multiple_fragments
Fix `ResolveInfo::getFieldSelection()` when using multiple fragments
2 parents 7916c54 + be46b14 commit 0e1929f

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/Type/Definition/ResolveInfo.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ public function __construct(array $values)
9595
*/
9696
public function getFieldSelection($depth = 0)
9797
{
98+
$fields = [];
99+
98100
/** @var Field $fieldAST */
99-
$fieldAST = $this->fieldASTs[0];
100-
return $this->foldSelectionSet($fieldAST->selectionSet, $depth);
101+
foreach ($this->fieldASTs as $fieldAST) {
102+
$fields = array_merge_recursive($fields, $this->foldSelectionSet($fieldAST->selectionSet, $depth));
103+
}
104+
105+
return $fields;
101106
}
102107

103108
private function foldSelectionSet(SelectionSet $selectionSet, $descend)

tests/StarWarsQueryTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,57 @@ function testUsingFragment()
302302
$this->assertValidQuery($query, $expected);
303303
}
304304

305+
function testFragmentWithProjection()
306+
{
307+
$query = '
308+
query UseFragment {
309+
human(id: "1003") {
310+
name
311+
...fa
312+
...fb
313+
}
314+
}
315+
316+
fragment fa on Character {
317+
friends {
318+
id
319+
}
320+
}
321+
fragment fb on Character {
322+
friends {
323+
name
324+
}
325+
}
326+
';
327+
328+
$expected = [
329+
'human' => [
330+
'name' => 'Leia Organa',
331+
'friends' => [
332+
[
333+
'name' => 'Luke Skywalker',
334+
'id' => '1000'
335+
336+
],
337+
[
338+
'name' => 'Han Solo',
339+
'id' => '1002'
340+
],
341+
[
342+
'name' => 'C-3PO',
343+
'id' => '2000'
344+
],
345+
[
346+
'name' => 'R2-D2',
347+
'id' => '2001'
348+
]
349+
]
350+
]
351+
];
352+
353+
$this->assertValidQuery($query, $expected);
354+
}
355+
305356
// Using __typename to find the type of an object
306357
public function testVerifyThatR2D2IsADroid()
307358
{

tests/StarWarsSchema.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use GraphQL\Type\Definition\InterfaceType;
1717
use GraphQL\Type\Definition\NonNull;
1818
use GraphQL\Type\Definition\ObjectType;
19+
use GraphQL\Type\Definition\ResolveInfo;
1920
use GraphQL\Type\Definition\Type;
2021

2122
/**
@@ -161,8 +162,18 @@ public static function build()
161162
'friends' => [
162163
'type' => Type::listOf($characterInterface),
163164
'description' => 'The friends of the human, or an empty list if they have none.',
164-
'resolve' => function ($human) {
165-
return StarWarsData::getFriends($human);
165+
'resolve' => function ($human, $args, ResolveInfo $info) {
166+
$fieldSelection = $info->getFieldSelection();
167+
$fieldSelection['id'] = true;
168+
169+
$friends = array_map(
170+
function($friend) use ($fieldSelection) {
171+
return array_intersect_key($friend, $fieldSelection);
172+
},
173+
StarWarsData::getFriends($human)
174+
);
175+
176+
return $friends;
166177
},
167178
],
168179
'appearsIn' => [

0 commit comments

Comments
 (0)