Skip to content

Commit

Permalink
Merge pull request #7 from TomHAnderson/feature/resolve-events
Browse files Browse the repository at this point in the history
Added RESOLVE and RESOLVE_POST events
  • Loading branch information
TomHAnderson authored Jun 21, 2018
2 parents d5677b8 + f35a72f commit 8af7326
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 6 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
zf-doctrine-graphql
===================

GraphQL for Doctrine using Hydrators
====================================

[![Build Status](https://travis-ci.org/API-Skeletons/zf-doctrine-graphql.svg)](https://travis-ci.org/API-Skeletons/zf-doctrine-graphql)
[![Coverage](https://coveralls.io/repos/github/API-Skeletons/zf-doctrine-graphql/badge.svg?branch=master&123)](https://coveralls.io/repos/github/API-Skeletons/zf-doctrine-graphql/badge.svg?branch=master&123)
Expand All @@ -15,7 +13,6 @@ Data is collected via hydrators thereby
allowing full control over each field using hydrator filters and strategies.
Multiple object managers are supported.
Multiple hydrator configurations are supported.
This library enables queries only.


Installation
Expand Down Expand Up @@ -322,6 +319,9 @@ Each of these tools takes a fully qualified entity name as a paramter allowing y
There is not a tool for mutations. Those are left to the developer to build.


Events
======

Filtering Query Builders
------------------------

Expand Down Expand Up @@ -356,3 +356,18 @@ $events->attach(
100
);
```


Resolve
-------

The `EntityResolveAbstractFactory::RESOLVE` event includes the paramters
and allows you to override the whole ResolveLoader event. This allows
you to have custom parameters and act on them through the ResolveLoader RESOLVE event.


Resolve Post
------------

The `EntityResolveAbstractFactory::RESOLVE_POST` event allows you to modify the values
returned from the ResolveLoader via an ArrayObject or replace the values.
45 changes: 44 additions & 1 deletion src/Resolve/EntityResolveAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Exception;
use ArrayObject;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand All @@ -18,6 +19,8 @@
final class EntityResolveAbstractFactory extends AbstractAbstractFactory implements
AbstractFactoryInterface
{
const RESOLVE = 'resolve';
const RESOLVE_POST = 'resolvePost';
const FILTER_QUERY_BUILDER = 'filterQueryBuilder';

protected $events;
Expand Down Expand Up @@ -95,6 +98,23 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$orderByManager
) {

// Allow listener to resolve function
$results = $this->getEventManager()->trigger(
self::RESOLVE,
$this,
[
'object' => $obj,
'arguments' => $args,
'context' => $context,
'hydrator' => $hydrator,
'objectManager' => $objectManager,
'entityClassName' => $requestedName,
]
);
if ($results->stopped()) {
return $results->last();
}

// Build query builder from Query Provider
$queryBuilder = ($objectManager->createQueryBuilder())
->select('row')
Expand All @@ -104,6 +124,9 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
self::FILTER_QUERY_BUILDER,
$this,
[
'object' => $obj,
'arguments' => $args,
'context' => $context,
'objectManager' => $objectManager,
'queryBuilder' => $queryBuilder,
'entityClassName' => $requestedName,
Expand Down Expand Up @@ -267,7 +290,27 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
}
}

return $matching;
// Allow listener to resolve post function
$matching = new ArrayObject($matching);
$results = $this->getEventManager()->trigger(
self::RESOLVE_POST,
$this,
[
'object' => $obj,
'arguments' => $args,
'context' => $context,
'matching' => $matching,
'hydrator' => $hydrator,
'objectManager' => $objectManager,
'queryBuilder' => $queryBuilder,
'entityClassName' => $requestedName,
]
);
if ($results->stopped()) {
return $results->last();
}

return $matching->getArrayCopy();
};

$this->cache($requestedName, $options, $instance);
Expand Down
2 changes: 1 addition & 1 deletion test/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function setUp()
$performance3->artist = $artist1;
$performance3->performanceDate = '2011-01-03';
$performance3->venue = 'venue3';
$performance3->attendance = 3000;
$performance3->attendance = 2000;
$performance3->isTradable = false;
$performance3->ticketPrice = 30.01;
$objectManager->persist($performance3);
Expand Down
92 changes: 92 additions & 0 deletions test/GraphQL/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,96 @@ function(Event $event)

$this->assertEquals(1, sizeof($output['data']['performance']));
}

/**
* @dataProvider schemaDataProvider
*/
public function testResolveEvent($schemaName, $context)
{
$schema = $this->getSchema($schemaName);

$container = $this->getApplication()->getServiceManager();
$events = $container->get('SharedEventManager');

$events->attach(
EntityResolveAbstractFactory::class,
EntityResolveAbstractFactory::RESOLVE,
function(Event $event)
{
$object = $event->getParam('object');
$arguments = $event->getParam('arguments');
$context = $event->getParam('context');
$hydrator = $event->getParam('hydrator');
$objectManager = $event->getParam('objectManager');
$entityClassName = $event->getParam('entityClassName');

$results = $objectManager->getRepository($entityClassName)->findBy([
'attendance' => 2000,
]);

$matching = [];
foreach ($results as $key => $value) {
$matching[$key] = $hydrator->extract($value);
}

$event->stopPropagation(true);

return $matching;
},
100
);

$query = "{ performance { id attendance } }";

$result = GraphQL::executeQuery($schema, $query, $rootValue = null, $context, $variableValues = null);
$output = $result->toArray();

$this->assertEquals(2, sizeof($output['data']['performance']));
}

/**
* @dataProvider schemaDataProvider
*/
public function testResolvePostEvent($schemaName, $context)
{
$schema = $this->getSchema($schemaName);

$container = $this->getApplication()->getServiceManager();
$events = $container->get('SharedEventManager');

$events->attach(
EntityResolveAbstractFactory::class,
EntityResolveAbstractFactory::RESOLVE_POST,
function(Event $event)
{
$objectManager = $event->getParam('objectManager');
$entityClassName = $event->getParam('entityClassName');
$matching = $event->getParam('matching');
$context = $event->getParam('context');
$hydrator = $event->getParam('hydrator');

$matching->exchangeArray([]); // Clear ArrayObject

$results = $objectManager->getRepository($entityClassName)->findBy([
'attendance' => 2000,
]);

foreach ($results as $key => $value) {
$matching[$key] = $hydrator->extract($value);
}

$event->stopPropagation(true);

return $matching;
},
100
);

$query = "{ performance { id attendance } }";

$result = GraphQL::executeQuery($schema, $query, $rootValue = null, $context, $variableValues = null);
$output = $result->toArray();

$this->assertEquals(2, sizeof($output['data']['performance']));
}
}

0 comments on commit 8af7326

Please sign in to comment.