Skip to content

Commit dc9fb8a

Browse files
committed
Support aliases for selection set.
Add the ability to set arguments for scalar selected fields. fix: pass tests on windows PHP_EOL.
1 parent c8a6642 commit dc9fb8a

File tree

5 files changed

+118
-9
lines changed

5 files changed

+118
-9
lines changed

src/FieldTrait.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function __toString()
211211
{
212212
$queryFormat = static::QUERY_FORMAT;
213213
if (!$this->isNested && $this->fieldName !== static::OPERATION_TYPE) {
214-
$queryFormat = $this->generateSignature() . " {\n" . static::QUERY_FORMAT . "\n}";
214+
$queryFormat = $this->generateSignature() . " {" . PHP_EOL . static::QUERY_FORMAT . PHP_EOL. "}";
215215
}
216216
$argumentsString = $this->constructArguments();
217217
$selectionSetString = $this->constructSelectionSet();

src/QueryBuilder/AbstractQueryBuilder.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,33 @@ public function getQuery(): Query
7474
/**
7575
* @param string|QueryBuilder|Query $selectedField
7676
*
77+
* @param string|null $alias
78+
* @param array $arguments
7779
* @return $this
7880
*/
79-
protected function selectField($selectedField)
81+
protected function selectField($selectedField, string $alias = null, array $arguments = [])
8082
{
8183
if (
8284
is_string($selectedField)
8385
|| $selectedField instanceof AbstractQueryBuilder
8486
|| $selectedField instanceof Query
8587
|| $selectedField instanceof InlineFragment
8688
) {
89+
90+
// Set an alias for selectedField
91+
$selectedField = ($alias) ? $alias .':'. $selectedField : $selectedField;
92+
93+
// Add arguments for selectedField
94+
if (is_array($arguments) && count($arguments)) {
95+
96+
$params = '';
97+
foreach ($arguments as $k => $v) {
98+
$params.= " $k : \"$v\" ";
99+
}
100+
$selectedField .= " ($params)";
101+
}
102+
103+
87104
$this->selectionSet[] = $selectedField;
88105
}
89106

src/QueryBuilder/QueryBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ class QueryBuilder extends AbstractQueryBuilder
1616
*
1717
* @param Query|QueryBuilder|string $selectedField
1818
*
19+
* @param string|null $alias
20+
* @param array $arguments
1921
* @return AbstractQueryBuilder|QueryBuilder
2022
*/
21-
public function selectField($selectedField)
23+
public function selectField($selectedField, string $alias = null, array $arguments = [])
2224
{
23-
return parent::selectField($selectedField);
25+
return parent::selectField($selectedField, $alias, $arguments);
2426
}
2527

2628
/**

tests/QueryBuilderTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,75 @@ public function testEmptySelectionSet()
6161
$this->queryBuilder->getQuery();
6262
}
6363

64+
65+
/**
66+
* @covers \GraphQL\QueryBuilder\QueryBuilder::__construct
67+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct
68+
*/
69+
public function testSelectFieldAlias()
70+
{
71+
$builder = new QueryBuilder('Object');
72+
$builder->selectField('field_one', 'fieldAlias');
73+
$this->assertEquals(
74+
'query {
75+
Object {
76+
fieldAlias:field_one
77+
}
78+
}',
79+
(string) $builder->getQuery()
80+
);
81+
}
82+
83+
/**
84+
* @covers \GraphQL\QueryBuilder\QueryBuilder::__construct
85+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct
86+
*/
87+
public function testSelectFieldAliasWithArguments()
88+
{
89+
$builder = new QueryBuilder('Object');
90+
$builder->selectField('field_one', 'fieldAlias', ['param1' => 'val1', 'param2' => 'val2' ]);
91+
$this->assertEquals(
92+
'query {
93+
Object {
94+
fieldAlias:field_one ( param1 : "val1" param2 : "val2" )
95+
}
96+
}',
97+
(string) $builder->getQuery()
98+
);
99+
100+
}
101+
102+
/**
103+
* @covers \GraphQL\QueryBuilder\QueryBuilder::__construct
104+
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct
105+
*/
106+
public function testSelectionSetAliasWithArguments()
107+
{
108+
$builder = new QueryBuilder('Object');
109+
110+
$builder->selectField(
111+
(new Query('data'))->setArguments(['some_field' => 'params'])
112+
->setSelectionSet(
113+
[
114+
'aliasId:first_field' => ['param1' => 'val1', 'param2' => 'val2' ],
115+
'second_field' => ['param1' => 'val1', 'param2' => 'val2' ]
116+
])
117+
);
118+
119+
$this->assertEquals(
120+
'query {
121+
Object {
122+
data(some_field: "params") {
123+
aliasId:first_field ( param1 : "val1" param2 : "val2" )
124+
second_field ( param1 : "val1" param2 : "val2" )
125+
}
126+
}
127+
}',
128+
(string) $builder->getQuery()
129+
);
130+
131+
132+
}
64133
/**
65134
* @covers \GraphQL\QueryBuilder\QueryBuilder::setVariable
66135
* @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::setVariable

0 commit comments

Comments
 (0)