Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added async support to the interface. #969

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Controller/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Overblog\GraphQLBundle\Controller;

use GraphQL\Executor\Promise\Promise;
use Overblog\GraphQLBundle\Request\BatchParser;
use Overblog\GraphQLBundle\Request\Executor;
use Overblog\GraphQLBundle\Request\Parser;
Expand Down Expand Up @@ -53,15 +54,15 @@ public function batchEndpointAction(Request $request, string $schemaName = null)
/**
* @return JsonResponse|Response
*/
private function createResponse(Request $request, ?string $schemaName, bool $batched)
private function createResponse(Request $request, ?string $schemaName, bool $batched, bool $async = false)
{
if ('OPTIONS' === $request->getMethod()) {
$response = new JsonResponse([], 200);
} else {
if (!in_array($request->getMethod(), ['POST', 'GET'])) {
return new JsonResponse('', 405);
}
$payload = $this->processQuery($request, $schemaName, $batched);
$payload = $this->processQuery($request, $schemaName, $batched, $async);
$response = new JsonResponse($payload, 200);
}
$this->addCORSHeadersIfNeeded($response, $request);
Expand All @@ -80,12 +81,12 @@ private function addCORSHeadersIfNeeded(Response $response, Request $request): v
}
}

private function processQuery(Request $request, ?string $schemaName, bool $batched): array
private function processQuery(Request $request, ?string $schemaName, bool $batched, bool $async = false): array|Promise
{
if ($batched) {
$payload = $this->processBatchQuery($request, $schemaName);
} else {
$payload = $this->processNormalQuery($request, $schemaName);
$payload = $this->processNormalQuery($request, $schemaName, $async);
}

return $payload;
Expand All @@ -109,10 +110,14 @@ private function processBatchQuery(Request $request, string $schemaName = null):
return $payloads;
}

private function processNormalQuery(Request $request, string $schemaName = null): array
private function processNormalQuery(Request $request, string $schemaName = null, bool $async = false): array|Promise
{
$params = $this->requestParser->parse($request);

if ($async) {
return $this->requestExecutor->execute($schemaName, $params);
}

return $this->requestExecutor->execute($schemaName, $params)->toArray();
}
}
15 changes: 15 additions & 0 deletions src/Executor/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Overblog\GraphQLBundle\Executor;

use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
Expand Down Expand Up @@ -42,4 +43,18 @@ public function execute(

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

public function executeAsync(
PromiseAdapter $promiseAdapter,
Schema $schema,
string $requestString,
$rootValue = null,
$contextValue = null,
$variableValues = null,
$operationName = null,
?callable $fieldResolver = null,
?array $validationRules = null
): Promise {
return GraphQL::promiseToExecute(...func_get_args());
}
}
21 changes: 21 additions & 0 deletions src/Executor/ExecutorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

Expand All @@ -28,4 +29,24 @@ public function execute(
?callable $fieldResolver = null,
?array $validationRules = null
): ExecutionResult;

/**
* @param mixed $rootValue
* @param null $contextValue
* @param null $variableValues
* @param string|null $operationName
* @param array|null $validationRules
* @return Promise<ExecutionResult>
*/
public function executeAsync(
PromiseAdapter $promiseAdapter,
Schema $schema,
string $requestString,
$rootValue = null,
$contextValue = null,
$variableValues = null,
$operationName = null,
?callable $fieldResolver = null,
?array $validationRules = null
): Promise;
}
18 changes: 17 additions & 1 deletion src/Request/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayObject;
use Closure;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Type\Schema;
use GraphQL\Validator\DocumentValidator;
Expand Down Expand Up @@ -127,7 +128,7 @@ public function disableIntrospectionQuery(): void
/**
* @param array|ArrayObject|object|null $rootValue
*/
public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
public function execute(?string $schemaName, array $request, $rootValue = null, bool $async = false): ExecutionResult|Promise
{
$schema = $this->getSchema($schemaName);
/** @var string $schemaName */
Expand All @@ -145,6 +146,21 @@ public function execute(?string $schemaName, array $request, $rootValue = null):

$executorArgumentsEvent->getSchema()->processExtensions();

if ($async) {
return $this->executor->executeAsync(
$this->promiseAdapter,
$executorArgumentsEvent->getSchema(),
$executorArgumentsEvent->getRequestString(),
$executorArgumentsEvent->getRootValue(),
$executorArgumentsEvent->getContextValue(),
$executorArgumentsEvent->getVariableValue(),
$executorArgumentsEvent->getOperationName(),
$this->defaultFieldResolver
)->then(
fn ($result) => $this->postExecute($result, $executorArgumentsEvent)
);
}

$result = $this->executor->execute(
$this->promiseAdapter,
$executorArgumentsEvent->getSchema(),
Expand Down