Path.php in GraphQL 8.4
File
src/GraphQL/Resolver/Path.php
View source
<?php
namespace Drupal\graphql\GraphQL\Resolver;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataTrait;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\typed_data\DataFetcherTrait;
use GraphQL\Type\Definition\ResolveInfo;
class Path implements ResolverInterface {
use TypedDataTrait;
use DataFetcherTrait;
protected $type;
protected $path;
protected $value;
public function __construct($type, $path, ResolverInterface $value = NULL) {
$this->type = $type;
$this->path = $path;
$this->value = $value;
}
public function resolve($value, $args, ResolveContext $context, ResolveInfo $info, FieldContext $field) {
$value = $this->value ?? new ParentValue();
$value = $value
->resolve($value, $args, $context, $info, $field);
$metadata = new BubbleableMetadata();
$type = $this->type instanceof DataDefinitionInterface ? $this->type : DataDefinition::create($this->type);
$data = $this
->getTypedDataManager()
->create($type, $value);
$output = $this
->getDataFetcher()
->fetchDataByPropertyPath($data, $this->path, $metadata)
->getValue();
$context
->addCacheableDependency($metadata);
if ($output instanceof CacheableDependencyInterface) {
$context
->addCacheableDependency($output);
}
return $output;
}
}
Classes
Name |
Description |
Path |
Resolves a property path. |