diff --git a/src/Deferred.php b/src/Deferred.php index 5182fb7..6e962de 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -1,23 +1,20 @@ isEmpty()) { + while ($q && ! $q->isEmpty()) { /** @var self $dfd */ $dfd = $q->dequeue(); $dfd->run(); @@ -38,7 +35,7 @@ class Deferred public function __construct(callable $callback) { $this->callback = $callback; - $this->promise = new SyncPromise(); + $this->promise = new SyncPromise(); self::getQueue()->enqueue($this); } @@ -47,7 +44,7 @@ class Deferred return $this->promise->then($onFulfilled, $onRejected); } - private function run() + public function run() : void { try { $cb = $this->callback; diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 2a94e38..511939e 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -35,6 +35,7 @@ use GraphQL\Utils\TypeInfo; use GraphQL\Utils\Utils; use function array_keys; use function array_merge; +use function array_reduce; use function array_values; use function get_class; use function is_array; @@ -68,7 +69,7 @@ class Executor self::$UNDEFINED = Utils::undefined(); } - $this->exeContext = $context; + $this->exeContext = $context; $this->subFieldCache = new \SplObjectStorage(); } @@ -285,7 +286,7 @@ class Executor // field and its descendants will be omitted, and sibling fields will still // be executed. An execution which encounters errors will still result in a // resolved Promise. - $data = $this->executeOperation($this->exeContext->operation, $this->exeContext->rootValue); + $data = $this->executeOperation($this->exeContext->operation, $this->exeContext->rootValue); $result = $this->buildResponse($data); // Note: we deviate here from the reference implementation a bit by always returning promise @@ -995,8 +996,7 @@ class Executor * If the callback does not return a Promise, then this function will also not * return a Promise. * - * @param mixed[] $values - * @param \Closure $callback + * @param mixed[] $values * @param Promise|mixed|null $initialValue * @return mixed[] */ @@ -1291,23 +1291,18 @@ class Executor return $this->executeFields($returnType, $result, $path, $subFieldNodes); } - /** - * @param ObjectType $returnType - * @param $fieldNodes - * @return ArrayObject - */ - private function collectSubFields(ObjectType $returnType, $fieldNodes): ArrayObject + private function collectSubFields(ObjectType $returnType, $fieldNodes) : ArrayObject { - if (!isset($this->subFieldCache[$returnType])) { + if (! isset($this->subFieldCache[$returnType])) { $this->subFieldCache[$returnType] = new \SplObjectStorage(); } - if (!isset($this->subFieldCache[$returnType][$fieldNodes])) { + if (! isset($this->subFieldCache[$returnType][$fieldNodes])) { // Collect sub-fields to execute to complete this value. - $subFieldNodes = new \ArrayObject(); + $subFieldNodes = new \ArrayObject(); $visitedFragmentNames = new \ArrayObject(); foreach ($fieldNodes as $fieldNode) { - if (!isset($fieldNode->selectionSet)) { + if (! isset($fieldNode->selectionSet)) { continue; } diff --git a/src/GraphQL.php b/src/GraphQL.php index d5c6237..f7915bb 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -1,4 +1,7 @@ wait($promise); } @@ -93,30 +103,23 @@ class GraphQL * Useful for Async PHP platforms. * * @api - * @param PromiseAdapter $promiseAdapter - * @param \GraphQL\Type\Schema $schema - * @param string|DocumentNode $source - * @param mixed $rootValue - * @param mixed $context - * @param array|null $variableValues - * @param string|null $operationName - * @param callable $fieldResolver - * @param array $validationRules - * - * @return Promise + * @param string|DocumentNode $source + * @param mixed $rootValue + * @param mixed $context + * @param mixed[]|null $variableValues + * @param ValidationRule[]|null $validationRules */ public static function promiseToExecute( PromiseAdapter $promiseAdapter, - \GraphQL\Type\Schema $schema, + SchemaType $schema, $source, $rootValue = null, $context = null, $variableValues = null, - $operationName = null, - callable $fieldResolver = null, - array $validationRules = null - ) - { + ?string $operationName = null, + ?callable $fieldResolver = null, + ?array $validationRules = null + ) : Promise { try { if ($source instanceof DocumentNode) { $documentNode = $source; @@ -125,36 +128,38 @@ class GraphQL } // FIXME - if (!empty($validationRules)) { - foreach ($validationRules as $rule) { - if ($rule instanceof QueryComplexity) { - $rule->setRawVariableValues($variableValues); - } - } - } else { + if (empty($validationRules)) { /** @var QueryComplexity $queryComplexity */ $queryComplexity = DocumentValidator::getRule(QueryComplexity::class); $queryComplexity->setRawVariableValues($variableValues); + } else { + foreach ($validationRules as $rule) { + if (! ($rule instanceof QueryComplexity)) { + continue; + } + + $rule->setRawVariableValues($variableValues); + } } $validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules); - if (!empty($validationErrors)) { + if (! empty($validationErrors)) { return $promiseAdapter->createFulfilled( new ExecutionResult(null, $validationErrors) ); - } else { - return Executor::promiseToExecute( - $promiseAdapter, - $schema, - $documentNode, - $rootValue, - $context, - $variableValues, - $operationName, - $fieldResolver - ); } + + return Executor::promiseToExecute( + $promiseAdapter, + $schema, + $documentNode, + $rootValue, + $context, + $variableValues, + $operationName, + $fieldResolver + ); } catch (Error $e) { return $promiseAdapter->createFulfilled( new ExecutionResult(null, [$e]) @@ -165,29 +170,28 @@ class GraphQL /** * @deprecated Use executeQuery()->toArray() instead * - * @param \GraphQL\Type\Schema $schema * @param string|DocumentNode $source - * @param mixed $rootValue - * @param mixed $contextValue - * @param array|null $variableValues - * @param string|null $operationName - * @return Promise|array + * @param mixed $rootValue + * @param mixed $contextValue + * @param mixed[]|null $variableValues + * @return Promise|mixed[] */ public static function execute( - \GraphQL\Type\Schema $schema, + SchemaType $schema, $source, $rootValue = null, $contextValue = null, $variableValues = null, - $operationName = null - ) - { + ?string $operationName = null + ) { trigger_error( __METHOD__ . ' is deprecated, use GraphQL::executeQuery()->toArray() as a quick replacement', E_USER_DEPRECATED ); - $result = self::promiseToExecute( - $promiseAdapter = Executor::getPromiseAdapter(), + + $promiseAdapter = Executor::getPromiseAdapter(); + $result = self::promiseToExecute( + $promiseAdapter, $schema, $source, $rootValue, @@ -199,40 +203,40 @@ class GraphQL if ($promiseAdapter instanceof SyncPromiseAdapter) { $result = $promiseAdapter->wait($result)->toArray(); } else { - $result = $result->then(function(ExecutionResult $r) { + $result = $result->then(function (ExecutionResult $r) { return $r->toArray(); }); } + return $result; } /** * @deprecated renamed to executeQuery() * - * @param \GraphQL\Type\Schema $schema * @param string|DocumentNode $source - * @param mixed $rootValue - * @param mixed $contextValue - * @param array|null $variableValues - * @param string|null $operationName + * @param mixed $rootValue + * @param mixed $contextValue + * @param mixed[]|null $variableValues * * @return ExecutionResult|Promise */ public static function executeAndReturnResult( - \GraphQL\Type\Schema $schema, + SchemaType $schema, $source, $rootValue = null, $contextValue = null, $variableValues = null, - $operationName = null - ) - { + ?string $operationName = null + ) { trigger_error( __METHOD__ . ' is deprecated, use GraphQL::executeQuery() as a quick replacement', E_USER_DEPRECATED ); - $result = self::promiseToExecute( - $promiseAdapter = Executor::getPromiseAdapter(), + + $promiseAdapter = Executor::getPromiseAdapter(); + $result = self::promiseToExecute( + $promiseAdapter, $schema, $source, $rootValue, @@ -240,9 +244,11 @@ class GraphQL $variableValues, $operationName ); + if ($promiseAdapter instanceof SyncPromiseAdapter) { $result = $promiseAdapter->wait($result); } + return $result; } @@ -252,7 +258,7 @@ class GraphQL * @api * @return Directive[] */ - public static function getStandardDirectives() + public static function getStandardDirectives() : array { return array_values(Directive::getInternalDirectives()); } @@ -263,7 +269,7 @@ class GraphQL * @api * @return Type[] */ - public static function getStandardTypes() + public static function getStandardTypes() : array { return array_values(Type::getInternalTypes()); } @@ -274,23 +280,17 @@ class GraphQL * @api * @return ValidationRule[] */ - public static function getStandardValidationRules() + public static function getStandardValidationRules() : array { return array_values(DocumentValidator::defaultRules()); } - /** - * @param callable $fn - */ - public static function setDefaultFieldResolver(callable $fn) + public static function setDefaultFieldResolver(callable $fn) : void { Executor::setDefaultFieldResolver($fn); } - /** - * @param PromiseAdapter|null $promiseAdapter - */ - public static function setPromiseAdapter(PromiseAdapter $promiseAdapter = null) + public static function setPromiseAdapter(?PromiseAdapter $promiseAdapter = null) : void { Executor::setPromiseAdapter($promiseAdapter); } @@ -301,7 +301,7 @@ class GraphQL * @deprecated Renamed to getStandardDirectives * @return Directive[] */ - public static function getInternalDirectives() + public static function getInternalDirectives() : array { return self::getStandardDirectives(); } diff --git a/src/Schema.php b/src/Schema.php index 39d500b..351047a 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -1,6 +1,12 @@