2017 © Pedro Peláez
 

symfony-bundle dataloader-bundle

DataLoader Symfony bundle implementation.

image

overblog/dataloader-bundle

DataLoader Symfony bundle implementation.

  • Wednesday, July 25, 2018
  • by Overblog
  • Repository
  • 8 Watchers
  • 21 Stars
  • 24,428 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 8 Versions
  • 30 % Grown

The README.md

DataLoaderBundle

This bundle allows to easy use DataLoaderPHP in your Symfony project by configuring it through configuration., (*1)

Build Status Coverage Status Latest Stable Version License, (*2)

Requirements

This library requires PHP >= 5.5 to work., (*3)

Installation

Download the Bundle

composer require overblog/dataloader-bundle

Enable the Bundle

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...

            new Overblog\DataLoaderBundle\OverblogDataLoaderBundle(),
        ];

        // ...
    }

    // ...
}

Getting Started

Here a fast example of how you can use the bundle, (*4)

overblog_dataloader:
    defaults:
        # required
        promise_adapter: "overblog_dataloader.react_promise_adapter"
        # optional
        factory: ~
        options:
            batch: true
            cache: true
            max_batch_size: ~
            cache_map: "overblog_dataloader.cache_map"
            cache_key_fn: ~
    loaders:
        users:
            alias: "users_dataloader"
            batch_load_fn: "@app.user:getUsers"
        posts: 
            batch_load_fn: "Post::getPosts"
            options:
                max_batch_size: 15
                batch: false
                cache: false
                cache_map: "app.cache.map"
                cache_key_fn: "@app.cache"
        images:
            factory: my_factory
            batch_load_fn: "Image\\Loader::get"

This example will create 3 loaders as services: - "@overblog_dataloader.users_loader" with alias "@users_dataloader" - "@overblog_dataloader.posts_loader" - "@overblog_dataloader.images_loader" create using custom factory function "my_factory", (*5)

Here the list of existing promise adapters:, (*6)

Combine with GraphQLBundle

This bundle can be use with GraphQLBundle. Here an example:, (*7)

  • Bundle config
#graphql
overblog_graphql:
    definitions:
        schema:
            query: Query
    services:
        promise_adapter: "webonyx_graphql.sync_promise_adapter"

#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"
  • Batch loader function
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"
<?php

namespace AppBundle\GraphQL\Loader;

use AppBundle\Entity\Repository\ShipRepository;
use GraphQL\Executor\Promise\PromiseAdapter;

class ShipLoader
{
    private $promiseAdapter;

    private $repository;

    public function __construct(PromiseAdapter $promiseAdapter, ShipRepository $repository)
    {
        $this->promiseAdapter = $promiseAdapter;
        $this->repository = $repository;
    }

    public function all(array $shipsIDs)
    {
        $qb = $this->repository->createQueryBuilder('s');
        $qb->add('where', $qb->expr()->in('s.id', ':ids'));
        $qb->setParameter('ids', $shipsIDs);
        $ships = $qb->getQuery()->getResult();

        return $this->promiseAdapter->all($ships);
    }
}
  • Usage in a resolver
    public function resolveShip($shipID)
    {
        $promise = $this->container->get('ships_loader')->load($shipID);

        return $promise;
    }

This is an example using the sync promise adapter of Webonyx., (*8)

License

Overblog/DataLoaderBundle is released under the MIT license., (*9)

The Versions

29/10 2017

v0.3.1

0.3.1.0

DataLoader Symfony bundle implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

caching batching dataloader

14/06 2017

v0.3.0

0.3.0.0

DataLoader Symfony bundle implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

caching batching dataloader

14/06 2017

dev-fix-hhvm-travis-build

dev-fix-hhvm-travis-build

DataLoader Symfony bundle implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

caching batching dataloader

09/02 2017

v0.2.0

0.2.0.0

DataLoader Symfony bundle implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

caching batching dataloader

04/02 2017

v0.1.0

0.1.0.0

DataLoader Symfony bundle implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

caching batching dataloader