| Total Complexity | 113 |
| Total Lines | 990 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ExecutionStrategy 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 ExecutionStrategy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class ExecutionStrategy |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var ExecutionContext |
||
| 42 | */ |
||
| 43 | protected $context; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var OperationDefinitionNode |
||
| 47 | */ |
||
| 48 | protected $operation; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var mixed |
||
| 52 | */ |
||
| 53 | protected $rootValue; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $finalResult; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected static $defaultFieldResolver = [__CLASS__, 'defaultFieldResolver']; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * AbstractStrategy constructor. |
||
| 67 | * @param ExecutionContext $context |
||
| 68 | * |
||
| 69 | * @param OperationDefinitionNode $operation |
||
| 70 | */ |
||
| 71 | public function __construct( |
||
| 72 | ExecutionContext $context, |
||
| 73 | OperationDefinitionNode $operation, |
||
| 74 | $rootValue |
||
| 75 | ) { |
||
| 76 | $this->context = $context; |
||
| 77 | $this->operation = $operation; |
||
| 78 | $this->rootValue = $rootValue; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return array|null |
||
| 83 | */ |
||
| 84 | abstract function execute(): ?array; |
||
|
|
|||
| 85 | |||
| 86 | /** |
||
| 87 | * @param ObjectType $runtimeType |
||
| 88 | * @param SelectionSetNode $selectionSet |
||
| 89 | * @param $fields |
||
| 90 | * @param $visitedFragmentNames |
||
| 91 | * @return mixed |
||
| 92 | * @throws InvalidTypeException |
||
| 93 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 94 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 95 | */ |
||
| 96 | protected function collectFields( |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $node |
||
| 142 | * @return bool |
||
| 143 | * @throws InvalidTypeException |
||
| 144 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 145 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 146 | */ |
||
| 147 | private function shouldIncludeNode(NodeInterface $node): bool |
||
| 148 | { |
||
| 149 | |||
| 150 | $contextVariables = $this->context->getVariableValues(); |
||
| 151 | |||
| 152 | $skip = coerceDirectiveValues(SkipDirective(), $node, $contextVariables); |
||
| 153 | |||
| 154 | if ($skip && $skip['if'] === true) { |
||
| 155 | return false; |
||
| 156 | } |
||
| 157 | |||
| 158 | $include = coerceDirectiveValues(IncludeDirective(), $node, $contextVariables); |
||
| 159 | |||
| 160 | if ($include && $include['if'] === false) { |
||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | return true; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param FragmentDefinitionNode|InlineFragmentNode $fragment |
||
| 169 | * @param ObjectType $type |
||
| 170 | * @return bool |
||
| 171 | * @throws InvalidTypeException |
||
| 172 | */ |
||
| 173 | private function doesFragmentConditionMatch( |
||
| 174 | NodeInterface $fragment, |
||
| 175 | ObjectType $type |
||
| 176 | ): bool { |
||
| 177 | $typeConditionNode = $fragment->getTypeCondition(); |
||
| 178 | |||
| 179 | if (!$typeConditionNode) { |
||
| 180 | return true; |
||
| 181 | } |
||
| 182 | |||
| 183 | $conditionalType = typeFromAST($this->context->getSchema(), $typeConditionNode); |
||
| 184 | |||
| 185 | if ($conditionalType === $type) { |
||
| 186 | return true; |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($conditionalType instanceof AbstractTypeInterface) { |
||
| 190 | return $this->context->getSchema()->isPossibleType($conditionalType, $type); |
||
| 191 | } |
||
| 192 | |||
| 193 | return false; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @TODO: consider to move this to FieldNode |
||
| 198 | * @param FieldNode $node |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | private function getFieldNameKey(FieldNode $node): string |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Implements the "Evaluating selection sets" section of the spec for "read" mode. |
||
| 208 | * @param ObjectType $objectType |
||
| 209 | * @param $rootValue |
||
| 210 | * @param $path |
||
| 211 | * @param $fields |
||
| 212 | * @return array |
||
| 213 | * @throws InvalidTypeException |
||
| 214 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 215 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 216 | * @throws \Throwable |
||
| 217 | */ |
||
| 218 | protected function executeFields( |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Implements the "Evaluating selection sets" section of the spec for "write" mode. |
||
| 257 | * |
||
| 258 | * @param ObjectType $objectType |
||
| 259 | * @param $rootValue |
||
| 260 | * @param $path |
||
| 261 | * @param $fields |
||
| 262 | * @return array |
||
| 263 | * @throws InvalidTypeException |
||
| 264 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 265 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 266 | * @throws \Throwable |
||
| 267 | */ |
||
| 268 | public function executeFieldsSerially( |
||
| 269 | ObjectType $objectType, |
||
| 270 | $rootValue, |
||
| 271 | $path, |
||
| 272 | $fields |
||
| 273 | ) { |
||
| 274 | |||
| 275 | $finalResults = []; |
||
| 276 | |||
| 277 | $promise = new \React\Promise\FulfilledPromise([]); |
||
| 278 | |||
| 279 | $resolve = function ($results, $fieldName, $path, $objectType, $rootValue, $fieldNodes) { |
||
| 280 | $fieldPath = $path; |
||
| 281 | $fieldPath[] = $fieldName; |
||
| 282 | try { |
||
| 283 | $result = $this->resolveField($objectType, $rootValue, $fieldNodes, $fieldPath); |
||
| 284 | } catch (UndefinedException $ex) { |
||
| 285 | return null; |
||
| 286 | } |
||
| 287 | |||
| 288 | if ($this->isPromise($result)) { |
||
| 289 | /** @var ExtendedPromiseInterface $result */ |
||
| 290 | return $result->then(function ($resolvedResult) use ($fieldName, $results) { |
||
| 291 | $results[$fieldName] = $resolvedResult; |
||
| 292 | return $results; |
||
| 293 | }); |
||
| 294 | } |
||
| 295 | |||
| 296 | $results[$fieldName] = $result; |
||
| 297 | |||
| 298 | return $results; |
||
| 299 | }; |
||
| 300 | |||
| 301 | foreach ($fields as $fieldName => $fieldNodes) { |
||
| 302 | $promise = $promise->then(function ($resolvedResults) use ( |
||
| 303 | $resolve, |
||
| 304 | $fieldName, |
||
| 305 | $path, |
||
| 306 | $objectType, |
||
| 307 | $rootValue, |
||
| 308 | $fieldNodes |
||
| 309 | ) { |
||
| 310 | return $resolve($resolvedResults, $fieldName, $path, $objectType, $rootValue, $fieldNodes); |
||
| 311 | }); |
||
| 312 | } |
||
| 313 | |||
| 314 | $promise->then(function ($resolvedResults) use (&$finalResults) { |
||
| 315 | $finalResults = $resolvedResults ?? []; |
||
| 316 | }); |
||
| 317 | |||
| 318 | return $finalResults; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param Schema $schema |
||
| 323 | * @param ObjectType $parentType |
||
| 324 | * @param string $fieldName |
||
| 325 | * @return \Digia\GraphQL\Type\Definition\Field|null |
||
| 326 | * @throws InvalidTypeException |
||
| 327 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 328 | */ |
||
| 329 | public function getFieldDefinition( |
||
| 330 | Schema $schema, |
||
| 331 | ObjectType $parentType, |
||
| 332 | string $fieldName |
||
| 333 | ) { |
||
| 334 | $schemaMetaFieldDefinition = SchemaMetaFieldDefinition(); |
||
| 335 | $typeMetaFieldDefinition = TypeMetaFieldDefinition(); |
||
| 336 | $typeNameMetaFieldDefinition = TypeNameMetaFieldDefinition(); |
||
| 337 | |||
| 338 | if ($fieldName === $schemaMetaFieldDefinition->getName() && $schema->getQueryType() === $parentType) { |
||
| 339 | return $schemaMetaFieldDefinition; |
||
| 340 | } |
||
| 341 | |||
| 342 | if ($fieldName === $typeMetaFieldDefinition->getName() && $schema->getQueryType() === $parentType) { |
||
| 343 | return $typeMetaFieldDefinition; |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($fieldName === $typeNameMetaFieldDefinition->getName()) { |
||
| 347 | return $typeNameMetaFieldDefinition; |
||
| 348 | } |
||
| 349 | |||
| 350 | $fields = $parentType->getFields(); |
||
| 351 | |||
| 352 | return $fields[$fieldName] ?? null; |
||
| 353 | } |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * @param ObjectType $parentType |
||
| 358 | * @param $rootValue |
||
| 359 | * @param $fieldNodes |
||
| 360 | * @param $path |
||
| 361 | * @return array|null|\Throwable |
||
| 362 | * @throws InvalidTypeException |
||
| 363 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 364 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 365 | * @throws \Throwable |
||
| 366 | */ |
||
| 367 | protected function resolveField( |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param array $fieldNodes |
||
| 408 | * @param FieldNode $fieldNode |
||
| 409 | * @param Field $field |
||
| 410 | * @param ObjectType $parentType |
||
| 411 | * @param array|null $path |
||
| 412 | * @param ExecutionContext $context |
||
| 413 | * @return ResolveInfo |
||
| 414 | */ |
||
| 415 | private function buildResolveInfo( |
||
| 416 | array $fieldNodes, |
||
| 417 | FieldNode $fieldNode, |
||
| 418 | Field $field, |
||
| 419 | ObjectType $parentType, |
||
| 420 | ?array $path, |
||
| 421 | ExecutionContext $context |
||
| 422 | ) { |
||
| 423 | return new ResolveInfo( |
||
| 424 | $fieldNode->getNameValue(), |
||
| 425 | $fieldNodes, |
||
| 426 | $field->getType(), |
||
| 427 | $parentType, |
||
| 428 | $path, |
||
| 429 | $context->getSchema(), |
||
| 430 | $context->getFragments(), |
||
| 431 | $context->getRootValue(), |
||
| 432 | $context->getOperation(), |
||
| 433 | $context->getVariableValues() |
||
| 434 | ); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param Field $field |
||
| 439 | * @param ObjectType $objectType |
||
| 440 | * @param ExecutionContext $context |
||
| 441 | * @return callable|mixed|null |
||
| 442 | */ |
||
| 443 | private function determineResolveFunction( |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param TypeInterface $fieldType |
||
| 462 | * @param $fieldNodes |
||
| 463 | * @param ResolveInfo $info |
||
| 464 | * @param $path |
||
| 465 | * @param $result |
||
| 466 | * @return null |
||
| 467 | * @throws \Throwable |
||
| 468 | */ |
||
| 469 | public function completeValueCatchingError( |
||
| 470 | TypeInterface $fieldType, |
||
| 471 | $fieldNodes, |
||
| 472 | ResolveInfo $info, |
||
| 473 | $path, |
||
| 474 | &$result |
||
| 475 | ) { |
||
| 476 | if ($fieldType instanceof NonNullType) { |
||
| 477 | return $this->completeValueWithLocatedError( |
||
| 478 | $fieldType, |
||
| 479 | $fieldNodes, |
||
| 480 | $info, |
||
| 481 | $path, |
||
| 482 | $result |
||
| 483 | ); |
||
| 484 | } |
||
| 485 | |||
| 486 | try { |
||
| 487 | $completed = $this->completeValueWithLocatedError( |
||
| 488 | $fieldType, |
||
| 489 | $fieldNodes, |
||
| 490 | $info, |
||
| 491 | $path, |
||
| 492 | $result |
||
| 493 | ); |
||
| 494 | |||
| 495 | if ($this->isPromise($completed)) { |
||
| 496 | $context = $this->context; |
||
| 497 | /** @var ExtendedPromiseInterface $completed */ |
||
| 498 | return $completed->then(null, function ($error) use ($context, $fieldNodes, $path) { |
||
| 499 | //@TODO Handle $error better |
||
| 500 | if ($error instanceof \Exception) { |
||
| 501 | $context->addError($this->buildLocatedError($error, $fieldNodes, $path)); |
||
| 502 | } else { |
||
| 503 | $context->addError( |
||
| 504 | $this->buildLocatedError( |
||
| 505 | new ExecutionException($error ?? 'An unknown error occurred.'), |
||
| 506 | $fieldNodes, |
||
| 507 | $path |
||
| 508 | ) |
||
| 509 | ); |
||
| 510 | } |
||
| 511 | return new \React\Promise\FulfilledPromise(null); |
||
| 512 | }); |
||
| 513 | } |
||
| 514 | |||
| 515 | return $completed; |
||
| 516 | } catch (ExecutionException $ex) { |
||
| 517 | $this->context->addError($ex); |
||
| 518 | return null; |
||
| 519 | } catch (\Exception $ex) { |
||
| 520 | $this->context->addError($this->buildLocatedError($ex, $fieldNodes, $path)); |
||
| 521 | return null; |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param TypeInterface $fieldType |
||
| 527 | * @param $fieldNodes |
||
| 528 | * @param ResolveInfo $info |
||
| 529 | * @param $path |
||
| 530 | * @param $result |
||
| 531 | * @throws \Throwable |
||
| 532 | */ |
||
| 533 | public function completeValueWithLocatedError( |
||
| 534 | TypeInterface $fieldType, |
||
| 535 | $fieldNodes, |
||
| 536 | ResolveInfo $info, |
||
| 537 | $path, |
||
| 538 | $result |
||
| 539 | ) { |
||
| 540 | try { |
||
| 541 | $completed = $this->completeValue( |
||
| 542 | $fieldType, |
||
| 543 | $fieldNodes, |
||
| 544 | $info, |
||
| 545 | $path, |
||
| 546 | $result |
||
| 547 | ); |
||
| 548 | |||
| 549 | return $completed; |
||
| 550 | } catch (\Throwable $ex) { |
||
| 551 | throw $this->buildLocatedError($ex, $fieldNodes, $path); |
||
| 552 | } |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param TypeInterface $returnType |
||
| 557 | * @param $fieldNodes |
||
| 558 | * @param ResolveInfo $info |
||
| 559 | * @param $path |
||
| 560 | * @param $result |
||
| 561 | * @return array|mixed |
||
| 562 | * @throws ExecutionException |
||
| 563 | * @throws \Throwable |
||
| 564 | */ |
||
| 565 | private function completeValue( |
||
| 566 | TypeInterface $returnType, |
||
| 567 | $fieldNodes, |
||
| 568 | ResolveInfo $info, |
||
| 569 | $path, |
||
| 570 | &$result |
||
| 571 | ) { |
||
| 572 | if ($this->isPromise($result)) { |
||
| 573 | /** @var ExtendedPromiseInterface $result */ |
||
| 574 | return $result->then(function (&$value) use ($returnType, $fieldNodes, $info, $path) { |
||
| 575 | return $this->completeValue($returnType, $fieldNodes, $info, $path, $value); |
||
| 576 | }); |
||
| 577 | } |
||
| 578 | |||
| 579 | if ($result instanceof \Throwable) { |
||
| 580 | throw $result; |
||
| 581 | } |
||
| 582 | |||
| 583 | // If result is null-like, return null. |
||
| 584 | if (null === $result) { |
||
| 585 | return null; |
||
| 586 | } |
||
| 587 | |||
| 588 | if ($returnType instanceof NonNullType) { |
||
| 589 | $completed = $this->completeValue( |
||
| 590 | $returnType->getOfType(), |
||
| 591 | $fieldNodes, |
||
| 592 | $info, |
||
| 593 | $path, |
||
| 594 | $result |
||
| 595 | ); |
||
| 596 | |||
| 597 | if ($completed === null) { |
||
| 598 | throw new ExecutionException( |
||
| 599 | sprintf( |
||
| 600 | 'Cannot return null for non-nullable field %s.%s.', |
||
| 601 | $info->getParentType(), $info->getFieldName() |
||
| 602 | ) |
||
| 603 | ); |
||
| 604 | } |
||
| 605 | |||
| 606 | return $completed; |
||
| 607 | } |
||
| 608 | |||
| 609 | // If field type is List, complete each item in the list with the inner type |
||
| 610 | if ($returnType instanceof ListType) { |
||
| 611 | return $this->completeListValue($returnType, $fieldNodes, $info, $path, $result); |
||
| 612 | } |
||
| 613 | |||
| 614 | |||
| 615 | // If field type is Scalar or Enum, serialize to a valid value, returning |
||
| 616 | // null if serialization is not possible. |
||
| 617 | if ($returnType instanceof LeafTypeInterface) { |
||
| 618 | return $this->completeLeafValue($returnType, $result); |
||
| 619 | } |
||
| 620 | |||
| 621 | //@TODO Make a function for checking abstract type? |
||
| 622 | if ($returnType instanceof InterfaceType || $returnType instanceof UnionType) { |
||
| 623 | return $this->completeAbstractValue($returnType, $fieldNodes, $info, $path, $result); |
||
| 624 | } |
||
| 625 | |||
| 626 | // Field type must be Object, Interface or Union and expect sub-selections. |
||
| 627 | if ($returnType instanceof ObjectType) { |
||
| 628 | return $this->completeObjectValue($returnType, $fieldNodes, $info, $path, $result); |
||
| 629 | } |
||
| 630 | |||
| 631 | throw new ExecutionException("Cannot complete value of unexpected type \"{$returnType}\"."); |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @param AbstractTypeInterface $returnType |
||
| 636 | * @param $fieldNodes |
||
| 637 | * @param ResolveInfo $info |
||
| 638 | * @param $path |
||
| 639 | * @param $result |
||
| 640 | * @return array|PromiseInterface |
||
| 641 | * @throws ExecutionException |
||
| 642 | * @throws InvalidTypeException |
||
| 643 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 644 | * @throws \Throwable |
||
| 645 | */ |
||
| 646 | private function completeAbstractValue( |
||
| 647 | AbstractTypeInterface $returnType, |
||
| 648 | $fieldNodes, |
||
| 649 | ResolveInfo $info, |
||
| 650 | $path, |
||
| 651 | &$result |
||
| 652 | ) { |
||
| 653 | $runtimeType = $returnType->resolveType($result, $this->context->getContextValue(), $info); |
||
| 654 | |||
| 655 | if (null === $runtimeType) { |
||
| 656 | //@TODO Show warning |
||
| 657 | $runtimeType = $this->defaultTypeResolver($result, $this->context->getContextValue(), $info, $returnType); |
||
| 658 | } |
||
| 659 | |||
| 660 | if ($this->isPromise($runtimeType)) { |
||
| 661 | /** @var ExtendedPromiseInterface $runtimeType */ |
||
| 662 | return $runtimeType->then(function ($resolvedRuntimeType) use ( |
||
| 663 | $returnType, |
||
| 664 | $fieldNodes, |
||
| 665 | $info, |
||
| 666 | $path, |
||
| 667 | &$result |
||
| 668 | ) { |
||
| 669 | return $this->completeObjectValue( |
||
| 670 | $this->ensureValidRuntimeType( |
||
| 671 | $resolvedRuntimeType, |
||
| 672 | $returnType, |
||
| 673 | $fieldNodes, |
||
| 674 | $info, |
||
| 675 | $result |
||
| 676 | ), |
||
| 677 | $fieldNodes, |
||
| 678 | $info, |
||
| 679 | $path, |
||
| 680 | $result |
||
| 681 | ); |
||
| 682 | }); |
||
| 683 | } |
||
| 684 | |||
| 685 | return $this->completeObjectValue( |
||
| 686 | $this->ensureValidRuntimeType( |
||
| 687 | $runtimeType, |
||
| 688 | $returnType, |
||
| 689 | $fieldNodes, |
||
| 690 | $info, |
||
| 691 | $result |
||
| 692 | ), |
||
| 693 | $fieldNodes, |
||
| 694 | $info, |
||
| 695 | $path, |
||
| 696 | $result |
||
| 697 | ); |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @param $runtimeTypeOrName |
||
| 702 | * @param AbstractTypeInterface $returnType |
||
| 703 | * @param $fieldNodes |
||
| 704 | * @param ResolveInfo $info |
||
| 705 | * @param $result |
||
| 706 | * @return TypeInterface|ObjectType|null |
||
| 707 | * @throws ExecutionException |
||
| 708 | */ |
||
| 709 | private function ensureValidRuntimeType( |
||
| 710 | $runtimeTypeOrName, |
||
| 711 | AbstractTypeInterface $returnType, |
||
| 712 | $fieldNodes, |
||
| 713 | ResolveInfo $info, |
||
| 714 | &$result |
||
| 715 | ) { |
||
| 716 | $runtimeType = is_string($runtimeTypeOrName) |
||
| 717 | ? $this->context->getSchema()->getType($runtimeTypeOrName) |
||
| 718 | : $runtimeTypeOrName; |
||
| 719 | |||
| 720 | $runtimeTypeName = is_string($runtimeType) ?: $runtimeType->getName(); |
||
| 721 | $returnTypeName = $returnType->getName(); |
||
| 722 | |||
| 723 | if (!$runtimeType instanceof ObjectType) { |
||
| 724 | $parentTypeName = $info->getParentType()->getName(); |
||
| 725 | $fieldName = $info->getFieldName(); |
||
| 726 | |||
| 727 | throw new ExecutionException( |
||
| 728 | "Abstract type {$returnTypeName} must resolve to an Object type at runtime " . |
||
| 729 | "for field {$parentTypeName}.{$fieldName} with " . |
||
| 730 | 'value "' . $result . '", received "{$runtimeTypeName}".' |
||
| 731 | ); |
||
| 732 | } |
||
| 733 | |||
| 734 | if (!$this->context->getSchema()->isPossibleType($returnType, $runtimeType)) { |
||
| 735 | throw new ExecutionException( |
||
| 736 | "Runtime Object type \"{$runtimeTypeName}\" is not a possible type for \"{$returnTypeName}\"." |
||
| 737 | ); |
||
| 738 | } |
||
| 739 | |||
| 740 | if ($runtimeType !== $this->context->getSchema()->getType($runtimeType->getName())) { |
||
| 741 | throw new ExecutionException( |
||
| 742 | "Schema must contain unique named types but contains multiple types named \"{$runtimeTypeName}\". " . |
||
| 743 | "Make sure that `resolveType` function of abstract type \"{$returnTypeName}\" returns the same " . |
||
| 744 | "type instance as referenced anywhere else within the schema." |
||
| 745 | ); |
||
| 746 | } |
||
| 747 | |||
| 748 | return $runtimeType; |
||
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param $value |
||
| 753 | * @param $context |
||
| 754 | * @param ResolveInfo $info |
||
| 755 | * @param AbstractTypeInterface $abstractType |
||
| 756 | * @return TypeInterface|null |
||
| 757 | * @throws ExecutionException |
||
| 758 | */ |
||
| 759 | private function defaultTypeResolver($value, $context, ResolveInfo $info, AbstractTypeInterface $abstractType) |
||
| 798 | } |
||
| 799 | |||
| 800 | /** |
||
| 801 | * @param ListType $returnType |
||
| 802 | * @param $fieldNodes |
||
| 803 | * @param ResolveInfo $info |
||
| 804 | * @param $path |
||
| 805 | * @param $result |
||
| 806 | * @return array|\React\Promise\Promise |
||
| 807 | * @throws \Throwable |
||
| 808 | */ |
||
| 809 | private function completeListValue( |
||
| 810 | ListType $returnType, |
||
| 811 | $fieldNodes, |
||
| 812 | ResolveInfo $info, |
||
| 813 | $path, |
||
| 814 | &$result |
||
| 815 | ) { |
||
| 816 | $itemType = $returnType->getOfType(); |
||
| 817 | |||
| 818 | $completedItems = []; |
||
| 819 | $containsPromise = false; |
||
| 820 | |||
| 821 | if (!is_array($result) && !($result instanceof \Traversable)) { |
||
| 822 | throw new \Exception( |
||
| 823 | sprintf( |
||
| 824 | 'Expected Array or Traversable, but did not find one for field %s.%s.', |
||
| 825 | $info->getParentType()->getName(), |
||
| 826 | $info->getFieldName() |
||
| 827 | ) |
||
| 828 | ); |
||
| 829 | } |
||
| 830 | |||
| 831 | foreach ($result as $key => $item) { |
||
| 832 | $fieldPath = $path; |
||
| 833 | $fieldPath[] = $key; |
||
| 834 | $completedItem = $this->completeValueCatchingError($itemType, $fieldNodes, $info, $fieldPath, $item); |
||
| 835 | $completedItems[] = $completedItem; |
||
| 836 | $containsPromise = $containsPromise || $this->isPromise($completedItem); |
||
| 837 | } |
||
| 838 | |||
| 839 | return $containsPromise ? \React\Promise\all($completedItems) : $completedItems; |
||
| 840 | } |
||
| 841 | |||
| 842 | /** |
||
| 843 | * @param LeafTypeInterface $returnType |
||
| 844 | * @param $result |
||
| 845 | * @return mixed |
||
| 846 | * @throws ExecutionException |
||
| 847 | */ |
||
| 848 | private function completeLeafValue(LeafTypeInterface $returnType, &$result) |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * @param ObjectType $returnType |
||
| 864 | * @param $fieldNodes |
||
| 865 | * @param ResolveInfo $info |
||
| 866 | * @param $path |
||
| 867 | * @param $result |
||
| 868 | * @return array |
||
| 869 | * @throws ExecutionException |
||
| 870 | * @throws InvalidTypeException |
||
| 871 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 872 | * @throws \Throwable |
||
| 873 | */ |
||
| 874 | private function completeObjectValue( |
||
| 898 | ); |
||
| 899 | } |
||
| 900 | |||
| 901 | /** |
||
| 902 | * @param Field $field |
||
| 903 | * @param FieldNode $fieldNode |
||
| 904 | * @param callable $resolveFunction |
||
| 905 | * @param $rootValue |
||
| 906 | * @param ExecutionContext $context |
||
| 907 | * @param ResolveInfo $info |
||
| 908 | * @return array|\Throwable |
||
| 909 | */ |
||
| 910 | private function resolveFieldValueOrError( |
||
| 924 | } |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Try to resolve a field without any field resolver function. |
||
| 929 | * |
||
| 930 | * @param array|object $rootValue |
||
| 931 | * @param $args |
||
| 932 | * @param $context |
||
| 933 | * @param ResolveInfo $info |
||
| 934 | * @return mixed|null |
||
| 935 | */ |
||
| 936 | public static function defaultFieldResolver($rootValue, $args, $context, ResolveInfo $info) |
||
| 937 | { |
||
| 938 | $fieldName = $info->getFieldName(); |
||
| 939 | $property = null; |
||
| 940 | |||
| 941 | if (is_array($rootValue) && isset($rootValue[$fieldName])) { |
||
| 942 | $property = $rootValue[$fieldName]; |
||
| 943 | } |
||
| 944 | |||
| 945 | if (is_object($rootValue)) { |
||
| 946 | $getter = 'get' . ucfirst($fieldName); |
||
| 947 | if (method_exists($rootValue, $getter)) { |
||
| 948 | $property = $rootValue->{$getter}(); |
||
| 949 | } elseif (method_exists($rootValue, $fieldName)) { |
||
| 950 | $property = $rootValue->{$fieldName}($rootValue, $args, $context, $info); |
||
| 951 | } elseif (property_exists($rootValue, $fieldName)) { |
||
| 952 | $property = $rootValue->{$fieldName}; |
||
| 953 | } |
||
| 954 | } |
||
| 955 | |||
| 956 | |||
| 957 | return $property instanceof \Closure ? $property($rootValue, $args, $context, $info) : $property; |
||
| 958 | } |
||
| 959 | |||
| 960 | /** |
||
| 961 | * @param ObjectType $returnType |
||
| 962 | * @param $fieldNodes |
||
| 963 | * @param ResolveInfo $info |
||
| 964 | * @param $path |
||
| 965 | * @param $result |
||
| 966 | * @return array |
||
| 967 | * @throws InvalidTypeException |
||
| 968 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 969 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 970 | * @throws \Throwable |
||
| 971 | */ |
||
| 972 | private function collectAndExecuteSubFields( |
||
| 999 | } |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * @param $value |
||
| 1003 | * @return bool |
||
| 1004 | */ |
||
| 1005 | protected function isPromise($value): bool |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * @param \Throwable $originalException |
||
| 1012 | * @param array $nodes |
||
| 1013 | * @param string|array $path |
||
| 1014 | * @return GraphQLException |
||
| 1015 | */ |
||
| 1016 | protected function buildLocatedError( |
||
| 1028 | ); |
||
| 1029 | } |
||
| 1031 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.