overblog / dataloader-bundle

This bundle allows to easy use DataLoaderPHP in your Symfony project through configuration.
MIT License
46 stars 14 forks source link

What is the point of this bundle #15

Closed akomm closed 6 years ago

akomm commented 6 years ago
  1. The following does not require this bundle, except the adapter, which purpose is neither explained nor obvious from readme:

    services:
    app.graph.ship_repository:
        class: AppBundle\Entity\Repository\ShipRepository
        factory: ["@doctrine.orm.entity_manager", getRepository]
        arguments:
            - AppBundle\Entity\Ship
    
    app.graph.ships_loader:
        class: AppBundle\GraphQL\Loader\ShipLoader
        arguments:
            - "@overblog_graphql.promise_adapter"
            - "@app.graph.ship_repository"
  2. Why do I need to define the adapter in another bundle:

    #graphql
    overblog_graphql:
    definitions:
        schema:
            query: Query
    services:
        promise_adapter: "webonyx_graphql.sync_promise_adapter"
  3. #dataloader
    overblog_dataloader:
    defaults:
        promise_adapter: "overblog_dataloader.webonyx_graphql_sync_promise_adapter"
    loaders:
        ships:
            alias: "ships_loader"
            batch_load_fn: "@app.graph.ships_loader:all"

    Again, what is the purpose of the adapter? I create service, to create a service which lets the adapter call a load_fn, but it seems from the following example, that I still have to somehow collect the IDs:

    public function resolveShip($shipsID)
    {
        $promise = $this->container->get('ships_loader')->load($shipsID);

        return $promise;
    }

So I could simply spare a lot of config and simply use the deffered from webonyx and collect my ids anyway. I could create a single service (instead of two) with almost zero config (using autoconfig/autowire) which does exactly this. Also the resolver usage does magically get shipIds, while actually the resolver would not get IDs, but the value/data being resolved.

The resolver usage combined with previous explanation feels like an "1. do this 2. ??? 3. profit?" currently ;(.

akomm commented 6 years ago

Is it possible, that you meant $shipID , instead of $shipsID in this:

    public function resolveShip($shipsID)
    {
        $promise = $this->container->get('ships_loader')->load($shipsID);

        return $promise;
    }

If that is the case, this would explain everything... Load adds ID and the adapter is a higher order promise, when resolved internally resolved the "ship_loader::all" with all the collected $ids.

edit Oh, it looks like. So the loader config creates service instances of DataLoader and DataLoader::load takes a key for a single item ands it to the queue.

The shipsID confused me being plural, which actually is not true, because it would be shipIDs.

ivoba commented 6 years ago

The shipsID confused me being plural, which actually is not true, because it would be shipIDs.

can we change this in the docs, i also stumbled on this.

Also an example how to chain a single id in the example would be good. When i understand correct load ind $this->container->get('ships_loader')->load($shipsID) is not in the example.

mcg-web commented 6 years ago

Yes @ivoba, do you want to submit a PR fixing this?

ivoba commented 6 years ago

@mcg-web i can fix the typo, sure. But i still dont understand how to chain an id in the load method. Can you explain this maybe?

ivoba commented 6 years ago

PR is made. As i understand now the load method of the DataLoader takes care of the queueing, so actually no more to do.

mcg-web commented 6 years ago

Thanks @ivoba (sorry for late reply!)

akomm commented 6 years ago

@ivoba Yes, whenever you call load($key) it adds the $key to a list for later resolve. It also preserves the order of $key added to list, so it can later resolve values from the resolved collection you return back to the proper load($key)-generate promise. And loadMany($keys) is simply the same, just saves you some imperative manual iteration over multiple $keys and returns a composed promise that resolves, when all forwarded load($key) promises resolve.