mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 22:36:02 +03:00
Refactored executor: now works as instance with exeContext and promise adapter as properties
This commit is contained in:
parent
f3fca81e9d
commit
77244d3aab
@ -103,40 +103,10 @@ class Executor
|
|||||||
}
|
}
|
||||||
|
|
||||||
$exeContext = self::buildExecutionContext($schema, $ast, $rootValue, $contextValue, $variableValues, $operationName);
|
$exeContext = self::buildExecutionContext($schema, $ast, $rootValue, $contextValue, $variableValues, $operationName);
|
||||||
if (null === self::$promiseAdapter) {
|
$promiseAdapter = self::$promiseAdapter ?: new GenericPromiseAdapter();
|
||||||
static::setPromiseAdapter(new GenericPromiseAdapter());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
$executor = new self($exeContext, $promiseAdapter);
|
||||||
$data = self::$promiseAdapter
|
return $executor->executeQuery();
|
||||||
->createPromise(function (callable $resolve) use ($exeContext, $rootValue) {
|
|
||||||
return $resolve(self::executeOperation($exeContext, $exeContext->operation, $rootValue));
|
|
||||||
});
|
|
||||||
|
|
||||||
if (self::$promiseAdapter->isPromise($data)) {
|
|
||||||
// Return a Promise that will eventually resolve to the data described by
|
|
||||||
// The "Response" section of the GraphQL specification.
|
|
||||||
//
|
|
||||||
// If errors are encountered while executing a GraphQL field, only that
|
|
||||||
// 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.
|
|
||||||
return $data->then(null, function ($error) use ($exeContext) {
|
|
||||||
// Errors from sub-fields of a NonNull type may propagate to the top level,
|
|
||||||
// at which point we still log the error and null the parent field, which
|
|
||||||
// in this case is the entire response.
|
|
||||||
$exeContext->addError($error);
|
|
||||||
return null;
|
|
||||||
})->then(function ($data) use ($exeContext) {
|
|
||||||
return new ExecutionResult((array) $data, $exeContext->errors);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (Error $e) {
|
|
||||||
$exeContext->addError($e);
|
|
||||||
$data = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ExecutionResult((array) $data, $exeContext->errors);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -207,25 +177,80 @@ class Executor
|
|||||||
return $exeContext;
|
return $exeContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ExecutionContext
|
||||||
|
*/
|
||||||
|
private $exeContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var PromiseAdapter
|
||||||
|
*/
|
||||||
|
private $promises;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executor constructor.
|
||||||
|
*
|
||||||
|
* @param ExecutionContext $context
|
||||||
|
* @param PromiseAdapter $promiseAdapter
|
||||||
|
*/
|
||||||
|
private function __construct(ExecutionContext $context, PromiseAdapter $promiseAdapter)
|
||||||
|
{
|
||||||
|
$this->exeContext = $context;
|
||||||
|
$this->promises = $promiseAdapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function executeQuery()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$data = $this->promises->createPromise(function (callable $resolve) {
|
||||||
|
return $resolve($this->executeOperation($this->exeContext->operation, $this->exeContext->rootValue));
|
||||||
|
});
|
||||||
|
|
||||||
|
if ($this->promises->isPromise($data)) {
|
||||||
|
// Return a Promise that will eventually resolve to the data described by
|
||||||
|
// The "Response" section of the GraphQL specification.
|
||||||
|
//
|
||||||
|
// If errors are encountered while executing a GraphQL field, only that
|
||||||
|
// 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.
|
||||||
|
return $data
|
||||||
|
->then(null, function ($error) {
|
||||||
|
// Errors from sub-fields of a NonNull type may propagate to the top level,
|
||||||
|
// at which point we still log the error and null the parent field, which
|
||||||
|
// in this case is the entire response.
|
||||||
|
$this->exeContext->addError($error);
|
||||||
|
return null;
|
||||||
|
})->then(function ($data) {
|
||||||
|
return new ExecutionResult((array) $data, $this->exeContext->errors);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (Error $e) {
|
||||||
|
$this->exeContext->addError($e);
|
||||||
|
$data = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ExecutionResult((array) $data, $this->exeContext->errors);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the "Evaluating operations" section of the spec.
|
* Implements the "Evaluating operations" section of the spec.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param OperationDefinitionNode $operation
|
* @param OperationDefinitionNode $operation
|
||||||
* @param $rootValue
|
* @param $rootValue
|
||||||
* @return Promise|\stdClass|array
|
* @return Promise|\stdClass|array
|
||||||
*/
|
*/
|
||||||
private static function executeOperation(ExecutionContext $exeContext, OperationDefinitionNode $operation, $rootValue)
|
private function executeOperation(OperationDefinitionNode $operation, $rootValue)
|
||||||
{
|
{
|
||||||
$type = self::getOperationRootType($exeContext->schema, $operation);
|
$type = $this->getOperationRootType($this->exeContext->schema, $operation);
|
||||||
$fields = self::collectFields($exeContext, $type, $operation->selectionSet, new \ArrayObject(), new \ArrayObject());
|
$fields = $this->collectFields($type, $operation->selectionSet, new \ArrayObject(), new \ArrayObject());
|
||||||
|
|
||||||
$path = [];
|
$path = [];
|
||||||
if ($operation->operation === 'mutation') {
|
if ($operation->operation === 'mutation') {
|
||||||
return self::executeFieldsSerially($exeContext, $type, $rootValue, $path, $fields);
|
return $this->executeFieldsSerially($type, $rootValue, $path, $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::executeFields($exeContext, $type, $rootValue, $path, $fields);
|
return $this->executeFields($type, $rootValue, $path, $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -237,7 +262,7 @@ class Executor
|
|||||||
* @return ObjectType
|
* @return ObjectType
|
||||||
* @throws Error
|
* @throws Error
|
||||||
*/
|
*/
|
||||||
private static function getOperationRootType(Schema $schema, OperationDefinitionNode $operation)
|
private function getOperationRootType(Schema $schema, OperationDefinitionNode $operation)
|
||||||
{
|
{
|
||||||
switch ($operation->operation) {
|
switch ($operation->operation) {
|
||||||
case 'query':
|
case 'query':
|
||||||
@ -272,25 +297,24 @@ class Executor
|
|||||||
* Implements the "Evaluating selection sets" section of the spec
|
* Implements the "Evaluating selection sets" section of the spec
|
||||||
* for "write" mode.
|
* for "write" mode.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param ObjectType $parentType
|
* @param ObjectType $parentType
|
||||||
* @param $sourceValue
|
* @param $sourceValue
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param $fields
|
* @param $fields
|
||||||
* @return Promise|\stdClass|array
|
* @return Promise|\stdClass|array
|
||||||
*/
|
*/
|
||||||
private static function executeFieldsSerially(ExecutionContext $exeContext, ObjectType $parentType, $sourceValue, $path, $fields)
|
private function executeFieldsSerially(ObjectType $parentType, $sourceValue, $path, $fields)
|
||||||
{
|
{
|
||||||
$results = self::$promiseAdapter->createResolvedPromise([]);
|
$results = $this->promises->createResolvedPromise([]);
|
||||||
|
|
||||||
$process = function ($results, $responseName, $path, $exeContext, $parentType, $sourceValue, $fieldNodes) {
|
$process = function ($results, $responseName, $path, $parentType, $sourceValue, $fieldNodes) {
|
||||||
$fieldPath = $path;
|
$fieldPath = $path;
|
||||||
$fieldPath[] = $responseName;
|
$fieldPath[] = $responseName;
|
||||||
$result = self::resolveField($exeContext, $parentType, $sourceValue, $fieldNodes, $fieldPath);
|
$result = $this->resolveField($parentType, $sourceValue, $fieldNodes, $fieldPath);
|
||||||
if ($result === self::$UNDEFINED) {
|
if ($result === self::$UNDEFINED) {
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
if (self::$promiseAdapter->isPromise($result)) {
|
if ($this->promises->isPromise($result)) {
|
||||||
return $result->then(function ($resolvedResult) use ($responseName, $results) {
|
return $result->then(function ($resolvedResult) use ($responseName, $results) {
|
||||||
$results[$responseName] = $resolvedResult;
|
$results[$responseName] = $resolvedResult;
|
||||||
return $results;
|
return $results;
|
||||||
@ -301,16 +325,16 @@ class Executor
|
|||||||
};
|
};
|
||||||
|
|
||||||
foreach ($fields as $responseName => $fieldNodes) {
|
foreach ($fields as $responseName => $fieldNodes) {
|
||||||
if (self::$promiseAdapter->isPromise($results)) {
|
if ($this->promises->isPromise($results)) {
|
||||||
$results = $results->then(function ($resolvedResults) use ($process, $responseName, $path, $exeContext, $parentType, $sourceValue, $fieldNodes) {
|
$results = $results->then(function ($resolvedResults) use ($process, $responseName, $path, $parentType, $sourceValue, $fieldNodes) {
|
||||||
return $process($resolvedResults, $responseName, $path, $exeContext, $parentType, $sourceValue, $fieldNodes);
|
return $process($resolvedResults, $responseName, $path, $parentType, $sourceValue, $fieldNodes);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
$results = $process($results, $responseName, $path, $exeContext, $parentType, $sourceValue, $fieldNodes);
|
$results = $process($results, $responseName, $path, $parentType, $sourceValue, $fieldNodes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::$promiseAdapter->isPromise($results)) {
|
if ($this->promises->isPromise($results)) {
|
||||||
return $results->then(function ($resolvedResults) {
|
return $results->then(function ($resolvedResults) {
|
||||||
return self::fixResultsIfEmptyArray($resolvedResults);
|
return self::fixResultsIfEmptyArray($resolvedResults);
|
||||||
});
|
});
|
||||||
@ -323,14 +347,13 @@ class Executor
|
|||||||
* Implements the "Evaluating selection sets" section of the spec
|
* Implements the "Evaluating selection sets" section of the spec
|
||||||
* for "read" mode.
|
* for "read" mode.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param ObjectType $parentType
|
* @param ObjectType $parentType
|
||||||
* @param $source
|
* @param $source
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param $fields
|
* @param $fields
|
||||||
* @return Promise|\stdClass|array
|
* @return Promise|\stdClass|array
|
||||||
*/
|
*/
|
||||||
private static function executeFields(ExecutionContext $exeContext, ObjectType $parentType, $source, $path, $fields)
|
private function executeFields(ObjectType $parentType, $source, $path, $fields)
|
||||||
{
|
{
|
||||||
$containsPromise = false;
|
$containsPromise = false;
|
||||||
$finalResults = [];
|
$finalResults = [];
|
||||||
@ -338,11 +361,11 @@ class Executor
|
|||||||
foreach ($fields as $responseName => $fieldNodes) {
|
foreach ($fields as $responseName => $fieldNodes) {
|
||||||
$fieldPath = $path;
|
$fieldPath = $path;
|
||||||
$fieldPath[] = $responseName;
|
$fieldPath[] = $responseName;
|
||||||
$result = self::resolveField($exeContext, $parentType, $source, $fieldNodes, $fieldPath);
|
$result = $this->resolveField($parentType, $source, $fieldNodes, $fieldPath);
|
||||||
if ($result === self::$UNDEFINED) {
|
if ($result === self::$UNDEFINED) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!$containsPromise && self::$promiseAdapter->isPromise($result)) {
|
if (!$containsPromise && $this->promises->isPromise($result)) {
|
||||||
$containsPromise = true;
|
$containsPromise = true;
|
||||||
}
|
}
|
||||||
$finalResults[$responseName] = $result;
|
$finalResults[$responseName] = $result;
|
||||||
@ -357,7 +380,7 @@ class Executor
|
|||||||
// of resolving that field, which is possibly a promise. Return
|
// of resolving that field, which is possibly a promise. Return
|
||||||
// a promise that will return this same map, but with any
|
// a promise that will return this same map, but with any
|
||||||
// promises replaced with the values they resolved to.
|
// promises replaced with the values they resolved to.
|
||||||
return self::$promiseAdapter->createPromiseAll($finalResults)->then(function ($resolvedResults) {
|
return $this->promises->createPromiseAll($finalResults)->then(function ($resolvedResults) {
|
||||||
return self::fixResultsIfEmptyArray($resolvedResults);
|
return self::fixResultsIfEmptyArray($resolvedResults);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -385,7 +408,6 @@ class Executor
|
|||||||
* returns and Interface or Union type, the "runtime type" will be the actual
|
* returns and Interface or Union type, the "runtime type" will be the actual
|
||||||
* Object type returned by that field.
|
* Object type returned by that field.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param ObjectType $runtimeType
|
* @param ObjectType $runtimeType
|
||||||
* @param SelectionSetNode $selectionSet
|
* @param SelectionSetNode $selectionSet
|
||||||
* @param $fields
|
* @param $fields
|
||||||
@ -393,18 +415,18 @@ class Executor
|
|||||||
*
|
*
|
||||||
* @return \ArrayObject
|
* @return \ArrayObject
|
||||||
*/
|
*/
|
||||||
private static function collectFields(
|
private function collectFields(
|
||||||
ExecutionContext $exeContext,
|
|
||||||
ObjectType $runtimeType,
|
ObjectType $runtimeType,
|
||||||
SelectionSetNode $selectionSet,
|
SelectionSetNode $selectionSet,
|
||||||
$fields,
|
$fields,
|
||||||
$visitedFragmentNames
|
$visitedFragmentNames
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
$exeContext = $this->exeContext;
|
||||||
foreach ($selectionSet->selections as $selection) {
|
foreach ($selectionSet->selections as $selection) {
|
||||||
switch ($selection->kind) {
|
switch ($selection->kind) {
|
||||||
case NodeKind::FIELD:
|
case NodeKind::FIELD:
|
||||||
if (!self::shouldIncludeNode($exeContext, $selection->directives)) {
|
if (!$this->shouldIncludeNode($selection->directives)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$name = self::getFieldEntryKey($selection);
|
$name = self::getFieldEntryKey($selection);
|
||||||
@ -414,13 +436,12 @@ class Executor
|
|||||||
$fields[$name][] = $selection;
|
$fields[$name][] = $selection;
|
||||||
break;
|
break;
|
||||||
case NodeKind::INLINE_FRAGMENT:
|
case NodeKind::INLINE_FRAGMENT:
|
||||||
if (!self::shouldIncludeNode($exeContext, $selection->directives) ||
|
if (!$this->shouldIncludeNode($selection->directives) ||
|
||||||
!self::doesFragmentConditionMatch($exeContext, $selection, $runtimeType)
|
!$this->doesFragmentConditionMatch($selection, $runtimeType)
|
||||||
) {
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
self::collectFields(
|
$this->collectFields(
|
||||||
$exeContext,
|
|
||||||
$runtimeType,
|
$runtimeType,
|
||||||
$selection->selectionSet,
|
$selection->selectionSet,
|
||||||
$fields,
|
$fields,
|
||||||
@ -429,18 +450,17 @@ class Executor
|
|||||||
break;
|
break;
|
||||||
case NodeKind::FRAGMENT_SPREAD:
|
case NodeKind::FRAGMENT_SPREAD:
|
||||||
$fragName = $selection->name->value;
|
$fragName = $selection->name->value;
|
||||||
if (!empty($visitedFragmentNames[$fragName]) || !self::shouldIncludeNode($exeContext, $selection->directives)) {
|
if (!empty($visitedFragmentNames[$fragName]) || !$this->shouldIncludeNode($selection->directives)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$visitedFragmentNames[$fragName] = true;
|
$visitedFragmentNames[$fragName] = true;
|
||||||
|
|
||||||
/** @var FragmentDefinitionNode|null $fragment */
|
/** @var FragmentDefinitionNode|null $fragment */
|
||||||
$fragment = isset($exeContext->fragments[$fragName]) ? $exeContext->fragments[$fragName] : null;
|
$fragment = isset($exeContext->fragments[$fragName]) ? $exeContext->fragments[$fragName] : null;
|
||||||
if (!$fragment || !self::doesFragmentConditionMatch($exeContext, $fragment, $runtimeType)) {
|
if (!$fragment || !$this->doesFragmentConditionMatch($fragment, $runtimeType)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
self::collectFields(
|
$this->collectFields(
|
||||||
$exeContext,
|
|
||||||
$runtimeType,
|
$runtimeType,
|
||||||
$fragment->selectionSet,
|
$fragment->selectionSet,
|
||||||
$fields,
|
$fields,
|
||||||
@ -456,12 +476,12 @@ class Executor
|
|||||||
* Determines if a field should be included based on the @include and @skip
|
* Determines if a field should be included based on the @include and @skip
|
||||||
* directives, where @skip has higher precedence than @include.
|
* directives, where @skip has higher precedence than @include.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param $directives
|
* @param $directives
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private static function shouldIncludeNode(ExecutionContext $exeContext, $directives)
|
private function shouldIncludeNode($directives)
|
||||||
{
|
{
|
||||||
|
$exeContext = $this->exeContext;
|
||||||
$skipDirective = Directive::skipDirective();
|
$skipDirective = Directive::skipDirective();
|
||||||
$includeDirective = Directive::includeDirective();
|
$includeDirective = Directive::includeDirective();
|
||||||
|
|
||||||
@ -499,12 +519,11 @@ class Executor
|
|||||||
/**
|
/**
|
||||||
* Determines if a fragment is applicable to the given type.
|
* Determines if a fragment is applicable to the given type.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param $fragment
|
* @param $fragment
|
||||||
* @param ObjectType $type
|
* @param ObjectType $type
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private static function doesFragmentConditionMatch(ExecutionContext $exeContext,/* FragmentDefinitionNode | InlineFragmentNode*/ $fragment, ObjectType $type)
|
private function doesFragmentConditionMatch(/* FragmentDefinitionNode | InlineFragmentNode*/ $fragment, ObjectType $type)
|
||||||
{
|
{
|
||||||
$typeConditionNode = $fragment->typeCondition;
|
$typeConditionNode = $fragment->typeCondition;
|
||||||
|
|
||||||
@ -512,12 +531,12 @@ class Executor
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$conditionalType = Utils\TypeInfo::typeFromAST($exeContext->schema, $typeConditionNode);
|
$conditionalType = Utils\TypeInfo::typeFromAST($this->exeContext->schema, $typeConditionNode);
|
||||||
if ($conditionalType === $type) {
|
if ($conditionalType === $type) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ($conditionalType instanceof AbstractType) {
|
if ($conditionalType instanceof AbstractType) {
|
||||||
return $exeContext->schema->isPossibleType($conditionalType, $type);
|
return $this->exeContext->schema->isPossibleType($conditionalType, $type);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -539,7 +558,6 @@ class Executor
|
|||||||
* then calls completeValue to complete promises, serialize scalars, or execute
|
* then calls completeValue to complete promises, serialize scalars, or execute
|
||||||
* the sub-selection-set for objects.
|
* the sub-selection-set for objects.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param ObjectType $parentType
|
* @param ObjectType $parentType
|
||||||
* @param $source
|
* @param $source
|
||||||
* @param $fieldNodes
|
* @param $fieldNodes
|
||||||
@ -547,13 +565,13 @@ class Executor
|
|||||||
*
|
*
|
||||||
* @return array|\Exception|mixed|null
|
* @return array|\Exception|mixed|null
|
||||||
*/
|
*/
|
||||||
private static function resolveField(ExecutionContext $exeContext, ObjectType $parentType, $source, $fieldNodes, $path)
|
private function resolveField(ObjectType $parentType, $source, $fieldNodes, $path)
|
||||||
{
|
{
|
||||||
|
$exeContext = $this->exeContext;
|
||||||
$fieldNode = $fieldNodes[0];
|
$fieldNode = $fieldNodes[0];
|
||||||
|
|
||||||
$fieldName = $fieldNode->name->value;
|
$fieldName = $fieldNode->name->value;
|
||||||
|
$fieldDef = $this->getFieldDef($exeContext->schema, $parentType, $fieldName);
|
||||||
$fieldDef = self::getFieldDef($exeContext->schema, $parentType, $fieldName);
|
|
||||||
|
|
||||||
if (!$fieldDef) {
|
if (!$fieldDef) {
|
||||||
return self::$UNDEFINED;
|
return self::$UNDEFINED;
|
||||||
@ -592,8 +610,7 @@ class Executor
|
|||||||
|
|
||||||
// Get the resolve function, regardless of if its result is normal
|
// Get the resolve function, regardless of if its result is normal
|
||||||
// or abrupt (error).
|
// or abrupt (error).
|
||||||
$result = self::resolveOrError(
|
$result = $this->resolveOrError(
|
||||||
$exeContext,
|
|
||||||
$fieldDef,
|
$fieldDef,
|
||||||
$fieldNode,
|
$fieldNode,
|
||||||
$resolveFn,
|
$resolveFn,
|
||||||
@ -602,8 +619,7 @@ class Executor
|
|||||||
$info
|
$info
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = self::completeValueCatchingError(
|
$result = $this->completeValueCatchingError(
|
||||||
$exeContext,
|
|
||||||
$returnType,
|
$returnType,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
$info,
|
$info,
|
||||||
@ -618,7 +634,6 @@ class Executor
|
|||||||
* Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
|
* Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
|
||||||
* function. Returns the result of resolveFn or the abrupt-return Error object.
|
* function. Returns the result of resolveFn or the abrupt-return Error object.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param FieldDefinition $fieldDef
|
* @param FieldDefinition $fieldDef
|
||||||
* @param FieldNode $fieldNode
|
* @param FieldNode $fieldNode
|
||||||
* @param callable $resolveFn
|
* @param callable $resolveFn
|
||||||
@ -627,7 +642,7 @@ class Executor
|
|||||||
* @param ResolveInfo $info
|
* @param ResolveInfo $info
|
||||||
* @return \Exception|mixed
|
* @return \Exception|mixed
|
||||||
*/
|
*/
|
||||||
private static function resolveOrError($exeContext, $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 hash of arguments from the field.arguments AST, using the
|
||||||
@ -635,7 +650,7 @@ class Executor
|
|||||||
$args = Values::getArgumentValues(
|
$args = Values::getArgumentValues(
|
||||||
$fieldDef,
|
$fieldDef,
|
||||||
$fieldNode,
|
$fieldNode,
|
||||||
$exeContext->variableValues
|
$this->exeContext->variableValues
|
||||||
);
|
);
|
||||||
|
|
||||||
return call_user_func($resolveFn, $source, $args, $context, $info);
|
return call_user_func($resolveFn, $source, $args, $context, $info);
|
||||||
@ -648,7 +663,6 @@ class Executor
|
|||||||
* This is a small wrapper around completeValue which detects and logs errors
|
* This is a small wrapper around completeValue which detects and logs errors
|
||||||
* in the execution context.
|
* in the execution context.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param Type $returnType
|
* @param Type $returnType
|
||||||
* @param $fieldNodes
|
* @param $fieldNodes
|
||||||
* @param ResolveInfo $info
|
* @param ResolveInfo $info
|
||||||
@ -656,8 +670,7 @@ class Executor
|
|||||||
* @param $result
|
* @param $result
|
||||||
* @return array|null|Promise
|
* @return array|null|Promise
|
||||||
*/
|
*/
|
||||||
private static function completeValueCatchingError(
|
private function completeValueCatchingError(
|
||||||
ExecutionContext $exeContext,
|
|
||||||
Type $returnType,
|
Type $returnType,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
ResolveInfo $info,
|
ResolveInfo $info,
|
||||||
@ -665,11 +678,12 @@ class Executor
|
|||||||
$result
|
$result
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
$exeContext = $this->exeContext;
|
||||||
|
|
||||||
// If the field type is non-nullable, then it is resolved without any
|
// If the field type is non-nullable, then it is resolved without any
|
||||||
// protection from errors.
|
// protection from errors.
|
||||||
if ($returnType instanceof NonNull) {
|
if ($returnType instanceof NonNull) {
|
||||||
return self::completeValueWithLocatedError(
|
return $this->completeValueWithLocatedError(
|
||||||
$exeContext,
|
|
||||||
$returnType,
|
$returnType,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
$info,
|
$info,
|
||||||
@ -681,18 +695,17 @@ class Executor
|
|||||||
// Otherwise, error protection is applied, logging the error and resolving
|
// Otherwise, error protection is applied, logging the error and resolving
|
||||||
// a null value for this field if one is encountered.
|
// a null value for this field if one is encountered.
|
||||||
try {
|
try {
|
||||||
$completed = self::completeValueWithLocatedError(
|
$completed = $this->completeValueWithLocatedError(
|
||||||
$exeContext,
|
|
||||||
$returnType,
|
$returnType,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
$info,
|
$info,
|
||||||
$path,
|
$path,
|
||||||
$result
|
$result
|
||||||
);
|
);
|
||||||
if (self::$promiseAdapter->isPromise($completed)) {
|
if ($this->promises->isPromise($completed)) {
|
||||||
return $completed->then(null, function ($error) use ($exeContext) {
|
return $completed->then(null, function ($error) use ($exeContext) {
|
||||||
$exeContext->addError($error);
|
$exeContext->addError($error);
|
||||||
return self::$promiseAdapter->createResolvedPromise(null);
|
return $this->promises->createResolvedPromise(null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return $completed;
|
return $completed;
|
||||||
@ -709,7 +722,6 @@ class Executor
|
|||||||
* This is a small wrapper around completeValue which annotates errors with
|
* This is a small wrapper around completeValue which annotates errors with
|
||||||
* location information.
|
* location information.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param Type $returnType
|
* @param Type $returnType
|
||||||
* @param $fieldNodes
|
* @param $fieldNodes
|
||||||
* @param ResolveInfo $info
|
* @param ResolveInfo $info
|
||||||
@ -718,8 +730,7 @@ class Executor
|
|||||||
* @return array|null|Promise
|
* @return array|null|Promise
|
||||||
* @throws Error
|
* @throws Error
|
||||||
*/
|
*/
|
||||||
public static function completeValueWithLocatedError(
|
public function completeValueWithLocatedError(
|
||||||
ExecutionContext $exeContext,
|
|
||||||
Type $returnType,
|
Type $returnType,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
ResolveInfo $info,
|
ResolveInfo $info,
|
||||||
@ -728,17 +739,16 @@ class Executor
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$completed = self::completeValue(
|
$completed = $this->completeValue(
|
||||||
$exeContext,
|
|
||||||
$returnType,
|
$returnType,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
$info,
|
$info,
|
||||||
$path,
|
$path,
|
||||||
$result
|
$result
|
||||||
);
|
);
|
||||||
if (self::$promiseAdapter->isPromise($completed)) {
|
if ($this->promises->isPromise($completed)) {
|
||||||
return $completed->then(null, function ($error) use ($fieldNodes, $path) {
|
return $completed->then(null, function ($error) use ($fieldNodes, $path) {
|
||||||
return self::$promiseAdapter->createRejectedPromise(Error::createLocatedError($error, $fieldNodes, $path));
|
return $this->promises->createRejectedPromise(Error::createLocatedError($error, $fieldNodes, $path));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return $completed;
|
return $completed;
|
||||||
@ -768,7 +778,6 @@ class Executor
|
|||||||
* Otherwise, the field type expects a sub-selection set, and will complete the
|
* Otherwise, the field type expects a sub-selection set, and will complete the
|
||||||
* value by evaluating all sub-selections.
|
* value by evaluating all sub-selections.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param Type $returnType
|
* @param Type $returnType
|
||||||
* @param FieldNode[] $fieldNodes
|
* @param FieldNode[] $fieldNodes
|
||||||
* @param ResolveInfo $info
|
* @param ResolveInfo $info
|
||||||
@ -778,8 +787,7 @@ class Executor
|
|||||||
* @throws Error
|
* @throws Error
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
private static function completeValue(
|
private function completeValue(
|
||||||
ExecutionContext $exeContext,
|
|
||||||
Type $returnType,
|
Type $returnType,
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
ResolveInfo $info,
|
ResolveInfo $info,
|
||||||
@ -787,10 +795,12 @@ class Executor
|
|||||||
&$result
|
&$result
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
$exeContext = $this->exeContext;
|
||||||
|
|
||||||
// If result is a Promise, apply-lift over completeValue.
|
// If result is a Promise, apply-lift over completeValue.
|
||||||
if (self::$promiseAdapter->isPromise($result)) {
|
if ($this->promises->isPromise($result)) {
|
||||||
return $result->then(function (&$resolved) use ($exeContext, $returnType, $fieldNodes, $info, $path) {
|
return $result->then(function (&$resolved) use ($returnType, $fieldNodes, $info, $path) {
|
||||||
return self::completeValue($exeContext, $returnType, $fieldNodes, $info, $path, $resolved);
|
return $this->completeValue($returnType, $fieldNodes, $info, $path, $resolved);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -801,8 +811,7 @@ class Executor
|
|||||||
// If field type is NonNull, complete for inner type, and throw field error
|
// If field type is NonNull, complete for inner type, and throw field error
|
||||||
// if result is null.
|
// if result is null.
|
||||||
if ($returnType instanceof NonNull) {
|
if ($returnType instanceof NonNull) {
|
||||||
$completed = self::completeValue(
|
$completed = $this->completeValue(
|
||||||
$exeContext,
|
|
||||||
$returnType->getWrappedType(),
|
$returnType->getWrappedType(),
|
||||||
$fieldNodes,
|
$fieldNodes,
|
||||||
$info,
|
$info,
|
||||||
@ -824,22 +833,22 @@ class Executor
|
|||||||
|
|
||||||
// If field type is List, complete each item in the list with the inner type
|
// If field type is List, complete each item in the list with the inner type
|
||||||
if ($returnType instanceof ListOfType) {
|
if ($returnType instanceof ListOfType) {
|
||||||
return self::completeListValue($exeContext, $returnType, $fieldNodes, $info, $path, $result);
|
return $this->completeListValue($returnType, $fieldNodes, $info, $path, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If field type is Scalar or Enum, serialize to a valid value, returning
|
// If field type is Scalar or Enum, serialize to a valid value, returning
|
||||||
// null if serialization is not possible.
|
// null if serialization is not possible.
|
||||||
if ($returnType instanceof LeafType) {
|
if ($returnType instanceof LeafType) {
|
||||||
return self::completeLeafValue($returnType, $result);
|
return $this->completeLeafValue($returnType, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($returnType instanceof AbstractType) {
|
if ($returnType instanceof AbstractType) {
|
||||||
return self::completeAbstractValue($exeContext, $returnType, $fieldNodes, $info, $path, $result);
|
return $this->completeAbstractValue($returnType, $fieldNodes, $info, $path, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Field type must be Object, Interface or Union and expect sub-selections.
|
// Field type must be Object, Interface or Union and expect sub-selections.
|
||||||
if ($returnType instanceof ObjectType) {
|
if ($returnType instanceof ObjectType) {
|
||||||
return self::completeObjectValue($exeContext, $returnType, $fieldNodes, $info, $path, $result);
|
return $this->completeObjectValue($returnType, $fieldNodes, $info, $path, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \RuntimeException("Cannot complete value of unexpected type \"{$returnType}\".");
|
throw new \RuntimeException("Cannot complete value of unexpected type \"{$returnType}\".");
|
||||||
@ -891,7 +900,7 @@ class Executor
|
|||||||
*
|
*
|
||||||
* @return FieldDefinition
|
* @return FieldDefinition
|
||||||
*/
|
*/
|
||||||
private static function getFieldDef(Schema $schema, ObjectType $parentType, $fieldName)
|
private function getFieldDef(Schema $schema, ObjectType $parentType, $fieldName)
|
||||||
{
|
{
|
||||||
static $schemaMetaFieldDef, $typeMetaFieldDef, $typeNameMetaFieldDef;
|
static $schemaMetaFieldDef, $typeMetaFieldDef, $typeNameMetaFieldDef;
|
||||||
|
|
||||||
@ -915,7 +924,6 @@ class Executor
|
|||||||
* Complete a value of an abstract type by determining the runtime object type
|
* Complete a value of an abstract type by determining the runtime object type
|
||||||
* of that value, then complete the value for that type.
|
* of that value, then complete the value for that type.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param AbstractType $returnType
|
* @param AbstractType $returnType
|
||||||
* @param $fieldNodes
|
* @param $fieldNodes
|
||||||
* @param ResolveInfo $info
|
* @param ResolveInfo $info
|
||||||
@ -924,8 +932,9 @@ class Executor
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Error
|
* @throws Error
|
||||||
*/
|
*/
|
||||||
private static function completeAbstractValue(ExecutionContext $exeContext, AbstractType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
private function completeAbstractValue(AbstractType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
||||||
{
|
{
|
||||||
|
$exeContext = $this->exeContext;
|
||||||
$runtimeType = $returnType->resolveType($result, $exeContext->contextValue, $info);
|
$runtimeType = $returnType->resolveType($result, $exeContext->contextValue, $info);
|
||||||
|
|
||||||
if (null === $runtimeType) {
|
if (null === $runtimeType) {
|
||||||
@ -952,14 +961,13 @@ class Executor
|
|||||||
$fieldNodes
|
$fieldNodes
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return self::completeObjectValue($exeContext, $runtimeType, $fieldNodes, $info, $path, $result);
|
return $this->completeObjectValue($runtimeType, $fieldNodes, $info, $path, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 ExecutionContext $exeContext
|
|
||||||
* @param ListOfType $returnType
|
* @param ListOfType $returnType
|
||||||
* @param $fieldNodes
|
* @param $fieldNodes
|
||||||
* @param ResolveInfo $info
|
* @param ResolveInfo $info
|
||||||
@ -968,7 +976,7 @@ class Executor
|
|||||||
* @return array|Promise
|
* @return array|Promise
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
private static function completeListValue(ExecutionContext $exeContext, ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
private function completeListValue(ListOfType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
||||||
{
|
{
|
||||||
$itemType = $returnType->getWrappedType();
|
$itemType = $returnType->getWrappedType();
|
||||||
Utils::invariant(
|
Utils::invariant(
|
||||||
@ -982,13 +990,13 @@ class Executor
|
|||||||
foreach ($result as $item) {
|
foreach ($result as $item) {
|
||||||
$fieldPath = $path;
|
$fieldPath = $path;
|
||||||
$fieldPath[] = $i++;
|
$fieldPath[] = $i++;
|
||||||
$completedItem = self::completeValueCatchingError($exeContext, $itemType, $fieldNodes, $info, $fieldPath, $item);
|
$completedItem = $this->completeValueCatchingError($itemType, $fieldNodes, $info, $fieldPath, $item);
|
||||||
if (!$containsPromise && self::$promiseAdapter->isPromise($completedItem)) {
|
if (!$containsPromise && $this->promises->isPromise($completedItem)) {
|
||||||
$containsPromise = true;
|
$containsPromise = true;
|
||||||
}
|
}
|
||||||
$completedItems[] = $completedItem;
|
$completedItems[] = $completedItem;
|
||||||
}
|
}
|
||||||
return $containsPromise ? self::$promiseAdapter->createPromiseAll($completedItems) : $completedItems;
|
return $containsPromise ? $this->promises->createPromiseAll($completedItems) : $completedItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1000,7 +1008,7 @@ class Executor
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
private static function completeLeafValue(LeafType $returnType, &$result)
|
private function completeLeafValue(LeafType $returnType, &$result)
|
||||||
{
|
{
|
||||||
$serializedResult = $returnType->serialize($result);
|
$serializedResult = $returnType->serialize($result);
|
||||||
|
|
||||||
@ -1015,7 +1023,6 @@ class Executor
|
|||||||
/**
|
/**
|
||||||
* Complete an Object value by executing all sub-selections.
|
* Complete an Object value by executing all sub-selections.
|
||||||
*
|
*
|
||||||
* @param ExecutionContext $exeContext
|
|
||||||
* @param ObjectType $returnType
|
* @param ObjectType $returnType
|
||||||
* @param $fieldNodes
|
* @param $fieldNodes
|
||||||
* @param ResolveInfo $info
|
* @param ResolveInfo $info
|
||||||
@ -1024,8 +1031,10 @@ class Executor
|
|||||||
* @return array|Promise|\stdClass
|
* @return array|Promise|\stdClass
|
||||||
* @throws Error
|
* @throws Error
|
||||||
*/
|
*/
|
||||||
private static function completeObjectValue(ExecutionContext $exeContext, ObjectType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
private function completeObjectValue(ObjectType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
|
||||||
{
|
{
|
||||||
|
$exeContext = $this->exeContext;
|
||||||
|
|
||||||
// If there is an isTypeOf predicate function, call it with the
|
// If there is an isTypeOf predicate function, call it with the
|
||||||
// current result. If isTypeOf returns false, then raise an error rather
|
// current result. If isTypeOf returns false, then raise an error rather
|
||||||
// than continuing execution.
|
// than continuing execution.
|
||||||
@ -1042,8 +1051,7 @@ class Executor
|
|||||||
|
|
||||||
foreach ($fieldNodes as $fieldNode) {
|
foreach ($fieldNodes as $fieldNode) {
|
||||||
if (isset($fieldNode->selectionSet)) {
|
if (isset($fieldNode->selectionSet)) {
|
||||||
$subFieldNodes = self::collectFields(
|
$subFieldNodes = $this->collectFields(
|
||||||
$exeContext,
|
|
||||||
$returnType,
|
$returnType,
|
||||||
$fieldNode->selectionSet,
|
$fieldNode->selectionSet,
|
||||||
$subFieldNodes,
|
$subFieldNodes,
|
||||||
@ -1052,7 +1060,7 @@ class Executor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::executeFields($exeContext, $returnType, $result, $path, $subFieldNodes);
|
return $this->executeFields($returnType, $result, $path, $subFieldNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user