Skip to content

Commit

Permalink
Merge pull request #14 from TomHAnderson/feature/introspection
Browse files Browse the repository at this point in the history
Use default field resolver if hydrator does not exist
  • Loading branch information
TomHAnderson committed Jun 27, 2018
2 parents dcdca69 + f97fe42 commit 47a5afd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Field/FieldResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Zend\Hydrator\HydratorPluginManager;
use Doctrine\Common\Util\ClassUtils;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Executor\Executor;
use ZF\Doctrine\GraphQL\Context;
use ZF\Doctrine\GraphQL\Hydrator\HydratorExtractToolInterface;

Expand All @@ -17,6 +18,7 @@
class FieldResolver
{
private $hydratorExtractTool;
private $hydratorManager;

/**
* Cache all hydrator extract operations based on spl object hash
Expand All @@ -25,9 +27,12 @@ class FieldResolver
*/
private $extractValues = [];

public function __construct(HydratorExtractToolInterface $hydratorExtractTool)
{
public function __construct(
HydratorExtractToolInterface $hydratorExtractTool,
HydratorPluginManager $hydratorManager
) {
$this->hydratorExtractTool = $hydratorExtractTool;
$this->hydratorManager = $hydratorManager;
}

public function __invoke($source, $args, Context $context, ResolveInfo $info)
Expand All @@ -40,6 +45,11 @@ public function __invoke($source, $args, Context $context, ResolveInfo $info)
$splObjectHash = spl_object_hash($source);
$hydratorAlias = 'ZF\\Doctrine\\GraphQL\\Hydrator\\' . str_replace('\\', '_', $entityClassName);

// If the hydrator does not exist pass handling to default Executor field resolver
if (! $this->hydratorManager->has($hydratorAlias)) {
return Executor::defaultFieldResolver($source, $args, $context, $info);
}

/**
* For disabled hydrator cache store only last hydrator result and reuse for consecutive calls
* then drop the cache if it doesn't hit.
Expand Down
3 changes: 2 additions & 1 deletion src/Field/FieldResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public function __invoke(
array $options = null
) {
$hydratorExtractTool = $container->get('ZF\\Doctrine\\GraphQL\\Hydrator\\HydratorExtractTool');
$hydratorManager = $container->get('HydratorManager');

return new FieldResolver($hydratorExtractTool);
return new FieldResolver($hydratorExtractTool, $hydratorManager);
}
}

0 comments on commit 47a5afd

Please sign in to comment.