Skip to content

Commit

Permalink
Add PromiseAdapter to executor interface execute args
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed Jun 2, 2019
1 parent a14b662 commit df01253
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 38 deletions.
13 changes: 3 additions & 10 deletions UPGRADE-0.12.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,11 @@ Foo:
This section is only for users using custom executor.

The interface move to be look a little be more to `GraphQL\GraphQL`
execute method.
`promiseToExecute` method.

In `Overblog\GraphQLBundle\Executor\ExecutorInterface`
`setPromiseAdapter` and `setDefaultFieldResolver` has been removed.
To use promise adapter should now inject it using DI like this:

```yaml
services:
App\CustomExecutor:
calls:
- ["setPromiseAdapter", ['@overblog_graphql.promise_adapter']]
```

Default field resolver is now the 7th argument (`$fieldResolver`) of
Promise adapter is now the first argument (`$promiseAdapter`)
and default field resolver the 7th argument (`$fieldResolver`) of
`Overblog\GraphQLBundle\Executor\ExecutorInterface::execute` method.
13 changes: 3 additions & 10 deletions src/Executor/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@

class Executor implements ExecutorInterface
{
/** @var PromiseAdapter */
private $promiseAdapter;

/**
* {@inheritdoc}
*/
public function execute(
PromiseAdapter $promiseAdapter,
Schema $schema,
string $requestString,
$rootValue = null,
Expand All @@ -28,7 +26,7 @@ public function execute(
?callable $fieldResolver = null,
?array $validationRules = null
): ExecutionResult {
if ($this->promiseAdapter && !$this->promiseAdapter instanceof PromiseAdapterInterface && !\is_callable([$this->promiseAdapter, 'wait'])) {
if (!\method_exists($promiseAdapter, 'wait')) {
throw new \RuntimeException(
\sprintf(
'PromiseAdapter should be an object instantiating "%s" or "%s" with a "wait" method.',
Expand All @@ -38,11 +36,6 @@ public function execute(
);
}

return $this->promiseAdapter->wait(GraphQL::promiseToExecute($this->promiseAdapter, ...\func_get_args()));
}

public function setPromiseAdapter(PromiseAdapter $promiseAdapter): void
{
$this->promiseAdapter = $promiseAdapter;
return $promiseAdapter->wait(GraphQL::promiseToExecute(...\func_get_args()));
}
}
19 changes: 11 additions & 8 deletions src/Executor/ExecutorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
namespace Overblog\GraphQLBundle\Executor;

use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Type\Schema;

interface ExecutorInterface
{
/**
* @param Schema $schema
* @param string $requestString
* @param mixed $rootValue
* @param array|null $contextValue
* @param array|null $variableValues
* @param string|null $operationName
* @param callable|null $fieldResolver
* @param array|null $validationRules
* @param PromiseAdapter $promiseAdapter
* @param Schema $schema
* @param string $requestString
* @param mixed $rootValue
* @param array|null $contextValue
* @param array|null $variableValues
* @param string|null $operationName
* @param callable|null $fieldResolver
* @param array|null $validationRules
*
* @return ExecutionResult
*/
public function execute(
PromiseAdapter $promiseAdapter,
Schema $schema,
string $requestString,
$rootValue = null,
Expand Down
13 changes: 8 additions & 5 deletions src/Request/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Overblog\GraphQLBundle\Request;

use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Experimental\Executor\CoroutineExecutor;
use GraphQL\Type\Schema;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\DisableIntrospection;
Expand All @@ -23,24 +25,24 @@ class Executor
{
public const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter';

/** @var Schema[] */
private $schemas = [];

/** @var EventDispatcherInterface|null */
private $dispatcher;

/** @var ExecutorInterface */
private $promiseAdapter;

private $executor;

/** @var callable|null */
private $defaultFieldResolver;

public function __construct(
ExecutorInterface $executor,
PromiseAdapter $promiseAdapter,
EventDispatcherInterface $dispatcher,
?callable $defaultFieldResolver = null
) {
$this->executor = $executor;
$this->promiseAdapter = $promiseAdapter;
$this->dispatcher = $dispatcher;
$this->defaultFieldResolver = $defaultFieldResolver;
}
Expand Down Expand Up @@ -119,7 +121,7 @@ public function disableIntrospectionQuery(): void
*
* @return ExecutionResult
*/
public function execute(?string $schemaName = null, array $request, $rootValue = null): ExecutionResult
public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
{
$executorArgumentsEvent = $this->preExecute(
$this->getSchema($schemaName),
Expand All @@ -133,6 +135,7 @@ public function execute(?string $schemaName = null, array $request, $rootValue =
$executorArgumentsEvent->getSchema()->processExtensions();

$result = $this->executor->execute(
$this->promiseAdapter,
$executorArgumentsEvent->getSchema(),
$executorArgumentsEvent->getRequestString(),
$executorArgumentsEvent->getRootValue(),
Expand Down
5 changes: 5 additions & 0 deletions src/Resolver/AccessResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ private function checkAccessForStrictMode(callable $accessChecker, callable $res
if ($this->isThenable($promiseOrHasAccess)) {
return $this->createPromise($promiseOrHasAccess, $callback);
} else {
$promiseOrHasAccess = $this->extractAdoptedPromise($promiseOrHasAccess);
if ($promiseOrHasAccess instanceof SyncPromise) {
$promiseOrHasAccess = $promiseOrHasAccess->result;
}

return $callback($promiseOrHasAccess);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ services:
overblog_graphql.executor.default:
class: Overblog\GraphQLBundle\Executor\Executor
public: false
calls:
- ["setPromiseAdapter", ['@overblog_graphql.promise_adapter']]

overblog_graphql.request_executor:
class: Overblog\GraphQLBundle\Request\Executor
public: true
arguments:
- "@overblog_graphql.executor"
- "@overblog_graphql.promise_adapter"
- "@event_dispatcher"
- '%overblog_graphql.default_resolver%'
calls:
Expand Down
3 changes: 1 addition & 2 deletions tests/Executor/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function testInvalidExecutorAdapterPromise(): void
{
$schema = $this->getMockBuilder(Schema::class)->disableOriginalConstructor()->getMock();
$executor = new Executor();
$executor->setPromiseAdapter(new ReactPromiseAdapter());
$executor->execute($schema, '');
$executor->execute(new ReactPromiseAdapter(), $schema, '');
}
}
3 changes: 2 additions & 1 deletion tests/Request/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Overblog\GraphQLBundle\Tests\Request;

use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter;
use Overblog\GraphQLBundle\Executor\Executor;
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor;
use PHPUnit\Framework\TestCase;
Expand All @@ -19,6 +20,6 @@ public function testGetSchemaNoSchemaFound(): void
{
$dispatcher = $this->getMockBuilder(EventDispatcher::class)->setMethods(['dispatch'])->getMock();

(new RequestExecutor(new Executor(), $dispatcher))->getSchema('fake');
(new RequestExecutor(new Executor(), new ReactPromiseAdapter(), $dispatcher))->getSchema('fake');
}
}

0 comments on commit df01253

Please sign in to comment.