Skip to content

Commit 072341e

Browse files
committed
Issue-34: Removed empty braces added in the selection set when no fields are selected
- When selection set is empty an empty string is returned - Tests were modified to have cases with empty selection sets not show empty braces
1 parent 6acb643 commit 072341e

File tree

2 files changed

+24
-53
lines changed

2 files changed

+24
-53
lines changed

src/FieldTrait.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function setSelectionSet(array $selectionSet)
4040
*/
4141
protected function constructSelectionSet(): string
4242
{
43+
if (empty($this->selectionSet)) {
44+
return '';
45+
}
46+
4347
$attributesString = " {" . PHP_EOL;
4448
$first = true;
4549
foreach ($this->selectionSet as $attribute) {
@@ -63,4 +67,5 @@ protected function constructSelectionSet(): string
6367

6468
return $attributesString;
6569
}
66-
}
70+
}
71+

tests/QueryTest.php

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ public function testQueryWithoutFieldName()
5454
$query = new Query();
5555

5656
$this->assertEquals(
57-
"query {
58-
59-
}",
57+
"query",
6058
(string) $query
6159
);
6260

@@ -95,9 +93,7 @@ public function testQueryWithOperationName()
9593
->setOperationName('retrieveObject');
9694
$this->assertEquals(
9795
'query retrieveObject {
98-
Object {
99-
100-
}
96+
Object
10197
}',
10298
(string) $query
10399
);
@@ -118,9 +114,7 @@ public function testQueryWithOperationNameAndOperationType()
118114
->setSelectionSet([new Query('Object')]);
119115
$this->assertEquals(
120116
'query retrieveObject {
121-
Object {
122-
123-
}
117+
Object
124118
}',
125119
(string) $query
126120
);
@@ -141,9 +135,7 @@ public function testQueryWithOperationNameInSecondLevelDoesNothing()
141135
$this->assertEquals(
142136
'query retrieveObject {
143137
Object {
144-
Nested {
145-
146-
}
138+
Nested
147139
}
148140
}',
149141
(string) $query
@@ -174,9 +166,7 @@ public function testQueryWithOneVariable()
174166
->setVariables([new Variable('var', 'String')]);
175167
$this->assertEquals(
176168
'query($var: String) {
177-
Object {
178-
179-
}
169+
Object
180170
}',
181171
(string) $query
182172
);
@@ -196,9 +186,7 @@ public function testQueryWithMultipleVariables()
196186
->setVariables([new Variable('var', 'String'), new Variable('intVar', 'Int', false, 4)]);
197187
$this->assertEquals(
198188
'query($var: String $intVar: Int=4) {
199-
Object {
200-
201-
}
189+
Object
202190
}',
203191
(string) $query
204192
);
@@ -218,9 +206,7 @@ public function testQueryWithVariablesInSecondLevelDoesNothing()
218206
$this->assertEquals(
219207
'query($var: String $intVar: Int=4) {
220208
Object {
221-
Nested {
222-
223-
}
209+
Nested
224210
}
225211
}',
226212
(string) $query
@@ -241,9 +227,7 @@ public function testQueryWithOperationNameAndVariables()
241227
->setVariables([new Variable('var', 'String')]);
242228
$this->assertEquals(
243229
'query retrieveObject($var: String) {
244-
Object {
245-
246-
}
230+
Object
247231
}',
248232
(string) $query
249233
);
@@ -262,9 +246,7 @@ public function testEmptyQuery(Query $query)
262246
{
263247
$this->assertEquals(
264248
"query {
265-
Object {
266-
267-
}
249+
Object
268250
}",
269251
(string) $query,
270252
'Incorrect empty query string'
@@ -306,9 +288,7 @@ public function testStringArgumentValue(Query $query)
306288
$query->setArguments(['arg1' => 'value']);
307289
$this->assertEquals(
308290
"query {
309-
Object(arg1: \"value\") {
310-
311-
}
291+
Object(arg1: \"value\")
312292
}",
313293
(string) $query,
314294
'Query has improperly formatted parameter list'
@@ -332,9 +312,7 @@ public function testIntegerArgumentValue(Query $query)
332312
$query->setArguments(['arg1' => 23]);
333313
$this->assertEquals(
334314
"query {
335-
Object(arg1: 23) {
336-
337-
}
315+
Object(arg1: 23)
338316
}",
339317
(string) $query
340318
);
@@ -357,9 +335,7 @@ public function testBooleanArgumentValue(Query $query)
357335
$query->setArguments(['arg1' => true]);
358336
$this->assertEquals(
359337
"query {
360-
Object(arg1: true) {
361-
362-
}
338+
Object(arg1: true)
363339
}",
364340
(string) $query
365341
);
@@ -382,9 +358,7 @@ public function testNullArgumentValue(Query $query)
382358
$query->setArguments(['arg1' => null]);
383359
$this->assertEquals(
384360
"query {
385-
Object(arg1: null) {
386-
387-
}
361+
Object(arg1: null)
388362
}"
389363
, (string) $query
390364
);
@@ -407,9 +381,7 @@ public function testArrayIntegerArgumentValue(Query $query)
407381
$query->setArguments(['arg1' => [1, 2, 3]]);
408382
$this->assertEquals(
409383
"query {
410-
Object(arg1: [1, 2, 3]) {
411-
412-
}
384+
Object(arg1: [1, 2, 3])
413385
}",
414386
(string) $query
415387
);
@@ -433,9 +405,7 @@ public function testJsonObjectArgumentValue(Query $query)
433405
$query->setArguments(['obj' => new RawObject('{json_string_array: ["json value"]}')]);
434406
$this->assertEquals(
435407
"query {
436-
Object(obj: {json_string_array: [\"json value\"]}) {
437-
438-
}
408+
Object(obj: {json_string_array: [\"json value\"]})
439409
}"
440410
, (string) $query
441411
);
@@ -458,9 +428,7 @@ public function testArrayStringArgumentValue(Query $query)
458428
$query->setArguments(['arg1' => ['one', 'two', 'three']]);
459429
$this->assertEquals(
460430
"query {
461-
Object(arg1: [\"one\", \"two\", \"three\"]) {
462-
463-
}
431+
Object(arg1: [\"one\", \"two\", \"three\"])
464432
}",
465433
(string) $query
466434
);
@@ -485,9 +453,7 @@ public function testTwoOrMoreArguments(Query $query)
485453
$query->setArguments(['arg1' => 'val1', 'arg2' => 2, 'arg3' => true]);
486454
$this->assertEquals(
487455
"query {
488-
Object(arg1: \"val1\" arg2: 2 arg3: true) {
489-
490-
}
456+
Object(arg1: \"val1\" arg2: 2 arg3: true)
491457
}",
492458
(string) $query,
493459
'Query has improperly formatted parameter list'
@@ -715,4 +681,4 @@ public function testTwoLevelQueryWithInlineFragment(Query $query)
715681

716682
return $query;
717683
}
718-
}
684+
}

0 commit comments

Comments
 (0)