| Total Complexity | 84 |
| Total Lines | 832 |
| 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 |
||
| 35 | abstract class ExecutionStrategy |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var ExecutionContext |
||
| 39 | */ |
||
| 40 | protected $context; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var OperationDefinitionNode |
||
| 44 | */ |
||
| 45 | protected $operation; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var mixed |
||
| 49 | */ |
||
| 50 | protected $rootValue; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * @var ValuesResolver |
||
| 55 | */ |
||
| 56 | protected $valuesResolver; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $finalResult; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected static $defaultFieldResolver = [__CLASS__, 'defaultFieldResolver']; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * AbstractStrategy constructor. |
||
| 70 | * @param ExecutionContext $context |
||
| 71 | * |
||
| 72 | * @param OperationDefinitionNode $operation |
||
| 73 | */ |
||
| 74 | public function __construct( |
||
| 75 | ExecutionContext $context, |
||
| 76 | OperationDefinitionNode $operation, |
||
| 77 | $rootValue |
||
| 78 | ) { |
||
| 79 | $this->context = $context; |
||
| 80 | $this->operation = $operation; |
||
| 81 | $this->rootValue = $rootValue; |
||
| 82 | $this->valuesResolver = new ValuesResolver(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return array|null |
||
| 87 | */ |
||
| 88 | abstract function execute(): ?array; |
||
|
|
|||
| 89 | |||
| 90 | /** |
||
| 91 | * @param ObjectType $runtimeType |
||
| 92 | * @param SelectionSetNode $selectionSet |
||
| 93 | * @param $fields |
||
| 94 | * @param $visitedFragmentNames |
||
| 95 | * @return mixed |
||
| 96 | * @throws InvalidTypeException |
||
| 97 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 98 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 99 | */ |
||
| 100 | protected function collectFields( |
||
| 101 | ObjectType $runtimeType, |
||
| 102 | SelectionSetNode $selectionSet, |
||
| 103 | &$fields, |
||
| 104 | &$visitedFragmentNames |
||
| 105 | ) { |
||
| 106 | foreach ($selectionSet->getSelections() as $selection) { |
||
| 107 | // Check if this Node should be included first |
||
| 108 | if (!$this->shouldIncludeNode($selection)) { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | // Collect fields |
||
| 112 | if ($selection instanceof FieldNode) { |
||
| 113 | $fieldName = $this->getFieldNameKey($selection); |
||
| 114 | if (!isset($runtimeType->getFields()[$selection->getNameValue()])) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | if (!isset($fields[$fieldName])) { |
||
| 119 | $fields[$fieldName] = []; |
||
| 120 | } |
||
| 121 | |||
| 122 | $fields[$fieldName][] = $selection; |
||
| 123 | } elseif ($selection instanceof InlineFragmentNode) { |
||
| 124 | if (!$this->doesFragmentConditionMatch($selection, $runtimeType)) { |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->collectFields($runtimeType, $selection->getSelectionSet(), $fields, $visitedFragmentNames); |
||
| 129 | } elseif ($selection instanceof FragmentSpreadNode) { |
||
| 130 | $fragmentName = $selection->getNameValue(); |
||
| 131 | |||
| 132 | if (!empty($visitedFragmentNames[$fragmentName])) { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | |||
| 136 | $visitedFragmentNames[$fragmentName] = true; |
||
| 137 | /** @var FragmentDefinitionNode $fragment */ |
||
| 138 | $fragment = $this->context->getFragments()[$fragmentName]; |
||
| 139 | $this->collectFields($runtimeType, $fragment->getSelectionSet(), $fields, $visitedFragmentNames); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $fields; |
||
| 144 | } |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * @param $node |
||
| 149 | * @return bool |
||
| 150 | * @throws InvalidTypeException |
||
| 151 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 152 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 153 | */ |
||
| 154 | private function shouldIncludeNode(NodeInterface $node): bool |
||
| 155 | { |
||
| 156 | |||
| 157 | $contextVariables = $this->context->getVariableValues(); |
||
| 158 | |||
| 159 | $skip = $this->valuesResolver->getDirectiveValues(GraphQLSkipDirective(), $node, $contextVariables); |
||
| 160 | |||
| 161 | if ($skip && $skip['if'] === true) { |
||
| 162 | return false; |
||
| 163 | } |
||
| 164 | |||
| 165 | $include = $this->valuesResolver->getDirectiveValues(GraphQLIncludeDirective(), $node, $contextVariables); |
||
| 166 | |||
| 167 | if ($include && $include['if'] === false) { |
||
| 168 | return false; |
||
| 169 | } |
||
| 170 | |||
| 171 | return true; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param FragmentDefinitionNode|InlineFragmentNode $fragment |
||
| 176 | * @param ObjectType $type |
||
| 177 | * @return bool |
||
| 178 | * @throws InvalidTypeException |
||
| 179 | */ |
||
| 180 | private function doesFragmentConditionMatch( |
||
| 181 | NodeInterface $fragment, |
||
| 182 | ObjectType $type |
||
| 183 | ): bool { |
||
| 184 | $typeConditionNode = $fragment->getTypeCondition(); |
||
| 185 | |||
| 186 | if (!$typeConditionNode) { |
||
| 187 | return true; |
||
| 188 | } |
||
| 189 | |||
| 190 | $conditionalType = typeFromAST($this->context->getSchema(), $typeConditionNode); |
||
| 191 | |||
| 192 | if ($conditionalType === $type) { |
||
| 193 | return true; |
||
| 194 | } |
||
| 195 | |||
| 196 | if ($conditionalType instanceof AbstractTypeInterface) { |
||
| 197 | return $this->context->getSchema()->isPossibleType($conditionalType, $type); |
||
| 198 | } |
||
| 199 | |||
| 200 | return false; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @TODO: consider to move this to FieldNode |
||
| 205 | * @param FieldNode $node |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | private function getFieldNameKey(FieldNode $node): string |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Implements the "Evaluating selection sets" section of the spec for "read" mode. |
||
| 215 | * @param ObjectType $objectType |
||
| 216 | * @param $rootValue |
||
| 217 | * @param $path |
||
| 218 | * @param $fields |
||
| 219 | * @return array |
||
| 220 | * @throws InvalidTypeException |
||
| 221 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 222 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 223 | * @throws \Throwable |
||
| 224 | */ |
||
| 225 | protected function executeFields( |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Implements the "Evaluating selection sets" section of the spec for "write" mode. |
||
| 252 | * |
||
| 253 | * @param ObjectType $objectType |
||
| 254 | * @param $rootValue |
||
| 255 | * @param $path |
||
| 256 | * @param $fields |
||
| 257 | * @return array |
||
| 258 | * @throws InvalidTypeException |
||
| 259 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 260 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 261 | * @throws \Throwable |
||
| 262 | */ |
||
| 263 | public function executeFieldsSerially( |
||
| 264 | ObjectType $objectType, |
||
| 265 | $rootValue, |
||
| 266 | $path, |
||
| 267 | $fields |
||
| 268 | ) { |
||
| 269 | //@TODO execute fields serially |
||
| 270 | $finalResults = []; |
||
| 271 | |||
| 272 | foreach ($fields as $fieldName => $fieldNodes) { |
||
| 273 | $fieldPath = $path; |
||
| 274 | $fieldPath[] = $fieldName; |
||
| 275 | |||
| 276 | $result = $this->resolveField( |
||
| 277 | $objectType, |
||
| 278 | $rootValue, |
||
| 279 | $fieldNodes, |
||
| 280 | $fieldPath |
||
| 281 | ); |
||
| 282 | |||
| 283 | $finalResults[$fieldName] = $result; |
||
| 284 | } |
||
| 285 | |||
| 286 | return $finalResults; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param Schema $schema |
||
| 291 | * @param ObjectType $parentType |
||
| 292 | * @param string $fieldName |
||
| 293 | * @return \Digia\GraphQL\Type\Definition\Field|null |
||
| 294 | * @throws InvalidTypeException |
||
| 295 | */ |
||
| 296 | public function getFieldDefinition( |
||
| 297 | Schema $schema, |
||
| 298 | ObjectType $parentType, |
||
| 299 | string $fieldName |
||
| 300 | ) { |
||
| 301 | $schemaMetaFieldDefinition = SchemaMetaFieldDefinition(); |
||
| 302 | $typeMetaFieldDefinition = TypeMetaFieldDefinition(); |
||
| 303 | $typeNameMetaFieldDefinition = TypeNameMetaFieldDefinition(); |
||
| 304 | |||
| 305 | if ($fieldName === $schemaMetaFieldDefinition->getName() && $schema->getQuery() === $parentType) { |
||
| 306 | return $schemaMetaFieldDefinition; |
||
| 307 | } |
||
| 308 | |||
| 309 | if ($fieldName === $typeMetaFieldDefinition->getName() && $schema->getQuery() === $parentType) { |
||
| 310 | return $typeMetaFieldDefinition; |
||
| 311 | } |
||
| 312 | |||
| 313 | if ($fieldName === $typeNameMetaFieldDefinition->getName()) { |
||
| 314 | return $typeNameMetaFieldDefinition; |
||
| 315 | } |
||
| 316 | |||
| 317 | $fields = $parentType->getFields(); |
||
| 318 | |||
| 319 | return $fields[$fieldName] ?? null; |
||
| 320 | } |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * @param ObjectType $parentType |
||
| 325 | * @param $rootValue |
||
| 326 | * @param $fieldNodes |
||
| 327 | * @param $path |
||
| 328 | * @return array|null|\Throwable |
||
| 329 | * @throws InvalidTypeException |
||
| 330 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 331 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 332 | * @throws \Throwable |
||
| 333 | */ |
||
| 334 | protected function resolveField( |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param array $fieldNodes |
||
| 375 | * @param FieldNode $fieldNode |
||
| 376 | * @param Field $field |
||
| 377 | * @param ObjectType $parentType |
||
| 378 | * @param $path |
||
| 379 | * @param ExecutionContext $context |
||
| 380 | * @return ResolveInfo |
||
| 381 | */ |
||
| 382 | private function buildResolveInfo( |
||
| 383 | array $fieldNodes, |
||
| 384 | FieldNode $fieldNode, |
||
| 385 | Field $field, |
||
| 386 | ObjectType $parentType, |
||
| 387 | $path, |
||
| 388 | ExecutionContext $context |
||
| 389 | ) { |
||
| 390 | return new ResolveInfo([ |
||
| 391 | 'fieldName' => $fieldNode->getNameValue(), |
||
| 392 | 'fieldNodes' => $fieldNodes, |
||
| 393 | 'returnType' => $field->getType(), |
||
| 394 | 'parentType' => $parentType, |
||
| 395 | 'path' => $path, |
||
| 396 | 'schema' => $context->getSchema(), |
||
| 397 | 'fragments' => $context->getFragments(), |
||
| 398 | 'rootValue' => $context->getRootValue(), |
||
| 399 | 'operation' => $context->getOperation(), |
||
| 400 | 'variableValues' => $context->getVariableValues(), |
||
| 401 | ]); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param Field $field |
||
| 406 | * @param ObjectType $objectType |
||
| 407 | * @param ExecutionContext $context |
||
| 408 | * @return callable|mixed|null |
||
| 409 | */ |
||
| 410 | private function determineResolveFunction( |
||
| 411 | Field $field, |
||
| 412 | ObjectType $objectType, |
||
| 413 | ExecutionContext $context |
||
| 414 | ) { |
||
| 415 | |||
| 416 | if ($field->hasResolve()) { |
||
| 417 | return $field->getResolve(); |
||
| 418 | } |
||
| 419 | |||
| 420 | if ($objectType->hasResolve()) { |
||
| 421 | return $objectType->getResolve(); |
||
| 422 | } |
||
| 423 | |||
| 424 | return $this->context->getFieldResolver() ?? self::$defaultFieldResolver; |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param TypeInterface $fieldType |
||
| 429 | * @param $fieldNodes |
||
| 430 | * @param ResolveInfo $info |
||
| 431 | * @param $path |
||
| 432 | * @param $result |
||
| 433 | * @return null |
||
| 434 | * @throws \Throwable |
||
| 435 | */ |
||
| 436 | public function completeValueCatchingError( |
||
| 437 | TypeInterface $fieldType, |
||
| 438 | $fieldNodes, |
||
| 439 | ResolveInfo $info, |
||
| 440 | $path, |
||
| 441 | &$result |
||
| 442 | ) { |
||
| 443 | if ($fieldType instanceof NonNullType) { |
||
| 444 | return $this->completeValueWithLocatedError( |
||
| 445 | $fieldType, |
||
| 446 | $fieldNodes, |
||
| 447 | $info, |
||
| 448 | $path, |
||
| 449 | $result |
||
| 450 | ); |
||
| 451 | } |
||
| 452 | |||
| 453 | try { |
||
| 454 | $completed = $this->completeValueWithLocatedError( |
||
| 455 | $fieldType, |
||
| 456 | $fieldNodes, |
||
| 457 | $info, |
||
| 458 | $path, |
||
| 459 | $result |
||
| 460 | ); |
||
| 461 | |||
| 462 | return $completed; |
||
| 463 | } catch (\Exception $ex) { |
||
| 464 | $this->context->addError(new ExecutionException($ex->getMessage())); |
||
| 465 | return null; |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param TypeInterface $fieldType |
||
| 471 | * @param $fieldNodes |
||
| 472 | * @param ResolveInfo $info |
||
| 473 | * @param $path |
||
| 474 | * @param $result |
||
| 475 | * @throws \Throwable |
||
| 476 | */ |
||
| 477 | public function completeValueWithLocatedError( |
||
| 478 | TypeInterface $fieldType, |
||
| 479 | $fieldNodes, |
||
| 480 | ResolveInfo $info, |
||
| 481 | $path, |
||
| 482 | $result |
||
| 483 | ) { |
||
| 484 | try { |
||
| 485 | $completed = $this->completeValue( |
||
| 486 | $fieldType, |
||
| 487 | $fieldNodes, |
||
| 488 | $info, |
||
| 489 | $path, |
||
| 490 | $result |
||
| 491 | ); |
||
| 492 | return $completed; |
||
| 493 | } catch (\Exception $ex) { |
||
| 494 | //@TODO throw located error |
||
| 495 | throw $ex; |
||
| 496 | } catch (\Throwable $ex) { |
||
| 497 | //@TODO throw located error |
||
| 498 | throw $ex; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param TypeInterface $returnType |
||
| 504 | * @param $fieldNodes |
||
| 505 | * @param ResolveInfo $info |
||
| 506 | * @param $path |
||
| 507 | * @param $result |
||
| 508 | * @return array|mixed |
||
| 509 | * @throws ExecutionException |
||
| 510 | * @throws \Throwable |
||
| 511 | */ |
||
| 512 | private function completeValue( |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param AbstractTypeInterface $returnType |
||
| 576 | * @param $fieldNodes |
||
| 577 | * @param ResolveInfo $info |
||
| 578 | * @param $path |
||
| 579 | * @param $result |
||
| 580 | * @return array |
||
| 581 | * @throws ExecutionException |
||
| 582 | * @throws InvalidTypeException |
||
| 583 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 584 | * @throws \Throwable |
||
| 585 | */ |
||
| 586 | private function completeAbstractValue( |
||
| 614 | ); |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @param $runtimeTypeOrName |
||
| 619 | * @param AbstractTypeInterface $returnType |
||
| 620 | * @param $fieldNodes |
||
| 621 | * @param ResolveInfo $info |
||
| 622 | * @param $result |
||
| 623 | * @return TypeInterface|null |
||
| 624 | * @throws ExecutionException |
||
| 625 | */ |
||
| 626 | private function ensureValidRuntimeType( |
||
| 627 | $runtimeTypeOrName, |
||
| 628 | AbstractTypeInterface $returnType, |
||
| 629 | $fieldNodes, |
||
| 630 | ResolveInfo $info, |
||
| 631 | &$result |
||
| 632 | ) { |
||
| 633 | $runtimeType = is_string($runtimeTypeOrName) |
||
| 634 | ? $this->context->getSchema()->getType($runtimeTypeOrName) |
||
| 635 | : $runtimeTypeOrName; |
||
| 636 | |||
| 637 | $runtimeTypeName = is_string($runtimeType) ?: $runtimeType->getName(); |
||
| 638 | $returnTypeName = $returnType->getName(); |
||
| 639 | |||
| 640 | if (!$runtimeType instanceof ObjectType) { |
||
| 641 | $parentTypeName = $info->getParentType()->getName(); |
||
| 642 | $fieldName = $info->getFieldName(); |
||
| 643 | |||
| 644 | throw new ExecutionException( |
||
| 645 | "Abstract type {$returnTypeName} must resolve to an Object type at runtime " . |
||
| 646 | "for field {$parentTypeName}.{$fieldName} with " . |
||
| 647 | 'value "' . $result . '", received "{$runtimeTypeName}".' |
||
| 648 | ); |
||
| 649 | } |
||
| 650 | |||
| 651 | if (!$this->context->getSchema()->isPossibleType($returnType, $runtimeType)) { |
||
| 652 | throw new ExecutionException( |
||
| 653 | "Runtime Object type \"{$runtimeTypeName}\" is not a possible type for \"{$returnTypeName}\"." |
||
| 654 | ); |
||
| 655 | } |
||
| 656 | |||
| 657 | if ($runtimeType !== $this->context->getSchema()->getType($runtimeType->getName())) { |
||
| 658 | throw new ExecutionException( |
||
| 659 | "Schema must contain unique named types but contains multiple types named \"{$runtimeTypeName}\". " . |
||
| 660 | "Make sure that `resolveType` function of abstract type \"{$returnTypeName}\" returns the same " . |
||
| 661 | "type instance as referenced anywhere else within the schema." |
||
| 662 | ); |
||
| 663 | } |
||
| 664 | |||
| 665 | return $runtimeType; |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @param $value |
||
| 670 | * @param $context |
||
| 671 | * @param ResolveInfo $info |
||
| 672 | * @param AbstractTypeInterface $abstractType |
||
| 673 | * @return TypeInterface|null |
||
| 674 | */ |
||
| 675 | private function defaultTypeResolver($value, $context, ResolveInfo $info, AbstractTypeInterface $abstractType) |
||
| 676 | { |
||
| 677 | $possibleTypes = $info->getSchema()->getPossibleTypes($abstractType); |
||
| 678 | |||
| 679 | foreach ($possibleTypes as $type) { |
||
| 680 | $isTypeOfResult = $type->isTypeOf($value, $context, $info); |
||
| 681 | |||
| 682 | if (null !== $isTypeOfResult) { |
||
| 683 | if ($isTypeOfResult) { |
||
| 684 | return $type; |
||
| 685 | } |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | return null; |
||
| 690 | } |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @param ListType $returnType |
||
| 694 | * @param $fieldNodes |
||
| 695 | * @param ResolveInfo $info |
||
| 696 | * @param $path |
||
| 697 | * @param $result |
||
| 698 | * @return array |
||
| 699 | * @throws \Throwable |
||
| 700 | */ |
||
| 701 | private function completeListValue( |
||
| 702 | ListType $returnType, |
||
| 703 | $fieldNodes, |
||
| 704 | ResolveInfo $info, |
||
| 705 | $path, |
||
| 706 | &$result |
||
| 707 | ) { |
||
| 708 | $itemType = $returnType->getOfType(); |
||
| 709 | |||
| 710 | $completedItems = []; |
||
| 711 | |||
| 712 | foreach ($result as $key => $item) { |
||
| 713 | $fieldPath = $path; |
||
| 714 | $fieldPath[] = $key; |
||
| 715 | $completedItem = $this->completeValueCatchingError($itemType, $fieldNodes, $info, $fieldPath, $item); |
||
| 716 | $completedItems[] = $completedItem; |
||
| 717 | } |
||
| 718 | |||
| 719 | return $completedItems; |
||
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @param LeafTypeInterface $returnType |
||
| 724 | * @param $result |
||
| 725 | * @return mixed |
||
| 726 | * @throws ExecutionException |
||
| 727 | */ |
||
| 728 | private function completeLeafValue(LeafTypeInterface $returnType, &$result) |
||
| 729 | { |
||
| 730 | $serializedResult = $returnType->serialize($result); |
||
| 731 | |||
| 732 | if ($serializedResult === null) { |
||
| 733 | throw new ExecutionException( |
||
| 734 | sprintf('Expected a value of type "%s" but received: %s', toString($returnType), toString($result)) |
||
| 735 | ); |
||
| 736 | } |
||
| 737 | |||
| 738 | return $serializedResult; |
||
| 739 | } |
||
| 740 | |||
| 741 | /** |
||
| 742 | * @param ObjectType $returnType |
||
| 743 | * @param $fieldNodes |
||
| 744 | * @param ResolveInfo $info |
||
| 745 | * @param $path |
||
| 746 | * @param $result |
||
| 747 | * @return array |
||
| 748 | * @throws ExecutionException |
||
| 749 | * @throws InvalidTypeException |
||
| 750 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 751 | * @throws \Throwable |
||
| 752 | */ |
||
| 753 | private function completeObjectValue( |
||
| 754 | ObjectType $returnType, |
||
| 755 | $fieldNodes, |
||
| 756 | ResolveInfo $info, |
||
| 757 | $path, |
||
| 758 | &$result |
||
| 759 | ) { |
||
| 760 | return $this->collectAndExecuteSubFields( |
||
| 761 | $returnType, |
||
| 762 | $fieldNodes, |
||
| 763 | $info, |
||
| 764 | $path, |
||
| 765 | $result |
||
| 766 | ); |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * @param Field $field |
||
| 771 | * @param FieldNode $fieldNode |
||
| 772 | * @param callable $resolveFunction |
||
| 773 | * @param $rootValue |
||
| 774 | * @param ExecutionContext $context |
||
| 775 | * @param ResolveInfo $info |
||
| 776 | * @return array|\Throwable |
||
| 777 | */ |
||
| 778 | private function resolveFieldValueOrError( |
||
| 779 | Field $field, |
||
| 780 | FieldNode $fieldNode, |
||
| 781 | ?callable $resolveFunction, |
||
| 782 | $rootValue, |
||
| 783 | ExecutionContext $context, |
||
| 784 | ResolveInfo $info |
||
| 785 | ) { |
||
| 786 | try { |
||
| 787 | $args = $this->valuesResolver->coerceArgumentValues($field, $fieldNode, $context->getVariableValues()); |
||
| 788 | |||
| 789 | return $resolveFunction($rootValue, $args, $context->getContextValue(), $info); |
||
| 790 | } catch (\Throwable $error) { |
||
| 791 | return $error; |
||
| 792 | } |
||
| 793 | } |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Try to resolve a field without any field resolver function. |
||
| 797 | * |
||
| 798 | * @param array|object $rootValue |
||
| 799 | * @param $args |
||
| 800 | * @param $context |
||
| 801 | * @param ResolveInfo $info |
||
| 802 | * @return mixed|null |
||
| 803 | */ |
||
| 804 | public static function defaultFieldResolver($rootValue, $args, $context, ResolveInfo $info) |
||
| 805 | { |
||
| 806 | $fieldName = $info->getFieldName(); |
||
| 807 | $property = null; |
||
| 808 | |||
| 809 | if (is_array($rootValue) && isset($rootValue[$fieldName])) { |
||
| 810 | $property = $rootValue[$fieldName]; |
||
| 811 | } |
||
| 812 | |||
| 813 | if (is_object($rootValue)) { |
||
| 814 | $getter = 'get' . ucfirst($fieldName); |
||
| 815 | if (method_exists($rootValue, $fieldName)) { |
||
| 816 | $property = $rootValue->{$getter}(); |
||
| 817 | } |
||
| 818 | |||
| 819 | if (property_exists($rootValue, $fieldName)) { |
||
| 820 | $property = $rootValue->{$fieldName}; |
||
| 821 | } |
||
| 822 | } |
||
| 823 | |||
| 824 | |||
| 825 | return $property instanceof \Closure ? $property($rootValue, $args, $context, $info) : $property; |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * @param ObjectType $returnType |
||
| 830 | * @param $fieldNodes |
||
| 831 | * @param ResolveInfo $info |
||
| 832 | * @param $path |
||
| 833 | * @param $result |
||
| 834 | * @return array |
||
| 835 | * @throws InvalidTypeException |
||
| 836 | * @throws \Digia\GraphQL\Error\ExecutionException |
||
| 837 | * @throws \Digia\GraphQL\Error\InvariantException |
||
| 838 | * @throws \Throwable |
||
| 839 | */ |
||
| 840 | private function collectAndExecuteSubFields( |
||
| 867 | } |
||
| 868 | } |
||
| 869 |
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.