Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Drupal\graphql_core\Plugin\Deriver\Fields;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\graphql\Utility\StringHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;

class EntityReferenceQueryDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;

/**
* The typed data manager service.
*
* @var \Drupal\Core\TypedData\TypedDataManagerInterface
*/
protected $typedDataManager;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $basePluginId) {
return new static(
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('typed_data_manager')
);
}

/**
* RawValueFieldItemDeriver constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* The entity field manager.
* @param \Drupal\Core\TypedData\TypedDataManagerInterface $typedDataManager
* The typed data manager service.
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityFieldManagerInterface $entityFieldManager,
TypedDataManagerInterface $typedDataManager
) {
$this->entityTypeManager = $entityTypeManager;
$this->entityFieldManager = $entityFieldManager;
$this->typedDataManager = $typedDataManager;
}

/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($basePluginDefinition) {
foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
$interfaces = class_implements($entityType->getClass());
if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
continue;
}

foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldDefinition) {
if ($fieldDefinition->getType() !== 'entity_reference' || !$targetTypeId = $fieldDefinition->getSetting('target_type')) {
continue;
}

$tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']);
$contexts = $fieldDefinition->getCacheContexts();
$maxAge = $fieldDefinition->getCacheMaxAge();

$targetType = $this->entityTypeManager->getDefinition($targetTypeId);
$fieldName = $fieldDefinition->getName();
$derivative = [
'parents' => [StringHelper::camelCase($entityTypeId)],
'name' => StringHelper::propCase('query', $fieldName),
'description' => $this->t('Query reference: @description', [
'@description' => $fieldDefinition->getDescription(),
]),
'field' => $fieldName,
'entity_key' => $targetType->getKey('id'),
'entity_type' => $targetTypeId,
'schema_cache_tags' => $tags,
'schema_cache_contexts' => $contexts,
'schema_cache_max_age' => $maxAge,
'response_cache_tags' => $tags,
'response_cache_contexts' => $contexts,
'response_cache_max_age' => $maxAge,
] + $basePluginDefinition;

/** @var \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface $definition */
$this->derivatives["$entityTypeId-$fieldName"] = $derivative;
}
}

return $this->derivatives;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataManagerInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @GraphQLField(
* id = "entity_query",
* secure = true,
* type = "EntityQueryResult!",
* type = "EntityQueryResult",
* arguments = {
* "filter" = "EntityQueryFilterInput",
* "sort" = "[EntityQuerySortInput]",
Expand Down Expand Up @@ -138,11 +138,14 @@ protected function getEntityType($value, array $args, ResolveContext $context, R
* @param \GraphQL\Type\Definition\ResolveInfo $info
* The resolve info object.
*
* @return \Drupal\Core\Entity\Query\QueryInterface
* @return \Drupal\Core\Entity\Query\QueryInterface|null
* The entity query object.
*/
protected function getQuery($value, array $args, ResolveContext $context, ResolveInfo $info) {
$query = $this->getBaseQuery($value, $args, $context, $info);
if (!$query = $this->getBaseQuery($value, $args, $context, $info)) {
return NULL;
}

$query->range($args['offset'], $args['limit']);

if (array_key_exists('revisions', $args)) {
Expand Down Expand Up @@ -172,7 +175,7 @@ protected function getQuery($value, array $args, ResolveContext $context, Resolv
* @param \GraphQL\Type\Definition\ResolveInfo $info
* The resolve info object.
*
* @return \Drupal\Core\Entity\Query\QueryInterface
* @return \Drupal\Core\Entity\Query\QueryInterface|null
* The entity query object.
*/
protected function getBaseQuery($value, array $args, ResolveContext $context, ResolveInfo $info) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\graphql_core\Plugin\GraphQL\Fields\EntityReference;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery\EntityQuery;
use GraphQL\Type\Definition\ResolveInfo;

/**
* @GraphQLField(
* id = "entity_reference_query",
* secure = true,
* type = "EntityQueryResult!",
* arguments = {
* "filter" = "EntityQueryFilterInput",
* "sort" = "[EntityQuerySortInput]",
* "offset" = {
* "type" = "Int",
* "default" = 0
* },
* "limit" = {
* "type" = "Int",
* "default" = 10
* },
* "revisions" = {
* "type" = "EntityQueryRevisionMode",
* "default" = "default"
* }
* },
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityReferenceQueryDeriver"
* )
*/
class EntityReferenceQuery extends EntityQuery {

/**
* {@inheritdoc}
*/
public function getBaseQuery($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof ContentEntityInterface) {
$query = parent::getBaseQuery($value, $args, $context, $info);

// Add the target field condition to the query.
$definition = $this->getPluginDefinition();
$key = $definition['entity_key'];
$field = $definition['field'];
$ids = array_map(function ($item) {
return $item['target_id'];
}, $value->get($field)->getValue());

if (empty($ids)) {
return NULL;
}

$query->condition($key, $ids);

return $query;
}
}

}