dataloader-php
dataloader-php copied to clipboard
Unable to run dataloader for simple example
I am really struggling to create even a simple version of this using the documentation.
Here is my code:
use GuzzleHttp\Promise\Promise;
use Overblog\DataLoader\DataLoader;
use Overblog\PromiseAdapter\Adapter\GuzzleHttpPromiseAdapter;
class Sandbox
{
public function handle()
{
$myBatchGetUsers = function ($keys) {
echo "Running myBatchGetUsers()...\n";
$promise = new Promise();
$promise->then(function ($value) {
echo "Running data promise..."; // never runs!
return [
['name' => 'John'],
['name' => 'Sara'],
];
});
return $promise;
};
$promiseAdapter = new GuzzleHttpPromiseAdapter();
$userLoader = new DataLoader($myBatchGetUsers, $promiseAdapter);
$userLoader->load(4)
->then(function ($user) use ($userLoader) {
echo "{$user['name']}\n"; // never runs
});
$userLoader->load(5)
->then(function ($user) use ($userLoader) {
echo "{$user['name']}\n"; // never runs
});
$userLoader->await();
}
}
The expected output is:
Running myBatchGetUsers()...
Running data promise...
John
Sara
However the actual output is:
Running myBatchGetUsers()...
As you can see it is a very simple example, yet it does not work. What am I doing wrong?
Many Thanks :-)
Can you give this a try please:
use GuzzleHttp\Promise\Promise;
use Overblog\DataLoader\DataLoader;
use Overblog\PromiseAdapter\Adapter\GuzzleHttpPromiseAdapter;
class Sandbox
{
public function handle()
{
$promiseAdapter = new GuzzleHttpPromiseAdapter();
$myBatchGetUsers = function ($keys) use ($promiseAdapter) {
echo "Running myBatchGetUsers()...\n";
return $promiseAdapter->createAll([
['name' => 'John'],
['name' => 'Sara'],
]);
};
$userLoader = new DataLoader($myBatchGetUsers, $promiseAdapter);
$userLoader->load(4)
->then(function ($user) use ($userLoader) {
echo "{$user['name']}\n"; // never runs
});
$userLoader->load(5)
->then(function ($user) use ($userLoader) {
echo "{$user['name']}\n"; // never runs
});
$userLoader->await();
}
}
That worked. Thank you! Can you perhaps update the docs (or perhaps point me to the right direction) so that I can understand how GuzzleHttpPromiseAdapter actually works, as the README is very unclear on this part. Thank you!
Thanks for feedback we'll update the documentation to make this part a little clearer.