You are here

public function PropertyPath::resolve in GraphQL 8.4

Resolve the property path.

Parameters

string $path:

mixed $value:

string|null $type:

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata:

Return value

mixed

File

src/Plugin/GraphQL/DataProducer/TypedData/PropertyPath.php, line 52

Class

PropertyPath
Resolves a typed data value at a given property path.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\TypedData

Code

public function resolve($path, $value, $type, RefinableCacheableDependencyInterface $metadata) {
  if (!$value instanceof TypedDataInterface && !empty($type)) {
    $manager = $this
      ->getTypedDataManager();
    $definition = $manager
      ->createDataDefinition($type);
    $value = $manager
      ->create($definition, $value);
  }
  if (!$value instanceof TypedDataInterface) {
    throw new \BadMethodCallException('Could not derive typed data type.');
  }
  $bubbleable = new BubbleableMetadata();
  $fetcher = $this
    ->getDataFetcher();
  try {
    $output = $fetcher
      ->fetchDataByPropertyPath($value, $path, $bubbleable)
      ->getValue();
  } catch (MissingDataException $exception) {

    // There is no data at the given path.
  } catch (InvalidArgumentException $exception) {

    // The path is invalid for the source object.
  } finally {
    $metadata
      ->addCacheableDependency($bubbleable);
  }
  return $output ?? NULL;
}