Skip to content

Commit ee91c6a

Browse files
committed
Extend with Arguments which has a bunch of Arguments.
1 parent a0cc9f4 commit ee91c6a

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,72 @@ $searchType = (new Type('search'))
103103
);
104104

105105
echo (string) (new RootType('field'))->addSubType($searchType);
106+
```
107+
108+
## Build complex types
109+
110+
Its also possible to build complex types. This code examples show you how to do this.
111+
112+
### Arguments with array of arguments
113+
114+
Sometimes you want to have arrays with complex `Argument` types, like the following example.
115+
116+
```graphql
117+
{
118+
persons: [
119+
{age: 30},
120+
{age: 20},
121+
{age: 12}
122+
]
123+
}
124+
```
125+
126+
For this concept you can use the class `ArrayArgument` which give the possibility to add `Argument`s to an array.
127+
128+
```php
129+
<?php
130+
declare(strict_types=1);
131+
132+
use GraphQL\RequestBuilder\Argument;
133+
use GraphQL\RequestBuilder\ArrayArgument;
134+
135+
$persons = new ArrayArgument(
136+
'persons',
137+
[new Argument('age', 30), new Argument('age', 20), new Argument('age', 12)]
138+
);
139+
```
140+
141+
### Arrays with more than one Argument in value.
142+
143+
The example above works if you have an `array` with only one `Argument`: every *person* only has one `Argument`, the
144+
*age*. If you want to have more `Argument`s you need to create an `Argument`with an empty name.
145+
146+
```graphql
147+
{
148+
persons: [
149+
{
150+
name: "Hans",
151+
age: 30
152+
},
153+
{
154+
name: "Max",
155+
age: 20
156+
}
157+
]
158+
}
159+
```
160+
161+
Your *PHP* code should look like this:
162+
163+
```php
164+
<?php
165+
declare(strict_types=1);
166+
167+
use GraphQL\RequestBuilder\Argument;
168+
use GraphQL\RequestBuilder\ArrayArgument;
169+
170+
$person1 = new ArrayArgument('', [new Argument('name', 'Hans'), new Argument('age', 30)]);
171+
$person2 = new ArrayArgument('', [new Argument('name', 'Max'), new Argument('age', 20)]);
172+
173+
$persons = new ArrayArgument('persons', [$person1, $person2]);
106174
```

src/Argument.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class Argument implements ArgumentInterface
1717
{
1818
/** @var string Name of the argument. */
19-
private $name;
19+
protected $name;
2020

2121
/** @var int|float|string|bool|Argument[]|Argument The value of the argument. */
2222
private $value;
@@ -67,7 +67,16 @@ public function __toString(): string
6767
default:
6868
$value = null;
6969
}
70-
return $value === null ? '' : $this->name . ':' . $value;
70+
71+
if ($value === null) {
72+
return '';
73+
}
74+
75+
if ($this->name === '') {
76+
return trim($value, '{}');
77+
}
78+
79+
return $this->name . ':' . $value;
7180
}
7281

7382
/**

src/ArrayArgument.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ protected function buildStringFromArray(array $values): ?string
1818
{
1919
$returnArray = [];
2020
foreach ($values as $value) {
21-
$returnArray[] = '{' . (string) $value . '}';
21+
$returnArray[] = $this->name === '' ? (string) $value : '{' . $value . '}';
2222
}
23-
return '[' . implode(',', $returnArray) . ']';
23+
return $this->name === '' ? implode(',', $returnArray) : '[' . implode(',', $returnArray) . ']';
2424
}
2525
}

tests/ArrayArgumentTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ArrayArgumentTest extends TestCase
1919
private const ARGUMENT_NAME_INNER_2 = 'argumentNameInner2';
2020
private const ARGUMENT_VALUE_INT_POSITIVE = 2;
2121
private const ARGUMENT_VALUE_STRING = 'argumentValueString';
22+
private const ARGUMENT_KEY = 'argumentKey';
23+
private const ARGUMENT_KEY2 = 'argumentKey2';
2224

2325
public function testToStringArgumentArray(): void
2426
{
@@ -34,4 +36,24 @@ public function testToStringArgumentArray(): void
3436
'Setting array of Arguments should return correct string.'
3537
);
3638
}
39+
40+
public function testToStringArgumentAssociative(): void
41+
{
42+
$innerArgument = new ArrayArgument(
43+
'',
44+
[
45+
new Argument(self::ARGUMENT_KEY, self::ARGUMENT_VALUE_INT_POSITIVE),
46+
new Argument(self::ARGUMENT_KEY2, self::ARGUMENT_VALUE_STRING)
47+
]
48+
);
49+
50+
$argument = new ArrayArgument(self::ARGUMENT_NAME, $innerArgument);
51+
52+
static::assertEquals(
53+
self::ARGUMENT_NAME . ':[{' . self::ARGUMENT_KEY . ':' . self::ARGUMENT_VALUE_INT_POSITIVE . ','
54+
. self::ARGUMENT_KEY2 . ':"' . self::ARGUMENT_VALUE_STRING . '"}]',
55+
(string) $argument,
56+
'Setting array of Arguments should return correct string.'
57+
);
58+
}
3759
}

0 commit comments

Comments
 (0)