mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 04:46:04 +03:00
Fix CS in src/Executor
This commit is contained in:
parent
0063bd6c15
commit
bfebcd7bee
@ -6,6 +6,7 @@ namespace GraphQL\Executor;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Error\FormattedError;
|
||||
use JsonSerializable;
|
||||
use function array_map;
|
||||
|
||||
/**
|
||||
@ -16,7 +17,7 @@ use function array_map;
|
||||
* Could be converted to [spec-compliant](https://facebook.github.io/graphql/#sec-Response-Format)
|
||||
* serializable array using `toArray()`
|
||||
*/
|
||||
class ExecutionResult implements \JsonSerializable
|
||||
class ExecutionResult implements JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Data collected from resolvers during query execution
|
||||
@ -77,8 +78,9 @@ class ExecutionResult implements \JsonSerializable
|
||||
* // ... other keys
|
||||
* );
|
||||
*
|
||||
* @api
|
||||
* @return self
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function setErrorFormatter(callable $errorFormatter)
|
||||
{
|
||||
@ -97,8 +99,9 @@ class ExecutionResult implements \JsonSerializable
|
||||
* return array_map($formatter, $errors);
|
||||
* }
|
||||
*
|
||||
* @api
|
||||
* @return self
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function setErrorsHandler(callable $handler)
|
||||
{
|
||||
@ -125,16 +128,18 @@ class ExecutionResult implements \JsonSerializable
|
||||
* $debug argument must be either bool (only adds "debugMessage" to result) or sum of flags from
|
||||
* GraphQL\Error\Debug
|
||||
*
|
||||
* @api
|
||||
* @param bool|int $debug
|
||||
*
|
||||
* @return mixed[]
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function toArray($debug = false)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
if (! empty($this->errors)) {
|
||||
$errorsHandler = $this->errorsHandler ?: function (array $errors, callable $formatter) {
|
||||
$errorsHandler = $this->errorsHandler ?: static function (array $errors, callable $formatter) {
|
||||
return array_map($formatter, $errors);
|
||||
};
|
||||
|
||||
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Executor;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayObject;
|
||||
use Exception;
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Error\InvariantViolation;
|
||||
use GraphQL\Error\Warning;
|
||||
@ -33,12 +35,18 @@ use GraphQL\Type\Introspection;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Utils\TypeInfo;
|
||||
use GraphQL\Utils\Utils;
|
||||
use RuntimeException;
|
||||
use SplObjectStorage;
|
||||
use stdClass;
|
||||
use Throwable;
|
||||
use Traversable;
|
||||
use function array_keys;
|
||||
use function array_merge;
|
||||
use function array_reduce;
|
||||
use function array_values;
|
||||
use function get_class;
|
||||
use function is_array;
|
||||
use function is_callable;
|
||||
use function is_object;
|
||||
use function is_string;
|
||||
use function sprintf;
|
||||
@ -52,7 +60,7 @@ class Executor
|
||||
private static $UNDEFINED;
|
||||
|
||||
/** @var callable|string[] */
|
||||
private static $defaultFieldResolver = [__CLASS__, 'defaultFieldResolver'];
|
||||
private static $defaultFieldResolver = [self::class, 'defaultFieldResolver'];
|
||||
|
||||
/** @var PromiseAdapter */
|
||||
private static $promiseAdapter;
|
||||
@ -60,7 +68,7 @@ class Executor
|
||||
/** @var ExecutionContext */
|
||||
private $exeContext;
|
||||
|
||||
/** @var \SplObjectStorage */
|
||||
/** @var SplObjectStorage */
|
||||
private $subFieldCache;
|
||||
|
||||
private function __construct(ExecutionContext $context)
|
||||
@ -70,13 +78,13 @@ class Executor
|
||||
}
|
||||
|
||||
$this->exeContext = $context;
|
||||
$this->subFieldCache = new \SplObjectStorage();
|
||||
$this->subFieldCache = new SplObjectStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom default resolve function
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function setDefaultFieldResolver(callable $fn)
|
||||
{
|
||||
@ -89,13 +97,14 @@ class Executor
|
||||
* Always returns ExecutionResult and never throws. All errors which occur during operation
|
||||
* execution are collected in `$result->errors`.
|
||||
*
|
||||
* @api
|
||||
* @param mixed|null $rootValue
|
||||
* @param mixed[]|null $contextValue
|
||||
* @param mixed[]|\ArrayAccess|null $variableValues
|
||||
* @param mixed[]|ArrayAccess|null $variableValues
|
||||
* @param string|null $operationName
|
||||
*
|
||||
* @return ExecutionResult|Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public static function execute(
|
||||
Schema $schema,
|
||||
@ -146,12 +155,14 @@ class Executor
|
||||
*
|
||||
* Useful for async PHP platforms.
|
||||
*
|
||||
* @api
|
||||
* @param mixed[]|null $rootValue
|
||||
* @param mixed[]|null $contextValue
|
||||
* @param mixed[]|null $variableValues
|
||||
* @param string|null $operationName
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public static function promiseToExecute(
|
||||
PromiseAdapter $promiseAdapter,
|
||||
@ -189,7 +200,7 @@ class Executor
|
||||
*
|
||||
* @param mixed[] $rootValue
|
||||
* @param mixed[] $contextValue
|
||||
* @param mixed[]|\Traversable $rawVariableValues
|
||||
* @param mixed[]|Traversable $rawVariableValues
|
||||
* @param string|null $operationName
|
||||
*
|
||||
* @return ExecutionContext|Error[]
|
||||
@ -297,7 +308,8 @@ class Executor
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|null|Promise $data
|
||||
* @param mixed|Promise|null $data
|
||||
*
|
||||
* @return ExecutionResult|Promise
|
||||
*/
|
||||
private function buildResponse($data)
|
||||
@ -317,12 +329,13 @@ class Executor
|
||||
* Implements the "Evaluating operations" section of the spec.
|
||||
*
|
||||
* @param mixed[] $rootValue
|
||||
* @return Promise|\stdClass|mixed[]
|
||||
*
|
||||
* @return Promise|stdClass|mixed[]
|
||||
*/
|
||||
private function executeOperation(OperationDefinitionNode $operation, $rootValue)
|
||||
{
|
||||
$type = $this->getOperationRootType($this->exeContext->schema, $operation);
|
||||
$fields = $this->collectFields($type, $operation->selectionSet, new \ArrayObject(), new \ArrayObject());
|
||||
$fields = $this->collectFields($type, $operation->selectionSet, new ArrayObject(), new ArrayObject());
|
||||
|
||||
$path = [];
|
||||
|
||||
@ -357,6 +370,7 @@ class Executor
|
||||
* Extracts the root type of the operation from the schema.
|
||||
*
|
||||
* @return ObjectType
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function getOperationRootType(Schema $schema, OperationDefinitionNode $operation)
|
||||
@ -411,7 +425,7 @@ class Executor
|
||||
* @param ArrayObject $fields
|
||||
* @param ArrayObject $visitedFragmentNames
|
||||
*
|
||||
* @return \ArrayObject
|
||||
* @return ArrayObject
|
||||
*/
|
||||
private function collectFields(
|
||||
ObjectType $runtimeType,
|
||||
@ -428,7 +442,7 @@ class Executor
|
||||
}
|
||||
$name = self::getFieldEntryKey($selection);
|
||||
if (! isset($fields[$name])) {
|
||||
$fields[$name] = new \ArrayObject();
|
||||
$fields[$name] = new ArrayObject();
|
||||
}
|
||||
$fields[$name][] = $selection;
|
||||
break;
|
||||
@ -475,6 +489,7 @@ class Executor
|
||||
* directives, where @skip has higher precedence than @include.
|
||||
*
|
||||
* @param FragmentSpreadNode|FieldNode|InlineFragmentNode $node
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function shouldIncludeNode($node)
|
||||
@ -521,6 +536,7 @@ class Executor
|
||||
* Determines if a fragment is applicable to the given type.
|
||||
*
|
||||
* @param FragmentDefinitionNode|InlineFragmentNode $fragment
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function doesFragmentConditionMatch(
|
||||
@ -551,7 +567,8 @@ class Executor
|
||||
* @param mixed[] $sourceValue
|
||||
* @param mixed[] $path
|
||||
* @param ArrayObject $fields
|
||||
* @return Promise|\stdClass|mixed[]
|
||||
*
|
||||
* @return Promise|stdClass|mixed[]
|
||||
*/
|
||||
private function executeFieldsSerially(ObjectType $parentType, $sourceValue, $path, $fields)
|
||||
{
|
||||
@ -567,7 +584,7 @@ class Executor
|
||||
}
|
||||
$promise = $this->getPromise($result);
|
||||
if ($promise) {
|
||||
return $promise->then(function ($resolvedResult) use ($responseName, $results) {
|
||||
return $promise->then(static function ($resolvedResult) use ($responseName, $results) {
|
||||
$results[$responseName] = $resolvedResult;
|
||||
return $results;
|
||||
});
|
||||
@ -578,7 +595,7 @@ class Executor
|
||||
[]
|
||||
);
|
||||
if ($this->isPromise($result)) {
|
||||
return $result->then(function ($resolvedResults) {
|
||||
return $result->then(static function ($resolvedResults) {
|
||||
return self::fixResultsIfEmptyArray($resolvedResults);
|
||||
});
|
||||
}
|
||||
@ -595,7 +612,7 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param mixed[] $path
|
||||
*
|
||||
* @return mixed[]|\Exception|mixed|null
|
||||
* @return mixed[]|Exception|mixed|null
|
||||
*/
|
||||
private function resolveField(ObjectType $parentType, $source, $fieldNodes, $path)
|
||||
{
|
||||
@ -705,7 +722,8 @@ class Executor
|
||||
* @param mixed $source
|
||||
* @param mixed $context
|
||||
* @param ResolveInfo $info
|
||||
* @return \Throwable|Promise|mixed
|
||||
*
|
||||
* @return Throwable|Promise|mixed
|
||||
*/
|
||||
private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $source, $context, $info)
|
||||
{
|
||||
@ -719,9 +737,9 @@ class Executor
|
||||
);
|
||||
|
||||
return $resolveFn($source, $args, $context, $info);
|
||||
} catch (\Exception $error) {
|
||||
} catch (Exception $error) {
|
||||
return $error;
|
||||
} catch (\Throwable $error) {
|
||||
} catch (Throwable $error) {
|
||||
return $error;
|
||||
}
|
||||
}
|
||||
@ -733,6 +751,7 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param string[] $path
|
||||
* @param mixed $result
|
||||
*
|
||||
* @return mixed[]|Promise|null
|
||||
*/
|
||||
private function completeValueCatchingError(
|
||||
@ -796,7 +815,9 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param string[] $path
|
||||
* @param mixed $result
|
||||
*
|
||||
* @return mixed[]|mixed|Promise|null
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
public function completeValueWithLocatedError(
|
||||
@ -829,9 +850,9 @@ class Executor
|
||||
}
|
||||
|
||||
return $completed;
|
||||
} catch (\Exception $error) {
|
||||
} catch (Exception $error) {
|
||||
throw Error::createLocatedError($error, $fieldNodes, $path);
|
||||
} catch (\Throwable $error) {
|
||||
} catch (Throwable $error) {
|
||||
throw Error::createLocatedError($error, $fieldNodes, $path);
|
||||
}
|
||||
}
|
||||
@ -860,9 +881,11 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param string[] $path
|
||||
* @param mixed $result
|
||||
*
|
||||
* @return mixed[]|mixed|Promise|null
|
||||
*
|
||||
* @throws Error
|
||||
* @throws \Throwable
|
||||
* @throws Throwable
|
||||
*/
|
||||
private function completeValue(
|
||||
Type $returnType,
|
||||
@ -880,7 +903,7 @@ class Executor
|
||||
});
|
||||
}
|
||||
|
||||
if ($result instanceof \Exception || $result instanceof \Throwable) {
|
||||
if ($result instanceof Exception || $result instanceof Throwable) {
|
||||
throw $result;
|
||||
}
|
||||
|
||||
@ -949,11 +972,12 @@ class Executor
|
||||
return $this->completeObjectValue($returnType, $fieldNodes, $info, $path, $result);
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('Cannot complete value of unexpected type "%s".', $returnType));
|
||||
throw new RuntimeException(sprintf('Cannot complete value of unexpected type "%s".', $returnType));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isPromise($value)
|
||||
@ -966,6 +990,7 @@ class Executor
|
||||
* otherwise returns null.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Promise|null
|
||||
*/
|
||||
private function getPromise($value)
|
||||
@ -998,14 +1023,15 @@ class Executor
|
||||
*
|
||||
* @param mixed[] $values
|
||||
* @param Promise|mixed|null $initialValue
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function promiseReduce(array $values, \Closure $callback, $initialValue)
|
||||
private function promiseReduce(array $values, callable $callback, $initialValue)
|
||||
{
|
||||
return array_reduce($values, function ($previous, $value) use ($callback) {
|
||||
$promise = $this->getPromise($previous);
|
||||
if ($promise) {
|
||||
return $promise->then(function ($resolved) use ($callback, $value) {
|
||||
return $promise->then(static function ($resolved) use ($callback, $value) {
|
||||
return $callback($resolved, $value);
|
||||
});
|
||||
}
|
||||
@ -1020,14 +1046,16 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param mixed[] $path
|
||||
* @param mixed $result
|
||||
*
|
||||
* @return mixed[]|Promise
|
||||
* @throws \Exception
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function completeListValue(ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
||||
{
|
||||
$itemType = $returnType->getWrappedType();
|
||||
Utils::invariant(
|
||||
is_array($result) || $result instanceof \Traversable,
|
||||
is_array($result) || $result instanceof Traversable,
|
||||
'User Error: expected iterable, but did not find one for field ' . $info->parentType . '.' . $info->fieldName . '.'
|
||||
);
|
||||
$containsPromise = false;
|
||||
@ -1051,20 +1079,22 @@ class Executor
|
||||
* Complete a Scalar or Enum by serializing to a valid value, throwing if serialization is not possible.
|
||||
*
|
||||
* @param mixed $result
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function completeLeafValue(LeafType $returnType, &$result)
|
||||
{
|
||||
try {
|
||||
return $returnType->serialize($result);
|
||||
} catch (\Exception $error) {
|
||||
} catch (Exception $error) {
|
||||
throw new InvariantViolation(
|
||||
'Expected a value of type "' . Utils::printSafe($returnType) . '" but received: ' . Utils::printSafe($result),
|
||||
0,
|
||||
$error
|
||||
);
|
||||
} catch (\Throwable $error) {
|
||||
} catch (Throwable $error) {
|
||||
throw new InvariantViolation(
|
||||
'Expected a value of type "' . Utils::printSafe($returnType) . '" but received: ' . Utils::printSafe($result),
|
||||
0,
|
||||
@ -1080,7 +1110,9 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param mixed[] $path
|
||||
* @param mixed[] $result
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function completeAbstractValue(AbstractType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
||||
@ -1142,6 +1174,7 @@ class Executor
|
||||
*
|
||||
* @param mixed|null $value
|
||||
* @param mixed|null $context
|
||||
*
|
||||
* @return ObjectType|Promise|null
|
||||
*/
|
||||
private function defaultTypeResolver($value, $context, ResolveInfo $info, AbstractType $abstractType)
|
||||
@ -1190,7 +1223,7 @@ class Executor
|
||||
|
||||
if (! empty($promisedIsTypeOfResults)) {
|
||||
return $this->exeContext->promises->all($promisedIsTypeOfResults)
|
||||
->then(function ($isTypeOfResults) use ($possibleTypes) {
|
||||
->then(static function ($isTypeOfResults) use ($possibleTypes) {
|
||||
foreach ($isTypeOfResults as $index => $result) {
|
||||
if ($result) {
|
||||
return $possibleTypes[$index];
|
||||
@ -1210,7 +1243,9 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param mixed[] $path
|
||||
* @param mixed $result
|
||||
* @return mixed[]|Promise|\stdClass
|
||||
*
|
||||
* @return mixed[]|Promise|stdClass
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function completeObjectValue(ObjectType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
||||
@ -1260,6 +1295,7 @@ class Executor
|
||||
/**
|
||||
* @param mixed[] $result
|
||||
* @param FieldNode[] $fieldNodes
|
||||
*
|
||||
* @return Error
|
||||
*/
|
||||
private function invalidReturnTypeError(
|
||||
@ -1277,7 +1313,9 @@ class Executor
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param mixed[] $path
|
||||
* @param mixed[] $result
|
||||
* @return mixed[]|Promise|\stdClass
|
||||
*
|
||||
* @return mixed[]|Promise|stdClass
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function collectAndExecuteSubfields(
|
||||
@ -1294,12 +1332,12 @@ class Executor
|
||||
private function collectSubFields(ObjectType $returnType, $fieldNodes) : ArrayObject
|
||||
{
|
||||
if (! isset($this->subFieldCache[$returnType])) {
|
||||
$this->subFieldCache[$returnType] = new \SplObjectStorage();
|
||||
$this->subFieldCache[$returnType] = new SplObjectStorage();
|
||||
}
|
||||
if (! isset($this->subFieldCache[$returnType][$fieldNodes])) {
|
||||
// Collect sub-fields to execute to complete this value.
|
||||
$subFieldNodes = new \ArrayObject();
|
||||
$visitedFragmentNames = new \ArrayObject();
|
||||
$subFieldNodes = new ArrayObject();
|
||||
$visitedFragmentNames = new ArrayObject();
|
||||
|
||||
foreach ($fieldNodes as $fieldNode) {
|
||||
if (! isset($fieldNode->selectionSet)) {
|
||||
@ -1325,7 +1363,8 @@ class Executor
|
||||
* @param mixed|null $source
|
||||
* @param mixed[] $path
|
||||
* @param ArrayObject $fields
|
||||
* @return Promise|\stdClass|mixed[]
|
||||
*
|
||||
* @return Promise|stdClass|mixed[]
|
||||
*/
|
||||
private function executeFields(ObjectType $parentType, $source, $path, $fields)
|
||||
{
|
||||
@ -1361,12 +1400,13 @@ class Executor
|
||||
* @see https://github.com/webonyx/graphql-php/issues/59
|
||||
*
|
||||
* @param mixed[] $results
|
||||
* @return \stdClass|mixed[]
|
||||
*
|
||||
* @return stdClass|mixed[]
|
||||
*/
|
||||
private static function fixResultsIfEmptyArray($results)
|
||||
{
|
||||
if ($results === []) {
|
||||
return new \stdClass();
|
||||
return new stdClass();
|
||||
}
|
||||
|
||||
return $results;
|
||||
@ -1380,6 +1420,7 @@ class Executor
|
||||
* any promises.
|
||||
*
|
||||
* @param (string|Promise)[] $assoc
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function promiseForAssocArray(array $assoc)
|
||||
@ -1389,7 +1430,7 @@ class Executor
|
||||
|
||||
$promise = $this->exeContext->promises->all($valuesAndPromises);
|
||||
|
||||
return $promise->then(function ($values) use ($keys) {
|
||||
return $promise->then(static function ($values) use ($keys) {
|
||||
$resolvedResults = [];
|
||||
foreach ($values as $i => $value) {
|
||||
$resolvedResults[$keys[$i]] = $value;
|
||||
@ -1403,6 +1444,7 @@ class Executor
|
||||
* @param string|ObjectType|null $runtimeTypeOrName
|
||||
* @param FieldNode[] $fieldNodes
|
||||
* @param mixed $result
|
||||
*
|
||||
* @return ObjectType
|
||||
*/
|
||||
private function ensureValidRuntimeType(
|
||||
@ -1471,7 +1513,7 @@ class Executor
|
||||
$fieldName = $info->fieldName;
|
||||
$property = null;
|
||||
|
||||
if (is_array($source) || $source instanceof \ArrayAccess) {
|
||||
if (is_array($source) || $source instanceof ArrayAccess) {
|
||||
if (isset($source[$fieldName])) {
|
||||
$property = $source[$fieldName];
|
||||
}
|
||||
@ -1481,6 +1523,6 @@ class Executor
|
||||
}
|
||||
}
|
||||
|
||||
return $property instanceof \Closure ? $property($source, $args, $context, $info) : $property;
|
||||
return is_callable($property) ? $property($source, $args, $context, $info) : $property;
|
||||
}
|
||||
}
|
||||
|
@ -80,12 +80,12 @@ class ReactPromiseAdapter implements PromiseAdapter
|
||||
// TODO: rework with generators when PHP minimum required version is changed to 5.5+
|
||||
$promisesOrValues = Utils::map(
|
||||
$promisesOrValues,
|
||||
function ($item) {
|
||||
static function ($item) {
|
||||
return $item instanceof Promise ? $item->adoptedPromise : $item;
|
||||
}
|
||||
);
|
||||
|
||||
$promise = all($promisesOrValues)->then(function ($values) use ($promisesOrValues) {
|
||||
$promise = all($promisesOrValues)->then(static function ($values) use ($promisesOrValues) {
|
||||
$orderedResults = [];
|
||||
|
||||
foreach ($promisesOrValues as $key => $value) {
|
||||
|
@ -4,8 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Executor\Promise\Adapter;
|
||||
|
||||
use Exception;
|
||||
use GraphQL\Executor\ExecutionResult;
|
||||
use GraphQL\Utils\Utils;
|
||||
use SplQueue;
|
||||
use Throwable;
|
||||
use function is_object;
|
||||
use function method_exists;
|
||||
@ -22,7 +24,7 @@ class SyncPromise
|
||||
const FULFILLED = 'fulfilled';
|
||||
const REJECTED = 'rejected';
|
||||
|
||||
/** @var \SplQueue */
|
||||
/** @var SplQueue */
|
||||
public static $queue;
|
||||
|
||||
/** @var string */
|
||||
@ -33,6 +35,7 @@ class SyncPromise
|
||||
|
||||
/**
|
||||
* Promises created in `then` method of this promise and awaiting for resolution of this promise
|
||||
*
|
||||
* @var mixed[][]
|
||||
*/
|
||||
private $waiting = [];
|
||||
@ -51,7 +54,7 @@ class SyncPromise
|
||||
switch ($this->state) {
|
||||
case self::PENDING:
|
||||
if ($value === $this) {
|
||||
throw new \Exception('Cannot resolve promise with self');
|
||||
throw new Exception('Cannot resolve promise with self');
|
||||
}
|
||||
if (is_object($value) && method_exists($value, 'then')) {
|
||||
$value->then(
|
||||
@ -72,11 +75,11 @@ class SyncPromise
|
||||
break;
|
||||
case self::FULFILLED:
|
||||
if ($this->result !== $value) {
|
||||
throw new \Exception('Cannot change value of fulfilled promise');
|
||||
throw new Exception('Cannot change value of fulfilled promise');
|
||||
}
|
||||
break;
|
||||
case self::REJECTED:
|
||||
throw new \Exception('Cannot resolve rejected promise');
|
||||
throw new Exception('Cannot resolve rejected promise');
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -84,8 +87,8 @@ class SyncPromise
|
||||
|
||||
public function reject($reason)
|
||||
{
|
||||
if (! $reason instanceof \Exception && ! $reason instanceof \Throwable) {
|
||||
throw new \Exception('SyncPromise::reject() has to be called with an instance of \Throwable');
|
||||
if (! $reason instanceof Exception && ! $reason instanceof Throwable) {
|
||||
throw new Exception('SyncPromise::reject() has to be called with an instance of \Throwable');
|
||||
}
|
||||
|
||||
switch ($this->state) {
|
||||
@ -96,11 +99,11 @@ class SyncPromise
|
||||
break;
|
||||
case self::REJECTED:
|
||||
if ($reason !== $this->result) {
|
||||
throw new \Exception('Cannot change rejection reason');
|
||||
throw new Exception('Cannot change rejection reason');
|
||||
}
|
||||
break;
|
||||
case self::FULFILLED:
|
||||
throw new \Exception('Cannot reject fulfilled promise');
|
||||
throw new Exception('Cannot reject fulfilled promise');
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -116,14 +119,14 @@ class SyncPromise
|
||||
foreach ($this->waiting as $descriptor) {
|
||||
self::getQueue()->enqueue(function () use ($descriptor) {
|
||||
/** @var $promise self */
|
||||
list($promise, $onFulfilled, $onRejected) = $descriptor;
|
||||
[$promise, $onFulfilled, $onRejected] = $descriptor;
|
||||
|
||||
if ($this->state === self::FULFILLED) {
|
||||
try {
|
||||
$promise->resolve($onFulfilled ? $onFulfilled($this->result) : $this->result);
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$promise->reject($e);
|
||||
} catch (\Throwable $e) {
|
||||
} catch (Throwable $e) {
|
||||
$promise->reject($e);
|
||||
}
|
||||
} elseif ($this->state === self::REJECTED) {
|
||||
@ -133,9 +136,9 @@ class SyncPromise
|
||||
} else {
|
||||
$promise->reject($this->result);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$promise->reject($e);
|
||||
} catch (\Throwable $e) {
|
||||
} catch (Throwable $e) {
|
||||
$promise->reject($e);
|
||||
}
|
||||
}
|
||||
@ -146,7 +149,7 @@ class SyncPromise
|
||||
|
||||
public static function getQueue()
|
||||
{
|
||||
return self::$queue ?: self::$queue = new \SplQueue();
|
||||
return self::$queue ?: self::$queue = new SplQueue();
|
||||
}
|
||||
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
|
||||
|
@ -4,12 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Executor\Promise\Adapter;
|
||||
|
||||
use Exception;
|
||||
use GraphQL\Deferred;
|
||||
use GraphQL\Error\InvariantViolation;
|
||||
use GraphQL\Executor\ExecutionResult;
|
||||
use GraphQL\Executor\Promise\Promise;
|
||||
use GraphQL\Executor\Promise\PromiseAdapter;
|
||||
use GraphQL\Utils\Utils;
|
||||
use Throwable;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
@ -67,9 +69,9 @@ class SyncPromiseAdapter implements PromiseAdapter
|
||||
'reject',
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$promise->reject($e);
|
||||
} catch (\Throwable $e) {
|
||||
} catch (Throwable $e) {
|
||||
$promise->reject($e);
|
||||
}
|
||||
|
||||
@ -111,7 +113,7 @@ class SyncPromiseAdapter implements PromiseAdapter
|
||||
if ($promiseOrValue instanceof Promise) {
|
||||
$result[$index] = null;
|
||||
$promiseOrValue->then(
|
||||
function ($value) use ($index, &$count, $total, &$result, $all) {
|
||||
static function ($value) use ($index, &$count, $total, &$result, $all) {
|
||||
$result[$index] = $value;
|
||||
$count++;
|
||||
if ($count < $total) {
|
||||
@ -167,7 +169,6 @@ class SyncPromiseAdapter implements PromiseAdapter
|
||||
|
||||
/**
|
||||
* Execute just before starting to run promise completion
|
||||
*
|
||||
*/
|
||||
protected function beforeWait(Promise $promise)
|
||||
{
|
||||
@ -175,7 +176,6 @@ class SyncPromiseAdapter implements PromiseAdapter
|
||||
|
||||
/**
|
||||
* Execute while running promise completion
|
||||
*
|
||||
*/
|
||||
protected function onWait(Promise $promise)
|
||||
{
|
||||
|
@ -24,14 +24,13 @@ class Promise
|
||||
*/
|
||||
public function __construct($adoptedPromise, PromiseAdapter $adapter)
|
||||
{
|
||||
Utils::invariant(! $adoptedPromise instanceof self, 'Expecting promise from adapted system, got ' . __CLASS__);
|
||||
Utils::invariant(! $adoptedPromise instanceof self, 'Expecting promise from adapted system, got ' . self::class);
|
||||
|
||||
$this->adapter = $adapter;
|
||||
$this->adoptedPromise = $adoptedPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
|
||||
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Executor\Promise;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Provides a means for integration of async PHP platforms ([related docs](data-fetching.md#async-php))
|
||||
*/
|
||||
@ -12,18 +14,22 @@ interface PromiseAdapter
|
||||
/**
|
||||
* Return true if the value is a promise or a deferred of the underlying platform
|
||||
*
|
||||
* @api
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function isThenable($value);
|
||||
|
||||
/**
|
||||
* Converts thenable of the underlying platform into GraphQL\Executor\Promise\Promise instance
|
||||
*
|
||||
* @api
|
||||
* @param object $thenable
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function convertThenable($thenable);
|
||||
|
||||
@ -31,9 +37,9 @@ interface PromiseAdapter
|
||||
* Accepts our Promise wrapper, extracts adopted promise out of it and executes actual `then` logic described
|
||||
* in Promises/A+ specs. Then returns new wrapped instance of GraphQL\Executor\Promise\Promise.
|
||||
*
|
||||
* @api
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null);
|
||||
|
||||
@ -43,17 +49,20 @@ interface PromiseAdapter
|
||||
* Expected resolver signature:
|
||||
* function(callable $resolve, callable $reject)
|
||||
*
|
||||
* @api
|
||||
* @return Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function create(callable $resolver);
|
||||
|
||||
/**
|
||||
* Creates a fulfilled Promise for a value if the value is not a promise.
|
||||
*
|
||||
* @api
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function createFulfilled($value = null);
|
||||
|
||||
@ -61,9 +70,11 @@ interface PromiseAdapter
|
||||
* Creates a rejected promise for a reason if the reason is not a promise. If
|
||||
* the provided reason is a promise, then it is returned as-is.
|
||||
*
|
||||
* @api
|
||||
* @param \Throwable $reason
|
||||
* @param Throwable $reason
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function createRejected($reason);
|
||||
|
||||
@ -71,9 +82,11 @@ interface PromiseAdapter
|
||||
* Given an array of promises (or values), returns a promise that is fulfilled when all the
|
||||
* items in the array are fulfilled.
|
||||
*
|
||||
* @api
|
||||
* @param Promise[]|mixed[] $promisesOrValues Promises or values.
|
||||
*
|
||||
* @return Promise
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function all(array $promisesOrValues);
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ use GraphQL\Utils\AST;
|
||||
use GraphQL\Utils\TypeInfo;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Utils\Value;
|
||||
use stdClass;
|
||||
use Throwable;
|
||||
use function array_key_exists;
|
||||
use function array_map;
|
||||
@ -41,6 +42,7 @@ class Values
|
||||
*
|
||||
* @param VariableDefinitionNode[] $varDefNodes
|
||||
* @param mixed[] $inputs
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function getVariableValues(Schema $schema, $varDefNodes, array $inputs)
|
||||
@ -125,7 +127,7 @@ class Values
|
||||
if (isset($node->directives) && $node->directives instanceof NodeList) {
|
||||
$directiveNode = Utils::find(
|
||||
$node->directives,
|
||||
function (DirectiveNode $directive) use ($directiveDef) {
|
||||
static function (DirectiveNode $directive) use ($directiveDef) {
|
||||
return $directive->name->value === $directiveDef->name;
|
||||
}
|
||||
);
|
||||
@ -145,7 +147,9 @@ class Values
|
||||
* @param FieldDefinition|Directive $def
|
||||
* @param FieldNode|DirectiveNode $node
|
||||
* @param mixed[] $variableValues
|
||||
*
|
||||
* @return mixed[]
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
public static function getArgumentValues($def, $node, $variableValues = null)
|
||||
@ -162,7 +166,7 @@ class Values
|
||||
/** @var ArgumentNode[] $argNodeMap */
|
||||
$argNodeMap = $argNodes ? Utils::keyMap(
|
||||
$argNodes,
|
||||
function (ArgumentNode $arg) {
|
||||
static function (ArgumentNode $arg) {
|
||||
return $arg->name->value;
|
||||
}
|
||||
) : [];
|
||||
@ -223,17 +227,20 @@ class Values
|
||||
* @deprecated as of 8.0 (Moved to \GraphQL\Utils\AST::valueFromAST)
|
||||
*
|
||||
* @param ValueNode $valueNode
|
||||
* @param null $variables
|
||||
* @return mixed[]|null|\stdClass
|
||||
* @param mixed[]|null $variables
|
||||
*
|
||||
* @return mixed[]|stdClass|null
|
||||
*/
|
||||
public static function valueFromAST($valueNode, InputType $type, $variables = null)
|
||||
public static function valueFromAST($valueNode, InputType $type, ?array $variables = null)
|
||||
{
|
||||
return AST::valueFromAST($valueNode, $type, $variables);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated as of 0.12 (Use coerceValue() directly for richer information)
|
||||
*
|
||||
* @param mixed[] $value
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public static function isValidPHPValue($value, InputType $type)
|
||||
@ -242,7 +249,7 @@ class Values
|
||||
|
||||
return $errors
|
||||
? array_map(
|
||||
function (Throwable $error) {
|
||||
static function (Throwable $error) {
|
||||
return $error->getMessage();
|
||||
},
|
||||
$errors
|
||||
|
Loading…
Reference in New Issue
Block a user