Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run dataloader for simple example #29

Open
yahya-uddin opened this issue Dec 2, 2018 · 3 comments
Open

Unable to run dataloader for simple example #29

yahya-uddin opened this issue Dec 2, 2018 · 3 comments

Comments

@yahya-uddin
Copy link

yahya-uddin commented Dec 2, 2018

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

@mcg-web
Copy link
Member

mcg-web commented Dec 2, 2018

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

@yahya-uddin
Copy link
Author

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!

@mcg-web
Copy link
Member

mcg-web commented Dec 3, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants