| Total Complexity | 102 |
| Total Lines | 980 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Executor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Executor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Executor |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var ExecutionContext |
||
| 38 | */ |
||
| 39 | protected $context; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var OperationDefinitionNode |
||
| 43 | */ |
||
| 44 | protected $operation; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var mixed |
||
| 48 | */ |
||
| 49 | protected $rootValue; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var FieldCollector |
||
| 53 | */ |
||
| 54 | protected $fieldCollector; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $finalResult; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ErrorHandlerInterface|null |
||
| 63 | */ |
||
| 64 | protected $errorHandler; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private static $defaultFieldResolver = [__CLASS__, 'defaultFieldResolver']; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Executor constructor. |
||
| 73 | * |
||
| 74 | * @param ExecutionContext $context |
||
| 75 | * @param FieldCollector $fieldCollector |
||
| 76 | * @param ErrorHandlerInterface|null $errorHandler |
||
| 77 | */ |
||
| 78 | public function __construct( |
||
| 79 | ExecutionContext $context, |
||
| 80 | FieldCollector $fieldCollector, |
||
| 81 | ?ErrorHandlerInterface $errorHandler = null |
||
| 82 | ) { |
||
| 83 | $this->context = $context; |
||
| 84 | $this->fieldCollector = $fieldCollector; |
||
| 85 | $this->errorHandler = $errorHandler; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return array|null |
||
| 90 | * @throws ExecutionException |
||
| 91 | * @throws \Throwable |
||
| 92 | */ |
||
| 93 | public function execute(): ?array |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Schema $schema |
||
| 126 | * @param OperationDefinitionNode $operation |
||
| 127 | * |
||
| 128 | * @return ObjectType|null |
||
| 129 | * @throws ExecutionException |
||
| 130 | */ |
||
| 131 | public function getOperationType(Schema $schema, OperationDefinitionNode $operation): ?ObjectType |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Implements the "Evaluating selection sets" section of the spec for "write" mode. |
||
| 166 | * |
||
| 167 | * @param ObjectType $objectType |
||
| 168 | * @param mixed $rootValue |
||
| 169 | * @param array $path |
||
| 170 | * @param array $fields |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function executeFieldsSerially( |
||
| 175 | ObjectType $objectType, |
||
| 176 | $rootValue, |
||
| 177 | array $path, |
||
| 178 | array $fields |
||
| 179 | ): array { |
||
| 180 | $finalResults = []; |
||
| 181 | |||
| 182 | $promise = new FulfilledPromise([]); |
||
| 183 | |||
| 184 | $resolve = function ($results, $fieldName, $path, $objectType, $rootValue, $fieldNodes) { |
||
| 185 | $fieldPath = $path; |
||
| 186 | $fieldPath[] = $fieldName; |
||
| 187 | try { |
||
| 188 | $result = $this->resolveField($objectType, $rootValue, $fieldNodes, $fieldPath); |
||
| 189 | } catch (UndefinedException $ex) { |
||
| 190 | return null; |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($result instanceof ExtendedPromiseInterface) { |
||
| 194 | return $result->then(function ($resolvedResult) use ($fieldName, $results) { |
||
| 195 | $results[$fieldName] = $resolvedResult; |
||
| 196 | |||
| 197 | return $results; |
||
| 198 | }); |
||
| 199 | } |
||
| 200 | |||
| 201 | $results[$fieldName] = $result; |
||
| 202 | |||
| 203 | return $results; |
||
| 204 | }; |
||
| 205 | |||
| 206 | foreach ($fields as $fieldName => $fieldNodes) { |
||
| 207 | $promise = $promise->then(function ($resolvedResults) use ( |
||
| 208 | $resolve, |
||
| 209 | $fieldName, |
||
| 210 | $path, |
||
| 211 | $objectType, |
||
| 212 | $rootValue, |
||
| 213 | $fieldNodes |
||
| 214 | ) { |
||
| 215 | return $resolve($resolvedResults, $fieldName, $path, $objectType, $rootValue, $fieldNodes); |
||
| 216 | }); |
||
| 217 | } |
||
| 218 | |||
| 219 | $promise->then(function ($resolvedResults) use (&$finalResults) { |
||
| 220 | $finalResults = $resolvedResults ?? []; |
||
| 221 | })->otherwise(function (ExecutionException $ex) { |
||
| 222 | $this->handleError($ex); |
||
| 223 | }); |
||
| 224 | |||
| 225 | return $finalResults; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param Schema $schema |
||
| 230 | * @param ObjectType $parentType |
||
| 231 | * @param string $fieldName |
||
| 232 | * |
||
| 233 | * @return Field|null |
||
| 234 | */ |
||
| 235 | public function getFieldDefinition( |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param TypeInterface $fieldType |
||
| 264 | * @param FieldNode[] $fieldNodes |
||
| 265 | * @param ResolveInfo $info |
||
| 266 | * @param array $path |
||
| 267 | * @param mixed $result |
||
| 268 | * |
||
| 269 | * @return array|mixed|null |
||
| 270 | * @throws \Throwable |
||
| 271 | */ |
||
| 272 | public function completeValueCatchingError( |
||
| 273 | TypeInterface $fieldType, |
||
| 274 | array $fieldNodes, |
||
| 275 | ResolveInfo $info, |
||
| 276 | array $path, |
||
| 277 | &$result |
||
| 278 | ) { |
||
| 279 | if ($fieldType instanceof NonNullType) { |
||
| 280 | return $this->completeValueWithLocatedError( |
||
| 281 | $fieldType, |
||
| 282 | $fieldNodes, |
||
| 283 | $info, |
||
| 284 | $path, |
||
| 285 | $result |
||
| 286 | ); |
||
| 287 | } |
||
| 288 | |||
| 289 | try { |
||
| 290 | $completed = $this->completeValueWithLocatedError( |
||
| 291 | $fieldType, |
||
| 292 | $fieldNodes, |
||
| 293 | $info, |
||
| 294 | $path, |
||
| 295 | $result |
||
| 296 | ); |
||
| 297 | |||
| 298 | if ($completed instanceof ExtendedPromiseInterface) { |
||
| 299 | return $completed->then(null, function ($error) use ($fieldNodes, $path) { |
||
| 300 | if ($error instanceof \Exception) { |
||
| 301 | $this->handleError( |
||
| 302 | $this->buildLocatedError($error, $fieldNodes, $path) |
||
| 303 | ); |
||
| 304 | } else { |
||
| 305 | $this->handleError( |
||
| 306 | $this->buildLocatedError( |
||
| 307 | new ExecutionException($error ?? 'An unknown error occurred.'), |
||
| 308 | $fieldNodes, |
||
| 309 | $path |
||
| 310 | ) |
||
| 311 | ); |
||
| 312 | } |
||
| 313 | |||
| 314 | return new FulfilledPromise(null); |
||
| 315 | }); |
||
| 316 | } |
||
| 317 | |||
| 318 | return $completed; |
||
| 319 | } catch (\Throwable $ex) { |
||
| 320 | $this->handleError($this->buildLocatedError($ex, $fieldNodes, $path)); |
||
| 321 | return null; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param TypeInterface $fieldType |
||
| 327 | * @param FieldNode[] $fieldNodes |
||
| 328 | * @param ResolveInfo $info |
||
| 329 | * @param array $path |
||
| 330 | * @param mixed $result |
||
| 331 | * |
||
| 332 | * @return array|mixed |
||
| 333 | * @throws \Throwable |
||
| 334 | */ |
||
| 335 | public function completeValueWithLocatedError( |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Implements the "Evaluating selection sets" section of the spec for "read" mode. |
||
| 359 | * |
||
| 360 | * @param ObjectType $objectType |
||
| 361 | * @param mixed $rootValue |
||
| 362 | * @param array $path |
||
| 363 | * @param array $fields |
||
| 364 | * |
||
| 365 | * @return array |
||
| 366 | * @throws ExecutionException |
||
| 367 | * @throws \Throwable |
||
| 368 | */ |
||
| 369 | protected function executeFields( |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param ObjectType $parentType |
||
| 410 | * @param mixed $rootValue |
||
| 411 | * @param FieldNode[] $fieldNodes |
||
| 412 | * @param array $path |
||
| 413 | * |
||
| 414 | * @return array|mixed|null |
||
| 415 | * @throws UndefinedException |
||
| 416 | * @throws \Throwable |
||
| 417 | */ |
||
| 418 | protected function resolveField( |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param Field $field |
||
| 459 | * @param ObjectType $objectType |
||
| 460 | * |
||
| 461 | * @return callable|mixed|null |
||
| 462 | */ |
||
| 463 | protected function determineResolveCallback(Field $field, ObjectType $objectType) |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param TypeInterface $returnType |
||
| 478 | * @param FieldNode[] $fieldNodes |
||
| 479 | * @param ResolveInfo $info |
||
| 480 | * @param array $path |
||
| 481 | * @param mixed $result |
||
| 482 | * |
||
| 483 | * @return array|mixed |
||
| 484 | * @throws InvariantException |
||
| 485 | * @throws InvalidTypeException |
||
| 486 | * @throws ExecutionException |
||
| 487 | * @throws \Throwable |
||
| 488 | */ |
||
| 489 | protected function completeValue( |
||
| 490 | TypeInterface $returnType, |
||
| 491 | array $fieldNodes, |
||
| 492 | ResolveInfo $info, |
||
| 493 | array $path, |
||
| 494 | &$result |
||
| 495 | ) { |
||
| 496 | if ($result instanceof ExtendedPromiseInterface) { |
||
| 497 | return $result->then(function (&$value) use ($returnType, $fieldNodes, $info, $path) { |
||
| 498 | return $this->completeValue($returnType, $fieldNodes, $info, $path, $value); |
||
| 499 | }); |
||
| 500 | } |
||
| 501 | |||
| 502 | if ($result instanceof \Throwable) { |
||
| 503 | throw $result; |
||
| 504 | } |
||
| 505 | |||
| 506 | // If result is null-like, return null. |
||
| 507 | if (null === $result) { |
||
| 508 | return null; |
||
| 509 | } |
||
| 510 | |||
| 511 | if ($returnType instanceof NonNullType) { |
||
| 512 | $completed = $this->completeValue( |
||
| 513 | $returnType->getOfType(), |
||
| 514 | $fieldNodes, |
||
| 515 | $info, |
||
| 516 | $path, |
||
| 517 | $result |
||
| 518 | ); |
||
| 519 | |||
| 520 | if ($completed === null) { |
||
| 521 | throw new ExecutionException( |
||
| 522 | \sprintf( |
||
| 523 | 'Cannot return null for non-nullable field %s.%s.', |
||
| 524 | $info->getParentType(), |
||
| 525 | $info->getFieldName() |
||
| 526 | ) |
||
| 527 | ); |
||
| 528 | } |
||
| 529 | |||
| 530 | return $completed; |
||
| 531 | } |
||
| 532 | |||
| 533 | // If field type is List, complete each item in the list with the inner type |
||
| 534 | if ($returnType instanceof ListType) { |
||
| 535 | return $this->completeListValue($returnType, $fieldNodes, $info, $path, $result); |
||
| 536 | } |
||
| 537 | |||
| 538 | // If field type is Scalar or Enum, serialize to a valid value, returning |
||
| 539 | // null if serialization is not possible. |
||
| 540 | if ($returnType instanceof LeafTypeInterface) { |
||
| 541 | return $this->completeLeafValue($returnType, $result); |
||
| 542 | } |
||
| 543 | |||
| 544 | // TODO: Make a function for checking abstract type? |
||
| 545 | if ($returnType instanceof InterfaceType || $returnType instanceof UnionType) { |
||
| 546 | return $this->completeAbstractValue($returnType, $fieldNodes, $info, $path, $result); |
||
| 547 | } |
||
| 548 | |||
| 549 | // Field type must be Object, Interface or Union and expect sub-selections. |
||
| 550 | if ($returnType instanceof ObjectType) { |
||
| 551 | return $this->completeObjectValue($returnType, $fieldNodes, $info, $path, $result); |
||
| 552 | } |
||
| 553 | |||
| 554 | throw new ExecutionException("Cannot complete value of unexpected type \"{$returnType}\"."); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param AbstractTypeInterface $returnType |
||
| 559 | * @param FieldNode[] $fieldNodes |
||
| 560 | * @param ResolveInfo $info |
||
| 561 | * @param array $path |
||
| 562 | * @param mixed $result |
||
| 563 | * |
||
| 564 | * @return array|PromiseInterface |
||
| 565 | * @throws ExecutionException |
||
| 566 | * @throws InvalidTypeException |
||
| 567 | * @throws InvariantException |
||
| 568 | * @throws \Throwable |
||
| 569 | */ |
||
| 570 | protected function completeAbstractValue( |
||
| 571 | AbstractTypeInterface $returnType, |
||
| 572 | array $fieldNodes, |
||
| 573 | ResolveInfo $info, |
||
| 574 | array $path, |
||
| 575 | &$result |
||
| 576 | ) { |
||
| 577 | $runtimeType = $returnType->resolveType($result, $this->context->getContextValue(), $info); |
||
| 578 | |||
| 579 | if (null === $runtimeType) { |
||
| 580 | // TODO: Display warning |
||
| 581 | $runtimeType = $this->defaultTypeResolver($result, $this->context->getContextValue(), $info, $returnType); |
||
| 582 | } |
||
| 583 | |||
| 584 | if ($runtimeType instanceof ExtendedPromiseInterface) { |
||
| 585 | return $runtimeType->then(function ($resolvedRuntimeType) use ( |
||
| 586 | $returnType, |
||
| 587 | $fieldNodes, |
||
| 588 | $info, |
||
| 589 | $path, |
||
| 590 | &$result |
||
| 591 | ) { |
||
| 592 | return $this->completeObjectValue( |
||
| 593 | $this->ensureValidRuntimeType($resolvedRuntimeType, $returnType, $info, $result), |
||
| 594 | $fieldNodes, |
||
| 595 | $info, |
||
| 596 | $path, |
||
| 597 | $result |
||
| 598 | ); |
||
| 599 | }); |
||
| 600 | } |
||
| 601 | |||
| 602 | return $this->completeObjectValue( |
||
| 603 | $this->ensureValidRuntimeType($runtimeType, $returnType, $info, $result), |
||
| 604 | $fieldNodes, |
||
| 605 | $info, |
||
| 606 | $path, |
||
| 607 | $result |
||
| 608 | ); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param NamedTypeInterface|string $runtimeTypeOrName |
||
| 613 | * @param NamedTypeInterface $returnType |
||
| 614 | * @param ResolveInfo $info |
||
| 615 | * @param mixed $result |
||
| 616 | * |
||
| 617 | * @return TypeInterface|ObjectType|null |
||
| 618 | * @throws ExecutionException |
||
| 619 | * @throws InvariantException |
||
| 620 | */ |
||
| 621 | protected function ensureValidRuntimeType( |
||
| 622 | $runtimeTypeOrName, |
||
| 623 | NamedTypeInterface $returnType, |
||
| 624 | ResolveInfo $info, |
||
| 625 | &$result |
||
| 626 | ) { |
||
| 627 | /** @var NamedTypeInterface $runtimeType */ |
||
| 628 | $runtimeType = \is_string($runtimeTypeOrName) |
||
| 629 | ? $this->context->getSchema()->getType($runtimeTypeOrName) |
||
| 630 | : $runtimeTypeOrName; |
||
| 631 | |||
| 632 | $runtimeTypeName = $runtimeType->getName(); |
||
| 633 | $returnTypeName = $returnType->getName(); |
||
| 634 | |||
| 635 | if (!$runtimeType instanceof ObjectType) { |
||
| 636 | $parentTypeName = $info->getParentType()->getName(); |
||
| 637 | $fieldName = $info->getFieldName(); |
||
| 638 | |||
| 639 | throw new ExecutionException( |
||
| 640 | \sprintf( |
||
| 641 | 'Abstract type %s must resolve to an Object type at runtime for field %s.%s ' . |
||
| 642 | 'with value "%s", received "%s".', |
||
| 643 | $returnTypeName, |
||
| 644 | $parentTypeName, |
||
| 645 | $fieldName, |
||
| 646 | $result, |
||
| 647 | $runtimeTypeName |
||
| 648 | ) |
||
| 649 | ); |
||
| 650 | } |
||
| 651 | |||
| 652 | if (!$this->context->getSchema()->isPossibleType($returnType, $runtimeType)) { |
||
| 653 | throw new ExecutionException( |
||
| 654 | \sprintf('Runtime Object type "%s" is not a possible type for "%s".', $runtimeTypeName, $returnTypeName) |
||
| 655 | ); |
||
| 656 | } |
||
| 657 | |||
| 658 | if ($runtimeType !== $this->context->getSchema()->getType($runtimeType->getName())) { |
||
|
|
|||
| 659 | throw new ExecutionException( |
||
| 660 | \sprintf( |
||
| 661 | 'Schema must contain unique named types but contains multiple types named "%s". ' . |
||
| 662 | 'Make sure that `resolveType` function of abstract type "%s" returns the same ' . |
||
| 663 | 'type instance as referenced anywhere else within the schema.', |
||
| 664 | $runtimeTypeName, |
||
| 665 | $returnTypeName |
||
| 666 | ) |
||
| 667 | ); |
||
| 668 | } |
||
| 669 | |||
| 670 | return $runtimeType; |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @param mixed $value |
||
| 675 | * @param mixed $context |
||
| 676 | * @param ResolveInfo $info |
||
| 677 | * @param AbstractTypeInterface $abstractType |
||
| 678 | * |
||
| 679 | * @return NamedTypeInterface|mixed|null |
||
| 680 | * @throws InvariantException |
||
| 681 | */ |
||
| 682 | protected function defaultTypeResolver( |
||
| 683 | $value, |
||
| 684 | $context, |
||
| 685 | ResolveInfo $info, |
||
| 686 | AbstractTypeInterface $abstractType |
||
| 687 | ) { |
||
| 688 | if (\is_array($value) && isset($value['__typename'])) { |
||
| 689 | return $value['__typename']; |
||
| 690 | } |
||
| 691 | |||
| 692 | /** @var ObjectType[] $possibleTypes */ |
||
| 693 | $possibleTypes = $info->getSchema()->getPossibleTypes($abstractType); |
||
| 694 | $promisedIsTypeOfResults = []; |
||
| 695 | |||
| 696 | foreach ($possibleTypes as $index => $type) { |
||
| 697 | $isTypeOfResult = $type->isTypeOf($value, $context, $info); |
||
| 698 | |||
| 699 | if ($isTypeOfResult instanceof ExtendedPromiseInterface) { |
||
| 700 | $promisedIsTypeOfResults[$index] = $isTypeOfResult; |
||
| 701 | continue; |
||
| 702 | } |
||
| 703 | |||
| 704 | if ($isTypeOfResult === true) { |
||
| 705 | return $type; |
||
| 706 | } |
||
| 707 | |||
| 708 | if (\is_array($value)) { |
||
| 709 | // TODO: Make `type` configurable |
||
| 710 | /** @noinspection NestedPositiveIfStatementsInspection */ |
||
| 711 | if (isset($value['type']) && $value['type'] === $type->getName()) { |
||
| 712 | return $type; |
||
| 713 | } |
||
| 714 | } |
||
| 715 | } |
||
| 716 | |||
| 717 | if (!empty($promisedIsTypeOfResults)) { |
||
| 718 | return promiseAll($promisedIsTypeOfResults) |
||
| 719 | ->then(function ($isTypeOfResults) use ($possibleTypes) { |
||
| 720 | /** @noinspection ForeachSourceInspection */ |
||
| 721 | foreach ($isTypeOfResults as $index => $result) { |
||
| 722 | if ($result) { |
||
| 723 | return $possibleTypes[$index]; |
||
| 724 | } |
||
| 725 | } |
||
| 726 | |||
| 727 | return null; |
||
| 728 | }); |
||
| 729 | } |
||
| 730 | |||
| 731 | return null; |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param ListType $returnType |
||
| 736 | * @param FieldNode[] $fieldNodes |
||
| 737 | * @param ResolveInfo $info |
||
| 738 | * @param array $path |
||
| 739 | * @param mixed $result |
||
| 740 | * |
||
| 741 | * @return array|\React\Promise\Promise |
||
| 742 | * @throws \Throwable |
||
| 743 | */ |
||
| 744 | protected function completeListValue( |
||
| 778 | } |
||
| 779 | |||
| 780 | /** |
||
| 781 | * @param LeafTypeInterface|SerializableTypeInterface $returnType |
||
| 782 | * @param mixed $result |
||
| 783 | * |
||
| 784 | * @return mixed |
||
| 785 | * @throws ExecutionException |
||
| 786 | */ |
||
| 787 | protected function completeLeafValue($returnType, &$result) |
||
| 788 | { |
||
| 789 | $serializedResult = $returnType->serialize($result); |
||
| 790 | |||
| 791 | if ($serializedResult === null) { |
||
| 792 | // TODO: Make a method for this type of exception |
||
| 793 | throw new ExecutionException( |
||
| 794 | \sprintf('Expected value of type "%s" but received: %s.', (string)$returnType, toString($result)) |
||
| 795 | ); |
||
| 796 | } |
||
| 797 | |||
| 798 | return $serializedResult; |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @param ObjectType $returnType |
||
| 803 | * @param array $fieldNodes |
||
| 804 | * @param ResolveInfo $info |
||
| 805 | * @param array $path |
||
| 806 | * @param mixed $result |
||
| 807 | * |
||
| 808 | * @return array |
||
| 809 | * @throws ExecutionException |
||
| 810 | * @throws InvalidTypeException |
||
| 811 | * @throws InvariantException |
||
| 812 | * @throws \Throwable |
||
| 813 | */ |
||
| 814 | protected function completeObjectValue( |
||
| 815 | ObjectType $returnType, |
||
| 816 | array $fieldNodes, |
||
| 817 | ResolveInfo $info, |
||
| 818 | array $path, |
||
| 819 | &$result |
||
| 820 | ): array { |
||
| 821 | if (null !== $returnType->getIsTypeOf()) { |
||
| 822 | $isTypeOf = $returnType->isTypeOf($result, $this->context->getContextValue(), $info); |
||
| 823 | |||
| 824 | // TODO: Check for promise? |
||
| 825 | if (!$isTypeOf) { |
||
| 826 | throw new ExecutionException( |
||
| 827 | sprintf('Expected value of type "%s" but received: %s.', (string)$returnType, toString($result)) |
||
| 828 | ); |
||
| 829 | } |
||
| 830 | } |
||
| 831 | |||
| 832 | return $this->executeSubFields($returnType, $fieldNodes, $path, $result); |
||
| 833 | } |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @param Field $field |
||
| 837 | * @param FieldNode $fieldNode |
||
| 838 | * @param callable $resolveCallback |
||
| 839 | * @param mixed $rootValue |
||
| 840 | * @param ExecutionContext $context |
||
| 841 | * @param ResolveInfo $info |
||
| 842 | * |
||
| 843 | * @return array|\Throwable |
||
| 844 | */ |
||
| 845 | protected function resolveFieldValueOrError( |
||
| 846 | Field $field, |
||
| 847 | FieldNode $fieldNode, |
||
| 848 | ?callable $resolveCallback, |
||
| 849 | $rootValue, |
||
| 850 | ExecutionContext $context, |
||
| 851 | ResolveInfo $info |
||
| 852 | ) { |
||
| 853 | try { |
||
| 854 | $result = $resolveCallback( |
||
| 855 | $rootValue, |
||
| 856 | coerceArgumentValues($field, $fieldNode, $context->getVariableValues()), |
||
| 857 | $context->getContextValue(), |
||
| 858 | $info |
||
| 859 | ); |
||
| 860 | } catch (\Throwable $error) { |
||
| 861 | return $error; |
||
| 862 | } |
||
| 863 | |||
| 864 | return $result; |
||
| 865 | } |
||
| 866 | |||
| 867 | /** |
||
| 868 | * @param ObjectType $returnType |
||
| 869 | * @param FieldNode[] $fieldNodes |
||
| 870 | * @param array $path |
||
| 871 | * @param mixed $result |
||
| 872 | * |
||
| 873 | * @return array |
||
| 874 | * @throws ExecutionException |
||
| 875 | * @throws InvalidTypeException |
||
| 876 | * @throws InvariantException |
||
| 877 | * @throws \Throwable |
||
| 878 | */ |
||
| 879 | protected function executeSubFields( |
||
| 880 | ObjectType $returnType, |
||
| 881 | array $fieldNodes, |
||
| 882 | array $path, |
||
| 883 | &$result |
||
| 884 | ): array { |
||
| 885 | $subFields = []; |
||
| 886 | $visitedFragmentNames = []; |
||
| 887 | |||
| 888 | foreach ($fieldNodes as $fieldNode) { |
||
| 889 | if (null !== $fieldNode->getSelectionSet()) { |
||
| 890 | $subFields = $this->fieldCollector->collectFields( |
||
| 891 | $returnType, |
||
| 892 | $fieldNode->getSelectionSet(), |
||
| 893 | $subFields, |
||
| 894 | $visitedFragmentNames |
||
| 895 | ); |
||
| 896 | } |
||
| 897 | } |
||
| 898 | |||
| 899 | if (!empty($subFields)) { |
||
| 900 | return $this->executeFields($returnType, $result, $path, $subFields); |
||
| 901 | } |
||
| 902 | |||
| 903 | return $result; |
||
| 904 | } |
||
| 905 | |||
| 906 | /** |
||
| 907 | * @param \Throwable $originalException |
||
| 908 | * @param array $nodes |
||
| 909 | * @param array $path |
||
| 910 | * |
||
| 911 | * @return ExecutionException |
||
| 912 | */ |
||
| 913 | protected function buildLocatedError( |
||
| 914 | \Throwable $originalException, |
||
| 915 | array $nodes = [], |
||
| 916 | array $path = [] |
||
| 917 | ): ExecutionException { |
||
| 918 | return new ExecutionException( |
||
| 919 | $originalException->getMessage(), |
||
| 920 | $originalException instanceof GraphQLException |
||
| 921 | ? $originalException->getNodes() |
||
| 922 | : $nodes, |
||
| 923 | $originalException instanceof GraphQLException |
||
| 924 | ? $originalException->getSource() |
||
| 925 | : null, |
||
| 926 | $originalException instanceof GraphQLException |
||
| 927 | ? $originalException->getPositions() |
||
| 928 | : null, |
||
| 929 | $originalException instanceof GraphQLException |
||
| 930 | ? ($originalException->getPath() ?? $path) |
||
| 931 | : $path, |
||
| 932 | null, |
||
| 933 | $originalException |
||
| 934 | ); |
||
| 935 | } |
||
| 936 | |||
| 937 | /** |
||
| 938 | * @param FieldNode[] $fieldNodes |
||
| 939 | * @param FieldNode $fieldNode |
||
| 940 | * @param Field $field |
||
| 941 | * @param ObjectType $parentType |
||
| 942 | * @param array|null $path |
||
| 943 | * @param ExecutionContext $context |
||
| 944 | * |
||
| 945 | * @return ResolveInfo |
||
| 946 | */ |
||
| 947 | protected function createResolveInfo( |
||
| 948 | array $fieldNodes, |
||
| 949 | FieldNode $fieldNode, |
||
| 950 | Field $field, |
||
| 951 | ObjectType $parentType, |
||
| 952 | ?array $path, |
||
| 953 | ExecutionContext $context |
||
| 954 | ): ResolveInfo { |
||
| 955 | return new ResolveInfo( |
||
| 956 | $fieldNode->getNameValue(), |
||
| 957 | $fieldNodes, |
||
| 958 | $field->getType(), |
||
| 959 | $parentType, |
||
| 960 | $path, |
||
| 961 | $context->getSchema(), |
||
| 962 | $context->getFragments(), |
||
| 963 | $context->getRootValue(), |
||
| 964 | $context->getOperation(), |
||
| 965 | $context->getVariableValues() |
||
| 966 | ); |
||
| 967 | } |
||
| 968 | |||
| 969 | /** |
||
| 970 | * @param ExecutionException $error |
||
| 971 | */ |
||
| 972 | protected function handleError(ExecutionException $error) |
||
| 979 | } |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Try to resolve a field without any field resolver function. |
||
| 983 | * |
||
| 984 | * @param array|object $rootValue |
||
| 985 | * @param array $arguments |
||
| 986 | * @param mixed $contextValues |
||
| 987 | * @param ResolveInfo $info |
||
| 988 | * |
||
| 989 | * @return mixed|null |
||
| 990 | */ |
||
| 991 | public static function defaultFieldResolver($rootValue, array $arguments, $contextValues, ResolveInfo $info) |
||
| 1014 | } |
||
| 1015 | } |
||
| 1016 |