dataloader-php icon indicating copy to clipboard operation
dataloader-php copied to clipboard

Unable to run dataloader for simple example

Open yahya-uddin opened this issue 7 years ago • 3 comments

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 :-)

yahya-uddin avatar Dec 02 '18 15:12 yahya-uddin

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();
    }
}

mcg-web avatar Dec 02 '18 16:12 mcg-web

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!

yahya-uddin avatar Dec 03 '18 00:12 yahya-uddin

Thanks for feedback we'll update the documentation to make this part a little clearer.

mcg-web avatar Dec 03 '18 06:12 mcg-web