mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 04:46:04 +03:00
Merge pull request #508 from spawnia/consistency-and-clarity
Nonfunctional changes to improve consistency and clarity
This commit is contained in:
commit
9704baf422
@ -14,7 +14,7 @@ use GraphQL\Type\Schema;
|
|||||||
* Data that must be available at all points during query execution.
|
* Data that must be available at all points during query execution.
|
||||||
*
|
*
|
||||||
* Namely, schema of the type system that is currently executing,
|
* Namely, schema of the type system that is currently executing,
|
||||||
* and the fragments defined in the query document
|
* and the fragments defined in the query document.
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@ -45,7 +45,7 @@ class ExecutionContext
|
|||||||
public $errors;
|
public $errors;
|
||||||
|
|
||||||
/** @var PromiseAdapter */
|
/** @var PromiseAdapter */
|
||||||
public $promises;
|
public $promiseAdapter;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$schema,
|
$schema,
|
||||||
@ -53,7 +53,7 @@ class ExecutionContext
|
|||||||
$root,
|
$root,
|
||||||
$contextValue,
|
$contextValue,
|
||||||
$operation,
|
$operation,
|
||||||
$variables,
|
$variableValues,
|
||||||
$errors,
|
$errors,
|
||||||
$fieldResolver,
|
$fieldResolver,
|
||||||
$promiseAdapter
|
$promiseAdapter
|
||||||
@ -63,10 +63,10 @@ class ExecutionContext
|
|||||||
$this->rootValue = $root;
|
$this->rootValue = $root;
|
||||||
$this->contextValue = $contextValue;
|
$this->contextValue = $contextValue;
|
||||||
$this->operation = $operation;
|
$this->operation = $operation;
|
||||||
$this->variableValues = $variables;
|
$this->variableValues = $variableValues;
|
||||||
$this->errors = $errors ?: [];
|
$this->errors = $errors ?: [];
|
||||||
$this->fieldResolver = $fieldResolver;
|
$this->fieldResolver = $fieldResolver;
|
||||||
$this->promises = $promiseAdapter;
|
$this->promiseAdapter = $promiseAdapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addError(Error $error)
|
public function addError(Error $error)
|
||||||
|
@ -199,7 +199,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
public function doExecute() : Promise
|
public function doExecute() : Promise
|
||||||
{
|
{
|
||||||
// Return a Promise that will eventually resolve to the data described by
|
// Return a Promise that will eventually resolve to the data described by
|
||||||
// The "Response" section of the GraphQL specification.
|
// the "Response" section of the GraphQL specification.
|
||||||
//
|
//
|
||||||
// If errors are encountered while executing a GraphQL field, only that
|
// If errors are encountered while executing a GraphQL field, only that
|
||||||
// field and its descendants will be omitted, and sibling fields will still
|
// field and its descendants will be omitted, and sibling fields will still
|
||||||
@ -212,7 +212,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
// But for the "sync" case it is always fulfilled
|
// But for the "sync" case it is always fulfilled
|
||||||
return $this->isPromise($result)
|
return $this->isPromise($result)
|
||||||
? $result
|
? $result
|
||||||
: $this->exeContext->promises->createFulfilled($result);
|
: $this->exeContext->promiseAdapter->createFulfilled($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -262,7 +262,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
if ($error instanceof Error) {
|
if ($error instanceof Error) {
|
||||||
$this->exeContext->addError($error);
|
$this->exeContext->addError($error);
|
||||||
|
|
||||||
return $this->exeContext->promises->createFulfilled(null);
|
return $this->exeContext->promiseAdapter->createFulfilled(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -574,6 +574,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This method looks up the field on the given type definition.
|
* This method looks up the field on the given type definition.
|
||||||
|
*
|
||||||
* It has special casing for the two introspection fields, __schema
|
* It has special casing for the two introspection fields, __schema
|
||||||
* and __typename. __typename is special because it can always be
|
* and __typename. __typename is special because it can always be
|
||||||
* queried as a field, even in situations where no other fields
|
* queried as a field, even in situations where no other fields
|
||||||
@ -608,8 +609,8 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
|
* Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField` function.
|
||||||
* function. Returns the result of resolveFn or the abrupt-return Error object.
|
* Returns the result of resolveFn or the abrupt-return Error object.
|
||||||
*
|
*
|
||||||
* @param FieldDefinition $fieldDef
|
* @param FieldDefinition $fieldDef
|
||||||
* @param FieldNode $fieldNode
|
* @param FieldNode $fieldNode
|
||||||
@ -623,7 +624,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $source, $context, $info)
|
private function resolveOrError($fieldDef, $fieldNode, $resolveFn, $source, $context, $info)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// Build hash of arguments from the field.arguments AST, using the
|
// Build a map of arguments from the field.arguments AST, using the
|
||||||
// variables scope to fulfill any variable references.
|
// variables scope to fulfill any variable references.
|
||||||
$args = Values::getArgumentValues(
|
$args = Values::getArgumentValues(
|
||||||
$fieldDef,
|
$fieldDef,
|
||||||
@ -685,7 +686,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
function ($error) use ($exeContext) {
|
function ($error) use ($exeContext) {
|
||||||
$exeContext->addError($error);
|
$exeContext->addError($error);
|
||||||
|
|
||||||
return $this->exeContext->promises->createFulfilled(null);
|
return $this->exeContext->promiseAdapter->createFulfilled(null);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -732,7 +733,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
return $promise->then(
|
return $promise->then(
|
||||||
null,
|
null,
|
||||||
function ($error) use ($fieldNodes, $path) {
|
function ($error) use ($fieldNodes, $path) {
|
||||||
return $this->exeContext->promises->createRejected(Error::createLocatedError(
|
return $this->exeContext->promiseAdapter->createRejected(Error::createLocatedError(
|
||||||
$error,
|
$error,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
$path
|
$path
|
||||||
@ -864,7 +865,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
*/
|
*/
|
||||||
private function isPromise($value)
|
private function isPromise($value)
|
||||||
{
|
{
|
||||||
return $value instanceof Promise || $this->exeContext->promises->isThenable($value);
|
return $value instanceof Promise || $this->exeContext->promiseAdapter->isThenable($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -880,12 +881,12 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
if ($value === null || $value instanceof Promise) {
|
if ($value === null || $value instanceof Promise) {
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
if ($this->exeContext->promises->isThenable($value)) {
|
if ($this->exeContext->promiseAdapter->isThenable($value)) {
|
||||||
$promise = $this->exeContext->promises->convertThenable($value);
|
$promise = $this->exeContext->promiseAdapter->convertThenable($value);
|
||||||
if (! $promise instanceof Promise) {
|
if (! $promise instanceof Promise) {
|
||||||
throw new InvariantViolation(sprintf(
|
throw new InvariantViolation(sprintf(
|
||||||
'%s::convertThenable is expected to return instance of GraphQL\Executor\Promise\Promise, got: %s',
|
'%s::convertThenable is expected to return instance of GraphQL\Executor\Promise\Promise, got: %s',
|
||||||
get_class($this->exeContext->promises),
|
get_class($this->exeContext->promiseAdapter),
|
||||||
Utils::printSafe($promise)
|
Utils::printSafe($promise)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -927,28 +928,27 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Complete a list value by completing each item in the list with the
|
* Complete a list value by completing each item in the list with the inner type.
|
||||||
* inner type
|
|
||||||
*
|
*
|
||||||
* @param FieldNode[] $fieldNodes
|
* @param FieldNode[] $fieldNodes
|
||||||
* @param mixed[] $path
|
* @param mixed[] $path
|
||||||
* @param mixed $result
|
* @param mixed[]|Traversable $results
|
||||||
*
|
*
|
||||||
* @return mixed[]|Promise
|
* @return mixed[]|Promise
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function completeListValue(ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
private function completeListValue(ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$results)
|
||||||
{
|
{
|
||||||
$itemType = $returnType->getWrappedType();
|
$itemType = $returnType->getWrappedType();
|
||||||
Utils::invariant(
|
Utils::invariant(
|
||||||
is_array($result) || $result instanceof Traversable,
|
is_array($results) || $results instanceof Traversable,
|
||||||
'User Error: expected iterable, but did not find one for field ' . $info->parentType . '.' . $info->fieldName . '.'
|
'User Error: expected iterable, but did not find one for field ' . $info->parentType . '.' . $info->fieldName . '.'
|
||||||
);
|
);
|
||||||
$containsPromise = false;
|
$containsPromise = false;
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$completedItems = [];
|
$completedItems = [];
|
||||||
foreach ($result as $item) {
|
foreach ($results as $item) {
|
||||||
$fieldPath = $path;
|
$fieldPath = $path;
|
||||||
$fieldPath[] = $i++;
|
$fieldPath[] = $i++;
|
||||||
$info->path = $fieldPath;
|
$info->path = $fieldPath;
|
||||||
@ -959,7 +959,9 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
$completedItems[] = $completedItem;
|
$completedItems[] = $completedItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $containsPromise ? $this->exeContext->promises->all($completedItems) : $completedItems;
|
return $containsPromise
|
||||||
|
? $this->exeContext->promiseAdapter->all($completedItems)
|
||||||
|
: $completedItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1101,7 +1103,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (! empty($promisedIsTypeOfResults)) {
|
if (! empty($promisedIsTypeOfResults)) {
|
||||||
return $this->exeContext->promises->all($promisedIsTypeOfResults)
|
return $this->exeContext->promiseAdapter->all($promisedIsTypeOfResults)
|
||||||
->then(static function ($isTypeOfResults) use ($possibleTypes) {
|
->then(static function ($isTypeOfResults) use ($possibleTypes) {
|
||||||
foreach ($isTypeOfResults as $index => $result) {
|
foreach ($isTypeOfResults as $index => $result) {
|
||||||
if ($result) {
|
if ($result) {
|
||||||
@ -1187,7 +1189,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
/**
|
/**
|
||||||
* @param FieldNode[] $fieldNodes
|
* @param FieldNode[] $fieldNodes
|
||||||
* @param mixed[] $path
|
* @param mixed[] $path
|
||||||
* @param mixed[] $result
|
* @param mixed $result
|
||||||
*
|
*
|
||||||
* @return mixed[]|Promise|stdClass
|
* @return mixed[]|Promise|stdClass
|
||||||
*
|
*
|
||||||
@ -1299,7 +1301,7 @@ class ReferenceExecutor implements ExecutorImplementation
|
|||||||
{
|
{
|
||||||
$keys = array_keys($assoc);
|
$keys = array_keys($assoc);
|
||||||
$valuesAndPromises = array_values($assoc);
|
$valuesAndPromises = array_values($assoc);
|
||||||
$promise = $this->exeContext->promises->all($valuesAndPromises);
|
$promise = $this->exeContext->promiseAdapter->all($valuesAndPromises);
|
||||||
|
|
||||||
return $promise->then(static function ($values) use ($keys) {
|
return $promise->then(static function ($values) use ($keys) {
|
||||||
$resolvedResults = [];
|
$resolvedResults = [];
|
||||||
|
@ -21,7 +21,7 @@ use function array_merge_recursive;
|
|||||||
class ResolveInfo
|
class ResolveInfo
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The name of the field being resolved
|
* The name of the field being resolved.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var string
|
* @var string
|
||||||
@ -37,7 +37,7 @@ class ResolveInfo
|
|||||||
public $fieldNodes = [];
|
public $fieldNodes = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expected return type of the field being resolved
|
* Expected return type of the field being resolved.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var ScalarType|ObjectType|InterfaceType|UnionType|EnumType|ListOfType|NonNull
|
* @var ScalarType|ObjectType|InterfaceType|UnionType|EnumType|ListOfType|NonNull
|
||||||
@ -45,7 +45,7 @@ class ResolveInfo
|
|||||||
public $returnType;
|
public $returnType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parent type of the field being resolved
|
* Parent type of the field being resolved.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var ObjectType
|
* @var ObjectType
|
||||||
@ -53,7 +53,7 @@ class ResolveInfo
|
|||||||
public $parentType;
|
public $parentType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to this field from the very root value
|
* Path to this field from the very root value.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var string[][]
|
* @var string[][]
|
||||||
@ -61,7 +61,7 @@ class ResolveInfo
|
|||||||
public $path;
|
public $path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instance of a schema used for execution
|
* Instance of a schema used for execution.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var Schema
|
* @var Schema
|
||||||
@ -69,7 +69,7 @@ class ResolveInfo
|
|||||||
public $schema;
|
public $schema;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AST of all fragments defined in query
|
* AST of all fragments defined in query.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var FragmentDefinitionNode[]
|
* @var FragmentDefinitionNode[]
|
||||||
@ -77,15 +77,15 @@ class ResolveInfo
|
|||||||
public $fragments = [];
|
public $fragments = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Root value passed to query execution
|
* Root value passed to query execution.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var mixed|null
|
* @var mixed
|
||||||
*/
|
*/
|
||||||
public $rootValue;
|
public $rootValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AST of operation definition node (query, mutation)
|
* AST of operation definition node (query, mutation).
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var OperationDefinitionNode|null
|
* @var OperationDefinitionNode|null
|
||||||
@ -93,7 +93,7 @@ class ResolveInfo
|
|||||||
public $operation;
|
public $operation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Array of variables passed to query execution
|
* Array of variables passed to query execution.
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
* @var mixed[]
|
* @var mixed[]
|
||||||
@ -137,7 +137,7 @@ class ResolveInfo
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper method that returns names of all fields selected in query for
|
* Helper method that returns names of all fields selected in query for
|
||||||
* $this->fieldName up to $depth levels
|
* $this->fieldName up to $depth levels.
|
||||||
*
|
*
|
||||||
* Example:
|
* Example:
|
||||||
* query MyQuery{
|
* query MyQuery{
|
||||||
|
@ -21,7 +21,7 @@ use function is_string;
|
|||||||
* Similar to PHP array, but allows any type of data to act as key (including arrays, objects, scalars)
|
* Similar to PHP array, but allows any type of data to act as key (including arrays, objects, scalars)
|
||||||
*
|
*
|
||||||
* Note: unfortunately when storing array as key - access and modification is O(N)
|
* Note: unfortunately when storing array as key - access and modification is O(N)
|
||||||
* (yet this should be really rare case and should be avoided when possible)
|
* (yet this should rarely be the case and should be avoided when possible)
|
||||||
*/
|
*/
|
||||||
class MixedStore implements ArrayAccess
|
class MixedStore implements ArrayAccess
|
||||||
{
|
{
|
||||||
|
@ -401,7 +401,7 @@ class DeferredFieldsTest extends TestCase
|
|||||||
return [
|
return [
|
||||||
'sync' => [
|
'sync' => [
|
||||||
'type' => Type::string(),
|
'type' => Type::string(),
|
||||||
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
'resolve' => function ($val, $args, $context, ResolveInfo $info) {
|
||||||
$this->paths[] = $info->path;
|
$this->paths[] = $info->path;
|
||||||
|
|
||||||
return 'sync';
|
return 'sync';
|
||||||
@ -409,7 +409,7 @@ class DeferredFieldsTest extends TestCase
|
|||||||
],
|
],
|
||||||
'deferred' => [
|
'deferred' => [
|
||||||
'type' => Type::string(),
|
'type' => Type::string(),
|
||||||
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
'resolve' => function ($val, $args, $context, ResolveInfo $info) {
|
||||||
$this->paths[] = $info->path;
|
$this->paths[] = $info->path;
|
||||||
|
|
||||||
return new Deferred(function () use ($info) {
|
return new Deferred(function () use ($info) {
|
||||||
@ -421,7 +421,7 @@ class DeferredFieldsTest extends TestCase
|
|||||||
],
|
],
|
||||||
'nest' => [
|
'nest' => [
|
||||||
'type' => $complexType,
|
'type' => $complexType,
|
||||||
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
'resolve' => function ($val, $args, $context, ResolveInfo $info) {
|
||||||
$this->paths[] = $info->path;
|
$this->paths[] = $info->path;
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
@ -429,7 +429,7 @@ class DeferredFieldsTest extends TestCase
|
|||||||
],
|
],
|
||||||
'deferredNest' => [
|
'deferredNest' => [
|
||||||
'type' => $complexType,
|
'type' => $complexType,
|
||||||
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
'resolve' => function ($val, $args, $context, ResolveInfo $info) {
|
||||||
$this->paths[] = $info->path;
|
$this->paths[] = $info->path;
|
||||||
|
|
||||||
return new Deferred(function () use ($info) {
|
return new Deferred(function () use ($info) {
|
||||||
|
Loading…
Reference in New Issue
Block a user