Skip to content

Commit 5e72999

Browse files
simPodspawnia
authored andcommitted
Allow iterable in PromiseAdapter::all()
1 parent 94df539 commit 5e72999

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ You can find and compare releases at the [GitHub release page](https://github.co
3737
- Always convert promises through `PromiseAdapter::convertThenable()` before calling `->then()` on them
3838
- Use `JSON_THROW_ON_ERROR` in `json_encode()`
3939
- Validate some internal invariants through `assert()`
40+
- `PromiseAdapter::all()` accepts `iterable`
4041

4142
### Added
4243

src/Executor/Promise/Adapter/AmpPromiseAdapter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use Amp\Promise as AmpPromise;
99
use Amp\Success;
1010
use function array_replace;
11+
use function assert;
1112
use GraphQL\Executor\Promise\Promise;
1213
use GraphQL\Executor\Promise\PromiseAdapter;
14+
use function is_array;
1315
use Throwable;
1416

1517
class AmpPromiseAdapter implements PromiseAdapter
@@ -77,8 +79,13 @@ public function createRejected(Throwable $reason): Promise
7779
return new Promise($promise, $this);
7880
}
7981

80-
public function all(array $promisesOrValues): Promise
82+
public function all(iterable $promisesOrValues): Promise
8183
{
84+
assert(
85+
is_array($promisesOrValues),
86+
'AmpPromiseAdapter::all(): Argument #1 ($promisesOrValues) must be of type array'
87+
);
88+
8289
/** @var array<AmpPromise<mixed>> $promises */
8390
$promises = [];
8491
foreach ($promisesOrValues as $key => $item) {

src/Executor/Promise/Adapter/ReactPromiseAdapter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ public function createRejected(Throwable $reason): Promise
5252
return new Promise($promise, $this);
5353
}
5454

55-
public function all(array $promisesOrValues): Promise
55+
public function all(iterable $promisesOrValues): Promise
5656
{
57+
assert(
58+
is_array($promisesOrValues),
59+
'ReactPromiseAdapter::all(): Argument #1 ($promisesOrValues) must be of type array'
60+
);
61+
5762
// TODO: rework with generators when PHP minimum required version is changed to 5.5+
5863

5964
foreach ($promisesOrValues as &$promiseOrValue) {

src/Executor/Promise/Adapter/SyncPromiseAdapter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace GraphQL\Executor\Promise\Adapter;
44

5+
use function assert;
56
use function count;
67
use GraphQL\Deferred;
78
use GraphQL\Error\InvariantViolation;
89
use GraphQL\Executor\Promise\Promise;
910
use GraphQL\Executor\Promise\PromiseAdapter;
1011
use GraphQL\Utils\Utils;
12+
use function is_array;
1113
use Throwable;
1214

1315
/**
@@ -72,8 +74,13 @@ public function createRejected(Throwable $reason): Promise
7274
return new Promise($promise->reject($reason), $this);
7375
}
7476

75-
public function all(array $promisesOrValues): Promise
77+
public function all(iterable $promisesOrValues): Promise
7678
{
79+
assert(
80+
is_array($promisesOrValues),
81+
'SyncPromiseAdapter::all(): Argument #1 ($promisesOrValues) must be of type array'
82+
);
83+
7784
$all = new SyncPromise();
7885

7986
$total = count($promisesOrValues);

src/Executor/Promise/PromiseAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public function createFulfilled($value = null): Promise;
6363
public function createRejected(Throwable $reason): Promise;
6464

6565
/**
66-
* Given an array of promises (or values), returns a promise that is fulfilled when all the
67-
* items in the array are fulfilled.
66+
* Given an iterable of promises (or values), returns a promise that is fulfilled when all the
67+
* items in the iterable are fulfilled.
6868
*
69-
* @param array<Promise|mixed> $promisesOrValues
69+
* @param iterable<Promise|mixed> $promisesOrValues
7070
*
7171
* @api
7272
*/
73-
public function all(array $promisesOrValues): Promise;
73+
public function all(iterable $promisesOrValues): Promise;
7474
}

0 commit comments

Comments
 (0)