GraphQL\Type\Definition\AbstractType::resolveType PHP Method

resolveType() public method

Resolves concrete ObjectType for given object value
public resolveType ( $objectValue, $context, ResolveInfo $info ) : mixed
$objectValue
$context
$info ResolveInfo
return mixed
    public function resolveType($objectValue, $context, ResolveInfo $info);

Usage Example

Example #1
0
 /**
  * Complete a value of an abstract type by determining the runtime object type
  * of that value, then complete the value for that type.
  *
  * @param ExecutionContext $exeContext
  * @param AbstractType $returnType
  * @param $fieldNodes
  * @param ResolveInfo $info
  * @param array $path
  * @param $result
  * @return mixed
  * @throws Error
  */
 private static function completeAbstractValue(ExecutionContext $exeContext, AbstractType $returnType, $fieldNodes, ResolveInfo $info, $path, &$result)
 {
     $runtimeType = $returnType->resolveType($result, $exeContext->contextValue, $info);
     if (null === $runtimeType) {
         $runtimeType = self::inferTypeOf($result, $exeContext->contextValue, $info, $returnType);
     }
     // If resolveType returns a string, we assume it's a ObjectType name.
     if (is_string($runtimeType)) {
         $runtimeType = $exeContext->schema->getType($runtimeType);
     }
     if (!$runtimeType instanceof ObjectType) {
         throw new Error("Abstract type {$returnType} must resolve to an Object type at runtime " . "for field {$info->parentType}.{$info->fieldName} with value \"" . print_r($result, true) . "\"," . "received \"{$runtimeType}\".", $fieldNodes);
     }
     if (!$exeContext->schema->isPossibleType($returnType, $runtimeType)) {
         throw new Error("Runtime Object type \"{$runtimeType}\" is not a possible type for \"{$returnType}\".", $fieldNodes);
     }
     return self::completeObjectValue($exeContext, $runtimeType, $fieldNodes, $info, $path, $result);
 }
AbstractType