overblog / dataloader-php

DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
MIT License
199 stars 21 forks source link

Promise never triggered #36

Open videni opened 4 years ago

videni commented 4 years ago

I use the example here, but the promise returned by loadMany method never get triggered, why?

class CustomerLoader {

   /**
     * @var WebonyxGraphQLSyncPromiseAdapter
     */
    private $promiseAdapter;

    public function __construct($promiseAdapter)
    {
        $this->promiseAdapter = $promiseAdapter
    }

 public function all(array $userIds)
    {
        $users = CSessionInfo::query()
            ->whereIn('id', $userIds)
            ->get()
            ->groupBy('id');

        $indexed = array_reduce($userIds, function($curry, $current) use($users) {
             $curry[] = $users->has($current) ? $users->get($current): null;
             return $curry;
        }, []);

        return $this->promiseAdapter->all($indexed);
    }

  public function getLoader()
    {
        return new DataLoader(function(array $userIds){
            $this->all($userIds);
        }, $this->promiseAdapter);
    }

   // this method is mapped to author field .
    public function resolveAuthor($parent)
    {
        return $this->getLoader()->load($parent['authorId']);
    }
}

$graphQLPromiseAdapter = new SyncPromiseAdapter(); $dataLoaderPromiseAdapter = new WebonyxGraphQLSyncPromiseAdapter($graphQLPromiseAdapter); $userLoader = new DataLoader(function ($keys) { /.../ }, $dataLoaderPromiseAdapter);

GraphQL::setPromiseAdapter($graphQLPromiseAdapter);

simPod commented 2 years ago

@videni is it fixed or can you provide a test?