@@ -22,14 +22,35 @@ trait FieldTrait
2222 public function setSelectionSet (array $ selectionSet )
2323 {
2424 $ nonStringsFields = array_filter ($ selectionSet , function ($ element ) {
25- return !is_string ($ element ) && !$ element instanceof Query && !$ element instanceof InlineFragment;
25+ return !is_string ($ element ) && !is_array ( $ element ) && ! $ element instanceof Query && !$ element instanceof InlineFragment;
2626 });
27+
2728 if (!empty ($ nonStringsFields )) {
2829 throw new InvalidSelectionException (
29- 'One or more of the selection fields provided is not of type string or Query '
30+ 'One or more of the selection fields provided is not of type string, array or Query '
3031 );
3132 }
3233
34+ // SelectionSet with arguments
35+ foreach ($ selectionSet as $ element => $ arguments ) {
36+
37+ if (is_array ($ arguments ) && count ($ arguments )) {
38+
39+ $ params = '' ;
40+ foreach ($ arguments as $ param => $ val ) {
41+ if (is_string ($ val )) {
42+ $ params .= " $ param : \"$ val \" " ;
43+ }
44+ }
45+
46+ if (!empty ($ params )) {
47+ $ selectedField = ' ' . $ element . " ( $ params) " ;
48+ $ selectionSet [] = $ selectedField ;
49+ unset($ selectionSet [$ element ]);
50+ }
51+ }
52+ }
53+
3354 $ this ->selectionSet = $ selectionSet ;
3455
3556 return $ this ;
@@ -40,15 +61,15 @@ public function setSelectionSet(array $selectionSet)
4061 */
4162 protected function constructSelectionSet (): string
4263 {
43- $ attributesString = " { \n" ;
64+ $ attributesString = " { " . PHP_EOL ;
4465 $ first = true ;
4566 foreach ($ this ->selectionSet as $ attribute ) {
4667
4768 // Append empty line at the beginning if it's not the first item on the list
4869 if ($ first ) {
4970 $ first = false ;
5071 } else {
51- $ attributesString .= "\n" ;
72+ $ attributesString .= PHP_EOL ;
5273 }
5374
5475 // If query is included in attributes set as a nested query
@@ -59,7 +80,7 @@ protected function constructSelectionSet(): string
5980 // Append attribute to returned attributes list
6081 $ attributesString .= $ attribute ;
6182 }
62- $ attributesString .= "\n } " ;
83+ $ attributesString .= PHP_EOL . " } " ;
6384
6485 return $ attributesString ;
6586 }
0 commit comments