Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class WebonyxGraphQLSyncPromiseAdapter implements PromiseAdapterInterface

public function __construct(?SyncPromiseAdapter $webonyxPromiseAdapter = null)
{
$webonyxPromiseAdapter = $webonyxPromiseAdapter?:new SyncPromiseAdapter();
$webonyxPromiseAdapter = $webonyxPromiseAdapter?:new \Overblog\DataLoader\Promise\Adapter\Webonyx\GraphQL\SyncPromiseAdapter();
$this->setWebonyxPromiseAdapter($webonyxPromiseAdapter);
}

Expand Down
11 changes: 2 additions & 9 deletions src/DataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,11 @@ function ($values) use ($keys, $queue) {
sprintf('not return a Promise of an Array: %s.', gettype($values))
);
}
if (count($values) !== count($keys)) {
throw new \RuntimeException(
'DataLoader must be constructed with a function which accepts ' .
'Array<key> and returns Promise<Array<value>>, but the function did ' .
'not return a Promise of an Array of the same length as the Array of keys.'
);
}

// Step through the values, resolving or rejecting each Promise in the
// loaded queue.
foreach ($queue as $index => $data) {
$value = $values[$index];
foreach ($queue as $data) {
$value = $values[$data['key']] ?? null;
if ($value instanceof \Throwable) {
$data['reject']($value);
} else {
Expand Down
48 changes: 48 additions & 0 deletions src/Promise/Adapter/Webonyx/GraphQL/SyncPromiseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@

namespace Overblog\DataLoader\Promise\Adapter\Webonyx\GraphQL;

use Countable;
use Ds\Map;
use Exception;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter as BaseSyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise;
use Overblog\DataLoader\DataLoader;
use Throwable;
use function assert;
use function is_array;

class SyncPromiseAdapter extends BaseSyncPromiseAdapter
{
Expand All @@ -26,4 +34,44 @@ protected function onWait(Promise $promise): void
{
DataLoader::await();
}

/** @throws InvariantViolation */
public function all(iterable $promisesOrValues): Promise
{
assert(is_countable($promisesOrValues));

$all = new SyncPromise();

$total = count($promisesOrValues);
$count = 0;
/** @var Map<K, V|null> $result */
$result = new Map();

$resolveAllWhenFinished = function () use (&$count, &$total, $all, &$result, $promisesOrValues): void {
if ($count === $total) {
$all->resolve(is_array($promisesOrValues) ? $result->toArray() : $result);
}
};

foreach ($promisesOrValues as $index => $promiseOrValue) {
if ($promiseOrValue instanceof Promise) {
$result->put($index, null);
$promiseOrValue->then(
static function ($value) use ($result, $index, &$count, &$resolveAllWhenFinished): void {
$result->put($index, $value);
++$count;
$resolveAllWhenFinished();
},
static fn (Throwable $reason): SyncPromise => $all->reject($reason)
);
} else {
$result->put($index, $promiseOrValue);
++$count;
}
}

$resolveAllWhenFinished();

return new Promise($all, $this);
}
}
Loading