From 4634f214eae4bb1fb5b409c4d8d9481a7ff9eea5 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Thu, 17 Aug 2017 02:12:37 +0700 Subject: [PATCH] Revamping GraphQL facade methods --- src/GraphQL.php | 159 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 105 insertions(+), 54 deletions(-) diff --git a/src/GraphQL.php b/src/GraphQL.php index bf56d65..a1ab229 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -11,6 +11,7 @@ use GraphQL\Language\Parser; use GraphQL\Language\Source; use GraphQL\Executor\Promise\PromiseAdapter; use GraphQL\Type\Definition\Directive; +use GraphQL\Type\Definition\Type; use GraphQL\Validator\DocumentValidator; use GraphQL\Validator\Rules\QueryComplexity; @@ -42,7 +43,7 @@ class GraphQL * fieldResolver: * A resolver function to use when one is not provided by the schema. * If not provided, the default field resolver is used (which looks for a - * value or method on the source value with the field's name). + * value on the source value with the field's name). * validationRules: * A set of rules for query validation step. Default value is all available rules. * Empty array would allow to skip query validation (may be convenient for persisted @@ -51,57 +52,6 @@ class GraphQL * @param \GraphQL\Type\Schema $schema * @param string|DocumentNode $source * @param mixed $rootValue - * @param array $contextValue - * @param array|null $variableValues - * @param string|null $operationName - * @param callable $fieldResolver - * @param array $validationRules - * @param PromiseAdapter $promiseAdapter - * - * @return Promise|array - */ - public static function execute( - \GraphQL\Type\Schema $schema, - $source, - $rootValue = null, - $contextValue = null, - $variableValues = null, - $operationName = null, - callable $fieldResolver = null, - array $validationRules = null, - PromiseAdapter $promiseAdapter = null - ) - { - $result = self::executeAndReturnResult( - $schema, - $source, - $rootValue, - $contextValue, - $variableValues, - $operationName, - $fieldResolver, - $validationRules, - $promiseAdapter - ); - - if ($result instanceof ExecutionResult) { - return $result->toArray(); - } - if ($result instanceof Promise) { - return $result->then(function(ExecutionResult $executionResult) { - return $executionResult->toArray(); - }); - } - throw new InvariantViolation("Unexpected execution result"); - } - - /** - * Same as `execute`, but returns instance of ExecutionResult instead of array, - * which can be used for custom error formatting or adding extensions to response - * - * @param \GraphQL\Type\Schema $schema - * @param string|DocumentNode $source - * @param mixed $rootValue * @param mixed $contextValue * @param array|null $variableValues * @param string|null $operationName @@ -111,7 +61,7 @@ class GraphQL * * @return ExecutionResult|Promise */ - public static function executeAndReturnResult( + public static function executeQuery( \GraphQL\Type\Schema $schema, $source, $rootValue = null, @@ -156,13 +106,114 @@ class GraphQL } /** - * @return array + * @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 + * @param callable $fieldResolver + * @param array $validationRules + * @param PromiseAdapter $promiseAdapter + * @return Promise|array + */ + public static function execute( + \GraphQL\Type\Schema $schema, + $source, + $rootValue = null, + $contextValue = null, + $variableValues = null, + $operationName = null, + callable $fieldResolver = null, + array $validationRules = null, + PromiseAdapter $promiseAdapter = null + ) + { + $result = self::executeQuery( + $schema, + $source, + $rootValue, + $contextValue, + $variableValues, + $operationName, + $fieldResolver, + $validationRules, + $promiseAdapter + ); + + if ($result instanceof ExecutionResult) { + return $result->toArray(); + } + if ($result instanceof Promise) { + return $result->then(function(ExecutionResult $executionResult) { + return $executionResult->toArray(); + }); + } + throw new InvariantViolation("Unexpected execution 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 callable $fieldResolver + * @param array $validationRules + * @param PromiseAdapter $promiseAdapter + * + * @return ExecutionResult|Promise + */ + public static function executeAndReturnResult( + \GraphQL\Type\Schema $schema, + $source, + $rootValue = null, + $contextValue = null, + $variableValues = null, + $operationName = null, + callable $fieldResolver = null, + array $validationRules = null, + PromiseAdapter $promiseAdapter = null + ) + { + return self::executeQuery( + $schema, + $source, + $rootValue, + $contextValue, + $variableValues, + $operationName, + $fieldResolver, + $validationRules, + $promiseAdapter + ); + } + + /** + * Returns directives defined in GraphQL spec + * + * @return Directive[] */ public static function getInternalDirectives() { return array_values(Directive::getInternalDirectives()); } + /** + * Returns types defined in GraphQL spec + * + * @return Type[] + */ + public static function getInternalTypes() + { + return Type::getInternalTypes(); + } + /** * @param callable $fn */