Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Reintroduce type resolvers (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 authored Oct 9, 2018
1 parent c851830 commit 4934b63
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Digia\GraphQL\Execution\ResolveInfo;

abstract class AbstractResolver implements ResolverInterface
abstract class AbstractFieldResolver implements ResolverInterface
{
use ResolverTrait;

/**
* @param mixed $rootValue
* @param array $arguments
Expand All @@ -32,34 +34,4 @@ public function getResolveCallback(): ?callable
return $this->resolve($rootValue, $arguments, $context, $info);
};
}

/**
* @inheritdoc
*/
public function getTypeResolver(): ?callable
{
return function ($rootValue, $context = null, ?ResolveInfo $info = null) {
return $this->resolveType($rootValue, $context, $info);
};
}

/**
* @param mixed $rootValue
* @param mixed $context
* @param ResolveInfo|null $info
* @return callable|null
*/
public function resolveType($rootValue, $context = null, ?ResolveInfo $info = null): ?callable
{
// Override this method when your resolver returns an interface or an union type.
return null;
}

/**
* @inheritdoc
*/
public function getMiddleware(): ?array
{
return null;
}
}
33 changes: 33 additions & 0 deletions src/Schema/Resolver/AbstractTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Digia\GraphQL\Schema\Resolver;

abstract class AbstractTypeResolver implements ResolverCollectionInterface
{
use ResolverTrait;

/**
* @return callable|null
*/
public function getResolveCallback(): ?callable
{
return function (string $fieldName) {
return $this->getResolver($fieldName);
};
}

/**
* @param string $fieldName
* @return callable|null
*/
public function getResolver(string $fieldName): ?callable
{
$resolveMethod = 'resolve' . \ucfirst($fieldName);

if (\method_exists($this, $resolveMethod)) {
return [$this, $resolveMethod];
}

return null;
}
}
12 changes: 12 additions & 0 deletions src/Schema/Resolver/ResolverCollectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Digia\GraphQL\Schema\Resolver;

interface ResolverCollectionInterface extends ResolverInterface
{
/**
* @param string $fieldName
* @return callable|null
*/
public function getResolver(string $fieldName): ?callable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Digia\GraphQL\Schema\Resolver;

class ResolverCollection implements ResolverInterface
class ResolverMap implements ResolverCollectionInterface
{
protected const TYPE_RESOLVER_KEY = '__resolveType';

Expand Down Expand Up @@ -43,7 +43,7 @@ public function getResolver(string $fieldName): ?callable
*/
public function getResolveCallback(): ?callable
{
return function ($fieldName) {
return function (string $fieldName) {
$resolver = $this->getResolver($fieldName);

return $resolver instanceof ResolverInterface
Expand Down Expand Up @@ -75,7 +75,7 @@ protected function registerResolvers(array $resolvers): void
{
foreach ($resolvers as $typeName => $resolver) {
if (\is_array($resolver)) {
$resolver = new ResolverCollection($resolver);
$resolver = new ResolverMap($resolver);
}

$this->addResolver($typeName, $resolver);
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Resolver/ResolverRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getFieldResolver(string $typeName, string $fieldName): ?callable
{
$resolver = $this->getResolver($typeName);

$resolver = $resolver instanceof ResolverCollection
$resolver = $resolver instanceof ResolverCollectionInterface
? $resolver->getResolver($fieldName)
: $resolver;

Expand Down Expand Up @@ -95,7 +95,7 @@ protected function registerResolvers(array $resolvers): void
{
foreach ($resolvers as $typeName => $resolver) {
if (\is_array($resolver)) {
$resolver = new ResolverCollection($resolver);
$resolver = new ResolverMap($resolver);
}

$this->register($typeName, $resolver);
Expand Down
38 changes: 38 additions & 0 deletions src/Schema/Resolver/ResolverTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Digia\GraphQL\Schema\Resolver;

use Digia\GraphQL\Execution\ResolveInfo;

trait ResolverTrait
{
/**
* @return callable|null
*/
public function getTypeResolver(): ?callable
{
return function ($rootValue, $context = null, ?ResolveInfo $info = null) {
return $this->resolveType($rootValue, $context, $info);
};
}

/**
* @param mixed $rootValue
* @param mixed $context
* @param ResolveInfo|null $info
* @return callable|null
*/
public function resolveType($rootValue, $context = null, ?ResolveInfo $info = null): ?callable
{
// Override this method when your resolver returns an interface or an union type.
return null;
}

/**
* @return array|null
*/
public function getMiddleware(): ?array
{
return null;
}
}
45 changes: 29 additions & 16 deletions tests/Unit/Schema/Resolver/ResolverRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace Digia\GraphQL\Test\Unit\Schema\Resolver;

use Digia\GraphQL\Execution\ResolveInfo;
use Digia\GraphQL\Schema\Resolver\AbstractObjectResolver;
use Digia\GraphQL\Schema\Resolver\AbstractResolver;
use Digia\GraphQL\Schema\Resolver\AbstractResolverMiddleware;
use Digia\GraphQL\Schema\Resolver\ResolverCollection;
use Digia\GraphQL\Schema\Resolver\AbstractTypeResolver;
use Digia\GraphQL\Schema\Resolver\AbstractFieldResolver;
use Digia\GraphQL\Schema\Resolver\ResolverMiddlewareInterface;
use Digia\GraphQL\Schema\Resolver\ResolverRegistry;
use Digia\GraphQL\Test\TestCase;
Expand All @@ -16,7 +14,7 @@

class ResolverRegistryTest extends TestCase
{
public function testArrayResolver()
public function testResolverMap()
{
$registry = new ResolverRegistry([
'Query' => [
Expand All @@ -40,7 +38,19 @@ public function testArrayResolver()
], $registry->getFieldResolver('Query', 'droid')(null, ['id' => '2001']));
}

public function testResolverClassMap()
public function testTypeResolver()
{
$registry = new ResolverRegistry([
'Query' => new QueryResolver(),
]);

/** @noinspection PhpUnhandledExceptionInspection */
$this->assertArraySubset([
'name' => 'Luke Skywalker',
], $registry->getFieldResolver('Query', 'human')(null, ['id' => '1000']));
}

public function testFieldResolver()
{
$registry = new ResolverRegistry([
'Query' => [
Expand All @@ -58,9 +68,7 @@ public function testRegisterResolver()
{
$registry = new ResolverRegistry();

$registry->register('Query', new ResolverCollection([
'human' => new HumanResolver(),
]));
$registry->register('Query', new QueryResolver());

/** @noinspection PhpUnhandledExceptionInspection */
$this->assertArraySubset([
Expand Down Expand Up @@ -206,18 +214,23 @@ public function resolve(
}
}

class HumanResolver extends AbstractResolver
class QueryResolver extends AbstractTypeResolver
{
/**
* @inheritdoc
*/
public function resolve($rootValue, array $arguments, $context = null, ?ResolveInfo $info = null)
public function resolveHuman($rootValue, array $arguments, $context = null, ?ResolveInfo $info = null): array
{
return getHuman($arguments['id']);
}
}

class HumanResolver extends AbstractFieldResolver
{
public function resolve($rootValue, array $arguments, $context = null, ?ResolveInfo $info = null): array
{
return getHuman($arguments['id']);
}
}

class HelloResolver extends AbstractResolver
class HelloResolver extends AbstractFieldResolver
{
protected $logCallback;

Expand All @@ -226,7 +239,7 @@ public function __construct(callable $logCallback)
$this->logCallback = $logCallback;
}

public function resolve($rootValue, array $arguments, $context = null, ?ResolveInfo $info = null)
public function resolve($rootValue, array $arguments, $context = null, ?ResolveInfo $info = null): string
{
\call_user_func($this->logCallback, '3. resolver: hello');
return \sprintf('Hello %s!', $arguments['name'] ?? 'world');
Expand Down

0 comments on commit 4934b63

Please sign in to comment.