File tree Expand file tree Collapse file tree 3 files changed +71
-4
lines changed Expand file tree Collapse file tree 3 files changed +71
-4
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 1616use GraphQL \Type \Definition \InterfaceType ;
1717use GraphQL \Type \Definition \NonNull ;
1818use GraphQL \Type \Definition \ObjectType ;
19+ use GraphQL \Type \Definition \ResolveInfo ;
1920use 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 ' => [
You can’t perform that action at this time.
0 commit comments