Developed with love by KnpLabs Hire us for your project!
51

dataloader-bundle

by overblog

This bundle allows to easy use DataLoaderPHP in your Symfony project through configuration.

DataLoaderBundle

This bundle allows to easy use DataLoaderPHP
in your Symfony 2 / 3 project by configuring it through configuration.

Build Status
Coverage Status
Latest Stable Version
License

Requirements

This library requires PHP >= 5.5 to work.

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

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"

Here the list of existing promise adapters:

Combine with GraphQLBundle

This bundle can be use with GraphQLBundle.
Here an example:

  • 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.

License

Overblog/DataLoaderBundle is released under the MIT license.

The MIT License (MIT)

Copyright (c) Overblog

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
overblog_dataloader:
factory: ~
defaults: # Required
promise_adapter: ~ # Required
factory: ~
options:
batch: true
max_batch_size: ~
cache: true
cache_key_fn: ~
cache_map: overblog_dataloader.cache_map
loaders:

# Prototype
name:
factory: ~
alias: ~
batch_load_fn: ~ # Required
options:
batch: true
max_batch_size: ~
cache: true
cache_key_fn: ~
cache_map: overblog_dataloader.cache_map
  • Add Symfony 5.0 support (#24)
    By web-flow, 8 months ago
  • Fix phpcs
    By web-flow, 1 year ago
  • Merge pull request #19 from mcg-web/support_symfony_4_2
    By web-flow, 1 year ago
  • Fix error unary operator expected
    By mcg-web, 1 year ago
  • Disable travis default php memory limit
    By mcg-web, 1 year ago
  • Optimized travis
    By mcg-web, 1 year ago
  • Remove deprecation messages
    By mcg-web, 1 year ago
  • Merge pull request #18 from cap-collectif/master
    By web-flow, 2 years ago
  • - Fixed phpcsfixer
    By , 2 years ago
  • - Added tests
    By Liinkiing, 2 years ago
  • - Modified regex to support FQCN service ids for callable nodes
    By Liinkiing, 2 years ago
  • Merge pull request #17 from ivoba/patch-1
    By web-flow, 2 years ago
  • Update README.md
    By web-flow, 2 years ago
  • Merge pull request #13 from ruudk/patch-1
    By web-flow, 2 years ago
  • Optimized travis builds
    By web-flow, 2 years ago
  • Travis > Test PHP 7.2
    By web-flow, 2 years ago
  • Merge pull request #12 from mcg-web/symfony_4
    By web-flow, 2 years ago
  • Add supports for Symfony 4
    By mcg-web, 2 years ago
  • Merge pull request #11 from rpander93/patch-1
    By web-flow, 2 years ago
  • Make CacheMap non-shared between DataLoaders
    By web-flow, 2 years ago
  • Merge pull request #6 from adri/patch-2
    By web-flow, 3 years ago
  • Merge pull request #7 from overblog/fix-hhvm-travis-build
    By web-flow, 3 years ago
  • Fix hhvm travis build
    By web-flow, 3 years ago
  • Fixes service name in docs
    By web-flow, 3 years ago
  • Add example using with GraphQLBundle
    By web-flow, 3 years ago
  • Bump master branch alias
    By web-flow, 3 years ago
  • Merge pull request #4 from adri/patch-1
    By web-flow, 3 years ago
  • Fix typo
    By web-flow, 3 years ago
  • Use `overblog/dataloader-php` 0.5 version
    By web-flow, 3 years ago
  • Fix Bundle path
    By web-flow, 3 years ago