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

Commit

Permalink
Named resolver middleware (#300)
Browse files Browse the repository at this point in the history
Named resolver middleware
  • Loading branch information
crisu83 authored Oct 9, 2018
1 parent 9fbb8cd commit c851830
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/Schema/Resolver/AbstractResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ public function resolveType($rootValue, $context = null, ?ResolveInfo $info = nu
// Override this method when your resolver returns an interface or an union type.
return null;
}

/**
* @inheritdoc
*/
public function getMiddleware(): ?array
{
return null;
}
}
26 changes: 17 additions & 9 deletions src/Schema/Resolver/ResolverCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public function addResolver(string $fieldName, callable $resolver)
$this->resolvers[$fieldName] = $resolver;
}

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

/**
* @inheritdoc
*/
Expand All @@ -51,6 +60,14 @@ public function getTypeResolver(): ?callable
return $this->resolvers[static::TYPE_RESOLVER_KEY] ?? null;
}

/**
* @inheritdoc
*/
public function getMiddleware(): ?array
{
return null;
}

/**
* @param array $resolvers
*/
Expand All @@ -64,13 +81,4 @@ protected function registerResolvers(array $resolvers): void
$this->addResolver($typeName, $resolver);
}
}

/**
* @param string $fieldName
* @return callable|null
*/
protected function getResolver(string $fieldName): ?callable
{
return $this->resolvers[$fieldName] ?? null;
}
}
5 changes: 5 additions & 0 deletions src/Schema/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public function getResolveCallback(): ?callable;
* @return callable|null
*/
public function getTypeResolver(): ?callable;

/**
* @return array|null
*/
public function getMiddleware(): ?array;
}
26 changes: 24 additions & 2 deletions src/Schema/Resolver/ResolverRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getFieldResolver(string $typeName, string $fieldName): ?callable
$resolver = $this->getResolver($typeName);

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

$resolveCallback = $resolver instanceof ResolverInterface
Expand All @@ -56,7 +56,11 @@ public function getFieldResolver(string $typeName, string $fieldName): ?callable
}

if (null !== $this->middleware) {
return $this->applyMiddleware($resolveCallback, \array_reverse($this->middleware));
$middleware = $resolver instanceof ResolverInterface
? $this->getMiddlewareToApply($resolver, $this->middleware)
: $this->middleware;

return $this->applyMiddleware($resolveCallback, \array_reverse($middleware));
}

return $resolveCallback;
Expand Down Expand Up @@ -98,6 +102,24 @@ protected function registerResolvers(array $resolvers): void
}
}

/**
* @param ResolverInterface $resolver
* @param ResolverMiddlewareInterface[] $middleware
* @return array
*/
protected function getMiddlewareToApply(ResolverInterface $resolver, array $middleware): array
{
$resolverMiddleware = $resolver->getMiddleware();

if (null === $resolverMiddleware) {
return $middleware;
}

return \array_filter($middleware, function (ResolverMiddlewareInterface $mw) use ($resolverMiddleware) {
return \in_array(\get_class($mw), $resolverMiddleware, true);
});
}

/**
* @param callable $resolveCallback
* @param array $middleware
Expand Down
41 changes: 39 additions & 2 deletions tests/Unit/Schema/Resolver/ResolverRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Digia\GraphQL\Test\Unit\Schema\Resolver;

use Digia\GraphQL\Execution\ResolveInfo;
use Digia\GraphQL\Schema\Resolver\AbstractResolver;
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\ResolverMiddlewareInterface;
Expand Down Expand Up @@ -119,8 +119,9 @@ public function testMiddleware()
new LogResultMiddleware($logCallback)
]);

$registry->getFieldResolver('Query', 'hello')(null, ['name' => 'Bob']);
$result = $registry->getFieldResolver('Query', 'hello')(null, ['name' => 'Bob']);

$this->assertEquals('Hello Bob!', $result);
$this->assertEquals([
'1. logInput {"name":"Bob"}',
'2. logResult',
Expand All @@ -129,6 +130,32 @@ public function testMiddleware()
'5. logInput',
], $messages);
}

public function testResolverWithSpecificMiddleware()
{
$messages = [];

$logCallback = function (string $message) use (&$messages) {
$messages[] = $message;
};

$registry = new ResolverRegistry([
'Query' => [
'hello' => new HelloResolverWithSpecificMiddleware($logCallback),
],
], [
new LogInputMiddleware($logCallback),
new LogResultMiddleware($logCallback)
]);

$registry->getFieldResolver('Query', 'hello')(null, ['name' => 'Bob']);

$this->assertEquals([
'1. logInput {"name":"Bob"}',
'3. resolver: hello',
'5. logInput',
], $messages);
}
}

abstract class LogMiddleware implements ResolverMiddlewareInterface
Expand Down Expand Up @@ -205,3 +232,13 @@ public function resolve($rootValue, array $arguments, $context = null, ?ResolveI
return \sprintf('Hello %s!', $arguments['name'] ?? 'world');
}
}

class HelloResolverWithSpecificMiddleware extends HelloResolver
{
public function getMiddleware(): ?array
{
return [
LogInputMiddleware::class,
];
}
}

0 comments on commit c851830

Please sign in to comment.