mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-06 15:59:24 +03:00
commit
9327e75a16
@ -1,23 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL;
|
namespace GraphQL;
|
||||||
|
|
||||||
use GraphQL\Executor\Promise\Adapter\SyncPromise;
|
use GraphQL\Executor\Promise\Adapter\SyncPromise;
|
||||||
|
|
||||||
class Deferred
|
class Deferred
|
||||||
{
|
{
|
||||||
/**
|
/** @var \SplQueue */
|
||||||
* @var \SplQueue
|
|
||||||
*/
|
|
||||||
private static $queue;
|
private static $queue;
|
||||||
|
|
||||||
/**
|
/** @var callable */
|
||||||
* @var callable
|
|
||||||
*/
|
|
||||||
private $callback;
|
private $callback;
|
||||||
|
|
||||||
/**
|
/** @var SyncPromise */
|
||||||
* @var SyncPromise
|
|
||||||
*/
|
|
||||||
public $promise;
|
public $promise;
|
||||||
|
|
||||||
public static function getQueue()
|
public static function getQueue()
|
||||||
@ -47,7 +44,7 @@ class Deferred
|
|||||||
return $this->promise->then($onFulfilled, $onRejected);
|
return $this->promise->then($onFulfilled, $onRejected);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function run()
|
public function run() : void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$cb = $this->callback;
|
$cb = $this->callback;
|
||||||
|
@ -35,6 +35,7 @@ use GraphQL\Utils\TypeInfo;
|
|||||||
use GraphQL\Utils\Utils;
|
use GraphQL\Utils\Utils;
|
||||||
use function array_keys;
|
use function array_keys;
|
||||||
use function array_merge;
|
use function array_merge;
|
||||||
|
use function array_reduce;
|
||||||
use function array_values;
|
use function array_values;
|
||||||
use function get_class;
|
use function get_class;
|
||||||
use function is_array;
|
use function is_array;
|
||||||
@ -996,7 +997,6 @@ class Executor
|
|||||||
* return a Promise.
|
* return a Promise.
|
||||||
*
|
*
|
||||||
* @param mixed[] $values
|
* @param mixed[] $values
|
||||||
* @param \Closure $callback
|
|
||||||
* @param Promise|mixed|null $initialValue
|
* @param Promise|mixed|null $initialValue
|
||||||
* @return mixed[]
|
* @return mixed[]
|
||||||
*/
|
*/
|
||||||
@ -1291,11 +1291,6 @@ class Executor
|
|||||||
return $this->executeFields($returnType, $result, $path, $subFieldNodes);
|
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])) {
|
||||||
|
138
src/GraphQL.php
138
src/GraphQL.php
@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL;
|
namespace GraphQL;
|
||||||
|
|
||||||
use GraphQL\Error\Error;
|
use GraphQL\Error\Error;
|
||||||
@ -6,15 +9,19 @@ use GraphQL\Executor\ExecutionResult;
|
|||||||
use GraphQL\Executor\Executor;
|
use GraphQL\Executor\Executor;
|
||||||
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
|
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
|
||||||
use GraphQL\Executor\Promise\Promise;
|
use GraphQL\Executor\Promise\Promise;
|
||||||
|
use GraphQL\Executor\Promise\PromiseAdapter;
|
||||||
use GraphQL\Language\AST\DocumentNode;
|
use GraphQL\Language\AST\DocumentNode;
|
||||||
use GraphQL\Language\Parser;
|
use GraphQL\Language\Parser;
|
||||||
use GraphQL\Language\Source;
|
use GraphQL\Language\Source;
|
||||||
use GraphQL\Executor\Promise\PromiseAdapter;
|
|
||||||
use GraphQL\Type\Definition\Directive;
|
use GraphQL\Type\Definition\Directive;
|
||||||
use GraphQL\Type\Definition\Type;
|
use GraphQL\Type\Definition\Type;
|
||||||
|
use GraphQL\Type\Schema as SchemaType;
|
||||||
use GraphQL\Validator\DocumentValidator;
|
use GraphQL\Validator\DocumentValidator;
|
||||||
use GraphQL\Validator\Rules\ValidationRule;
|
|
||||||
use GraphQL\Validator\Rules\QueryComplexity;
|
use GraphQL\Validator\Rules\QueryComplexity;
|
||||||
|
use GraphQL\Validator\Rules\ValidationRule;
|
||||||
|
use function array_values;
|
||||||
|
use function trigger_error;
|
||||||
|
use const E_USER_DEPRECATED;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the primary facade for fulfilling GraphQL operations.
|
* This is the primary facade for fulfilling GraphQL operations.
|
||||||
@ -58,32 +65,35 @@ class GraphQL
|
|||||||
* queries which are validated before persisting and assumed valid during execution)
|
* queries which are validated before persisting and assumed valid during execution)
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @param \GraphQL\Type\Schema $schema
|
|
||||||
* @param string|DocumentNode $source
|
* @param string|DocumentNode $source
|
||||||
* @param mixed $rootValue
|
* @param mixed $rootValue
|
||||||
* @param mixed $context
|
* @param mixed $context
|
||||||
* @param array|null $variableValues
|
* @param mixed[]|null $variableValues
|
||||||
* @param string|null $operationName
|
* @param ValidationRule[] $validationRules
|
||||||
* @param callable $fieldResolver
|
|
||||||
* @param array $validationRules
|
|
||||||
*
|
|
||||||
* @return ExecutionResult
|
|
||||||
*/
|
*/
|
||||||
public static function executeQuery(
|
public static function executeQuery(
|
||||||
\GraphQL\Type\Schema $schema,
|
SchemaType $schema,
|
||||||
$source,
|
$source,
|
||||||
$rootValue = null,
|
$rootValue = null,
|
||||||
$context = null,
|
$context = null,
|
||||||
$variableValues = null,
|
$variableValues = null,
|
||||||
$operationName = null,
|
?string $operationName = null,
|
||||||
callable $fieldResolver = null,
|
?callable $fieldResolver = null,
|
||||||
array $validationRules = null
|
?array $validationRules = null
|
||||||
)
|
) : ExecutionResult {
|
||||||
{
|
|
||||||
$promiseAdapter = new SyncPromiseAdapter();
|
$promiseAdapter = new SyncPromiseAdapter();
|
||||||
|
|
||||||
$promise = self::promiseToExecute($promiseAdapter, $schema, $source, $rootValue, $context,
|
$promise = self::promiseToExecute(
|
||||||
$variableValues, $operationName, $fieldResolver, $validationRules);
|
$promiseAdapter,
|
||||||
|
$schema,
|
||||||
|
$source,
|
||||||
|
$rootValue,
|
||||||
|
$context,
|
||||||
|
$variableValues,
|
||||||
|
$operationName,
|
||||||
|
$fieldResolver,
|
||||||
|
$validationRules
|
||||||
|
);
|
||||||
|
|
||||||
return $promiseAdapter->wait($promise);
|
return $promiseAdapter->wait($promise);
|
||||||
}
|
}
|
||||||
@ -93,30 +103,23 @@ class GraphQL
|
|||||||
* Useful for Async PHP platforms.
|
* Useful for Async PHP platforms.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @param PromiseAdapter $promiseAdapter
|
|
||||||
* @param \GraphQL\Type\Schema $schema
|
|
||||||
* @param string|DocumentNode $source
|
* @param string|DocumentNode $source
|
||||||
* @param mixed $rootValue
|
* @param mixed $rootValue
|
||||||
* @param mixed $context
|
* @param mixed $context
|
||||||
* @param array|null $variableValues
|
* @param mixed[]|null $variableValues
|
||||||
* @param string|null $operationName
|
* @param ValidationRule[]|null $validationRules
|
||||||
* @param callable $fieldResolver
|
|
||||||
* @param array $validationRules
|
|
||||||
*
|
|
||||||
* @return Promise
|
|
||||||
*/
|
*/
|
||||||
public static function promiseToExecute(
|
public static function promiseToExecute(
|
||||||
PromiseAdapter $promiseAdapter,
|
PromiseAdapter $promiseAdapter,
|
||||||
\GraphQL\Type\Schema $schema,
|
SchemaType $schema,
|
||||||
$source,
|
$source,
|
||||||
$rootValue = null,
|
$rootValue = null,
|
||||||
$context = null,
|
$context = null,
|
||||||
$variableValues = null,
|
$variableValues = null,
|
||||||
$operationName = null,
|
?string $operationName = null,
|
||||||
callable $fieldResolver = null,
|
?callable $fieldResolver = null,
|
||||||
array $validationRules = null
|
?array $validationRules = null
|
||||||
)
|
) : Promise {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
if ($source instanceof DocumentNode) {
|
if ($source instanceof DocumentNode) {
|
||||||
$documentNode = $source;
|
$documentNode = $source;
|
||||||
@ -125,16 +128,18 @@ class GraphQL
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FIXME
|
// FIXME
|
||||||
if (!empty($validationRules)) {
|
if (empty($validationRules)) {
|
||||||
foreach ($validationRules as $rule) {
|
|
||||||
if ($rule instanceof QueryComplexity) {
|
|
||||||
$rule->setRawVariableValues($variableValues);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/** @var QueryComplexity $queryComplexity */
|
/** @var QueryComplexity $queryComplexity */
|
||||||
$queryComplexity = DocumentValidator::getRule(QueryComplexity::class);
|
$queryComplexity = DocumentValidator::getRule(QueryComplexity::class);
|
||||||
$queryComplexity->setRawVariableValues($variableValues);
|
$queryComplexity->setRawVariableValues($variableValues);
|
||||||
|
} else {
|
||||||
|
foreach ($validationRules as $rule) {
|
||||||
|
if (! ($rule instanceof QueryComplexity)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rule->setRawVariableValues($variableValues);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules);
|
$validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules);
|
||||||
@ -143,7 +148,8 @@ class GraphQL
|
|||||||
return $promiseAdapter->createFulfilled(
|
return $promiseAdapter->createFulfilled(
|
||||||
new ExecutionResult(null, $validationErrors)
|
new ExecutionResult(null, $validationErrors)
|
||||||
);
|
);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return Executor::promiseToExecute(
|
return Executor::promiseToExecute(
|
||||||
$promiseAdapter,
|
$promiseAdapter,
|
||||||
$schema,
|
$schema,
|
||||||
@ -154,7 +160,6 @@ class GraphQL
|
|||||||
$operationName,
|
$operationName,
|
||||||
$fieldResolver
|
$fieldResolver
|
||||||
);
|
);
|
||||||
}
|
|
||||||
} catch (Error $e) {
|
} catch (Error $e) {
|
||||||
return $promiseAdapter->createFulfilled(
|
return $promiseAdapter->createFulfilled(
|
||||||
new ExecutionResult(null, [$e])
|
new ExecutionResult(null, [$e])
|
||||||
@ -165,29 +170,28 @@ class GraphQL
|
|||||||
/**
|
/**
|
||||||
* @deprecated Use executeQuery()->toArray() instead
|
* @deprecated Use executeQuery()->toArray() instead
|
||||||
*
|
*
|
||||||
* @param \GraphQL\Type\Schema $schema
|
|
||||||
* @param string|DocumentNode $source
|
* @param string|DocumentNode $source
|
||||||
* @param mixed $rootValue
|
* @param mixed $rootValue
|
||||||
* @param mixed $contextValue
|
* @param mixed $contextValue
|
||||||
* @param array|null $variableValues
|
* @param mixed[]|null $variableValues
|
||||||
* @param string|null $operationName
|
* @return Promise|mixed[]
|
||||||
* @return Promise|array
|
|
||||||
*/
|
*/
|
||||||
public static function execute(
|
public static function execute(
|
||||||
\GraphQL\Type\Schema $schema,
|
SchemaType $schema,
|
||||||
$source,
|
$source,
|
||||||
$rootValue = null,
|
$rootValue = null,
|
||||||
$contextValue = null,
|
$contextValue = null,
|
||||||
$variableValues = null,
|
$variableValues = null,
|
||||||
$operationName = null
|
?string $operationName = null
|
||||||
)
|
) {
|
||||||
{
|
|
||||||
trigger_error(
|
trigger_error(
|
||||||
__METHOD__ . ' is deprecated, use GraphQL::executeQuery()->toArray() as a quick replacement',
|
__METHOD__ . ' is deprecated, use GraphQL::executeQuery()->toArray() as a quick replacement',
|
||||||
E_USER_DEPRECATED
|
E_USER_DEPRECATED
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$promiseAdapter = Executor::getPromiseAdapter();
|
||||||
$result = self::promiseToExecute(
|
$result = self::promiseToExecute(
|
||||||
$promiseAdapter = Executor::getPromiseAdapter(),
|
$promiseAdapter,
|
||||||
$schema,
|
$schema,
|
||||||
$source,
|
$source,
|
||||||
$rootValue,
|
$rootValue,
|
||||||
@ -203,36 +207,36 @@ class GraphQL
|
|||||||
return $r->toArray();
|
return $r->toArray();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated renamed to executeQuery()
|
* @deprecated renamed to executeQuery()
|
||||||
*
|
*
|
||||||
* @param \GraphQL\Type\Schema $schema
|
|
||||||
* @param string|DocumentNode $source
|
* @param string|DocumentNode $source
|
||||||
* @param mixed $rootValue
|
* @param mixed $rootValue
|
||||||
* @param mixed $contextValue
|
* @param mixed $contextValue
|
||||||
* @param array|null $variableValues
|
* @param mixed[]|null $variableValues
|
||||||
* @param string|null $operationName
|
|
||||||
*
|
*
|
||||||
* @return ExecutionResult|Promise
|
* @return ExecutionResult|Promise
|
||||||
*/
|
*/
|
||||||
public static function executeAndReturnResult(
|
public static function executeAndReturnResult(
|
||||||
\GraphQL\Type\Schema $schema,
|
SchemaType $schema,
|
||||||
$source,
|
$source,
|
||||||
$rootValue = null,
|
$rootValue = null,
|
||||||
$contextValue = null,
|
$contextValue = null,
|
||||||
$variableValues = null,
|
$variableValues = null,
|
||||||
$operationName = null
|
?string $operationName = null
|
||||||
)
|
) {
|
||||||
{
|
|
||||||
trigger_error(
|
trigger_error(
|
||||||
__METHOD__ . ' is deprecated, use GraphQL::executeQuery() as a quick replacement',
|
__METHOD__ . ' is deprecated, use GraphQL::executeQuery() as a quick replacement',
|
||||||
E_USER_DEPRECATED
|
E_USER_DEPRECATED
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$promiseAdapter = Executor::getPromiseAdapter();
|
||||||
$result = self::promiseToExecute(
|
$result = self::promiseToExecute(
|
||||||
$promiseAdapter = Executor::getPromiseAdapter(),
|
$promiseAdapter,
|
||||||
$schema,
|
$schema,
|
||||||
$source,
|
$source,
|
||||||
$rootValue,
|
$rootValue,
|
||||||
@ -240,9 +244,11 @@ class GraphQL
|
|||||||
$variableValues,
|
$variableValues,
|
||||||
$operationName
|
$operationName
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($promiseAdapter instanceof SyncPromiseAdapter) {
|
if ($promiseAdapter instanceof SyncPromiseAdapter) {
|
||||||
$result = $promiseAdapter->wait($result);
|
$result = $promiseAdapter->wait($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +258,7 @@ class GraphQL
|
|||||||
* @api
|
* @api
|
||||||
* @return Directive[]
|
* @return Directive[]
|
||||||
*/
|
*/
|
||||||
public static function getStandardDirectives()
|
public static function getStandardDirectives() : array
|
||||||
{
|
{
|
||||||
return array_values(Directive::getInternalDirectives());
|
return array_values(Directive::getInternalDirectives());
|
||||||
}
|
}
|
||||||
@ -263,7 +269,7 @@ class GraphQL
|
|||||||
* @api
|
* @api
|
||||||
* @return Type[]
|
* @return Type[]
|
||||||
*/
|
*/
|
||||||
public static function getStandardTypes()
|
public static function getStandardTypes() : array
|
||||||
{
|
{
|
||||||
return array_values(Type::getInternalTypes());
|
return array_values(Type::getInternalTypes());
|
||||||
}
|
}
|
||||||
@ -274,23 +280,17 @@ class GraphQL
|
|||||||
* @api
|
* @api
|
||||||
* @return ValidationRule[]
|
* @return ValidationRule[]
|
||||||
*/
|
*/
|
||||||
public static function getStandardValidationRules()
|
public static function getStandardValidationRules() : array
|
||||||
{
|
{
|
||||||
return array_values(DocumentValidator::defaultRules());
|
return array_values(DocumentValidator::defaultRules());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static function setDefaultFieldResolver(callable $fn) : void
|
||||||
* @param callable $fn
|
|
||||||
*/
|
|
||||||
public static function setDefaultFieldResolver(callable $fn)
|
|
||||||
{
|
{
|
||||||
Executor::setDefaultFieldResolver($fn);
|
Executor::setDefaultFieldResolver($fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static function setPromiseAdapter(?PromiseAdapter $promiseAdapter = null) : void
|
||||||
* @param PromiseAdapter|null $promiseAdapter
|
|
||||||
*/
|
|
||||||
public static function setPromiseAdapter(PromiseAdapter $promiseAdapter = null)
|
|
||||||
{
|
{
|
||||||
Executor::setPromiseAdapter($promiseAdapter);
|
Executor::setPromiseAdapter($promiseAdapter);
|
||||||
}
|
}
|
||||||
@ -301,7 +301,7 @@ class GraphQL
|
|||||||
* @deprecated Renamed to getStandardDirectives
|
* @deprecated Renamed to getStandardDirectives
|
||||||
* @return Directive[]
|
* @return Directive[]
|
||||||
*/
|
*/
|
||||||
public static function getInternalDirectives()
|
public static function getInternalDirectives() : array
|
||||||
{
|
{
|
||||||
return self::getStandardDirectives();
|
return self::getStandardDirectives();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace GraphQL;
|
namespace GraphQL;
|
||||||
|
|
||||||
|
use function trigger_error;
|
||||||
|
use const E_USER_DEPRECATED;
|
||||||
|
|
||||||
trigger_error(
|
trigger_error(
|
||||||
'GraphQL\Schema is moved to GraphQL\Type\Schema',
|
'GraphQL\Schema is moved to GraphQL\Type\Schema',
|
||||||
E_USER_DEPRECATED
|
E_USER_DEPRECATED
|
||||||
|
@ -4,11 +4,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace GraphQL\Server;
|
namespace GraphQL\Server;
|
||||||
|
|
||||||
use const CASE_LOWER;
|
|
||||||
use function array_change_key_case;
|
use function array_change_key_case;
|
||||||
use function is_string;
|
use function is_string;
|
||||||
use function json_decode;
|
use function json_decode;
|
||||||
use function json_last_error;
|
use function json_last_error;
|
||||||
|
use const CASE_LOWER;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Structure representing parsed HTTP parameters for GraphQL operation
|
* Structure representing parsed HTTP parameters for GraphQL operation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user