Skip to content

Commit 93f3eb7

Browse files
authored
Adding targeted entity interfaces. (#511)
1 parent 557bf37 commit 93f3eb7

17 files changed

Lines changed: 311 additions & 51 deletions

modules/graphql_core/src/Plugin/Deriver/Fields/EntityRenderedDeriver.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ public function getDerivativeDefinitions($basePluginDefinition) {
4949
'entity_type' => $id,
5050
] + $basePluginDefinition;
5151

52-
if ($type->isTranslatable()) {
53-
$derivative['arguments']['language'] = [
54-
'type' => 'LanguageId',
55-
];
56-
}
57-
5852
$this->derivatives["entity:$id"] = $derivative;
5953
}
6054
}

modules/graphql_core/src/Plugin/Deriver/Interfaces/EntityTypeDeriver.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Component\Plugin\Derivative\DeriverBase;
66
use Drupal\Core\Entity\ContentEntityTypeInterface;
7+
use Drupal\Core\Entity\EntityTypeInterface;
78
use Drupal\Core\Entity\EntityTypeManagerInterface;
89
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
910
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -46,19 +47,52 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager) {
4647
* {@inheritdoc}
4748
*/
4849
public function getDerivativeDefinitions($basePluginDefinition) {
49-
$this->derivatives = [];
5050
foreach ($this->entityTypeManager->getDefinitions() as $typeId => $type) {
51-
if ($type instanceof ContentEntityTypeInterface) {
52-
$this->derivatives[$typeId] = [
53-
'name' => StringHelper::camelCase($typeId),
54-
'description' => $this->t("The '@type' entity type.", [
55-
'@type' => $type->getLabel(),
56-
]),
57-
'type' => "entity:$typeId",
58-
] + $basePluginDefinition;
51+
if (!$type instanceof ContentEntityTypeInterface) {
52+
continue;
5953
}
54+
55+
$interfaces = isset($basePluginDefinition['interfaces']) ? $basePluginDefinition['interfaces'] : [];
56+
$interfaces = array_unique(array_merge($interfaces, $this->getInterfaces($type)));
57+
58+
$this->derivatives[$typeId] = [
59+
'name' => StringHelper::camelCase($typeId),
60+
'description' => $this->t("The '@type' entity type.", [
61+
'@type' => $type->getLabel(),
62+
]),
63+
'type' => "entity:$typeId",
64+
'interfaces' => $interfaces,
65+
] + $basePluginDefinition;
6066
}
67+
6168
return parent::getDerivativeDefinitions($basePluginDefinition);
6269
}
6370

71+
/**
72+
* Retrieve the interfaces that the entity type should implement.
73+
*
74+
* @param \Drupal\Core\Entity\EntityTypeInterface $type
75+
* The entity type to retrieve the interfaces for.
76+
*
77+
* @return array
78+
* The interfaces that this entity type should implement.
79+
*/
80+
protected function getInterfaces(EntityTypeInterface $type) {
81+
$pairs = [
82+
'\Drupal\Core\Entity\EntityDescriptionInterface' => 'EntityDescribable',
83+
'\Drupal\Core\Entity\EntityPublishedInterface' => 'EntityPublishable',
84+
'\Drupal\Core\Entity\RevisionableInterface' => 'EntityRevisionable',
85+
'\Drupal\user\EntityOwnerInterface' => 'EntityOwnable',
86+
];
87+
88+
$interfaces = [];
89+
foreach ($pairs as $dependency => $interface) {
90+
if ($type->entityClassImplements($dependency)) {
91+
$interfaces[] = $interface;
92+
}
93+
}
94+
95+
return $interfaces;
96+
}
97+
6498
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Blocks/BlocksByRegion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* id = "blocks_by_region",
2525
* secure = true,
2626
* name = "blocksByRegion",
27-
* type = "[Entity]",
27+
* type = "[entity:block]",
2828
* parents = {"InternalUrl"},
2929
* arguments = {
3030
* "region" = "String!"

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityChanged.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use DateTime;
99

1010
/**
11+
* TODO: Should we derive this for each entity type individually?
12+
*
1113
* @GraphQLField(
1214
* id = "entity_changed",
1315
* secure = true,

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityCreated.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use DateTime;
88

99
/**
10+
* TODO: Should we derive this for each entity type individually?
11+
*
1012
* @GraphQLField(
1113
* id = "entity_created",
1214
* secure = true,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4+
5+
use Drupal\Core\Entity\EntityDescriptionInterface;
6+
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
7+
use Youshido\GraphQL\Execution\ResolveInfo;
8+
9+
/**
10+
* @GraphQLField(
11+
* id = "entity_description",
12+
* secure = true,
13+
* name = "entityDescription",
14+
* type = "String",
15+
* parents = {"EntityDescribable"}
16+
* )
17+
*/
18+
class EntityDescription extends FieldPluginBase {
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function resolveValues($value, array $args, ResolveInfo $info) {
24+
if ($value instanceof EntityDescriptionInterface) {
25+
yield $value->getDescription();
26+
}
27+
}
28+
29+
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityOwner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* secure = true,
1313
* name = "entityOwner",
1414
* type = "entity:user",
15-
* parents = {"Entity"}
15+
* parents = {"EntityOwnable"}
1616
* )
1717
*/
1818
class EntityOwner extends FieldPluginBase {

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityPublished.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* secure = true,
1313
* name = "entityPublished",
1414
* type = "Boolean",
15-
* parents = {"Entity"}
15+
* parents = {"EntityPublishable"}
1616
* )
1717
*/
1818
class EntityPublished extends FieldPluginBase {

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityRendered.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ protected function buildArguments(PluggableSchemaBuilderInterface $schemaBuilder
9595
public function resolveValues($value, array $args, ResolveInfo $info) {
9696
if ($value instanceof ContentEntityInterface) {
9797
$mode = isset($args['mode']) ? $args['mode'] : 'full';
98-
$language = isset($args['language']) ? $args['language'] : $value->language()->getId();
99-
98+
$language = $value->language()->getId();
10099
$builder = $this->entityTypeManager->getViewBuilder($value->getEntityTypeId());
101100
$rendered = $builder->view($value, $mode, $language);
102101
yield $this->renderer->render($rendered);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery\EntityQuery;
7+
use Youshido\GraphQL\Execution\ResolveInfo;
8+
9+
/**
10+
* @GraphQLField(
11+
* id = "entity_revisions",
12+
* name = "entityRevisions",
13+
* secure = true,
14+
* parents = {"EntityRevisionable"},
15+
* type = "EntityQueryResult!",
16+
* arguments = {
17+
* "filter" = "EntityQueryFilterInput",
18+
* "sort" = "[EntityQuerySortInput]",
19+
* "offset" = {
20+
* "type" = "Int",
21+
* "default" = 0
22+
* },
23+
* "limit" = {
24+
* "type" = "Int",
25+
* "default" = 10
26+
* }
27+
* }
28+
* )
29+
*/
30+
class EntityRevisions extends EntityQuery {
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getBaseQuery($value, array $args, ResolveInfo $info) {
36+
if ($value instanceof EntityInterface) {
37+
$query = parent::getBaseQuery($value, $args, $info);
38+
39+
// Add the entity id as a filter condition.
40+
$key = $value->getEntityType()->getKey('id');
41+
$query->condition($key, $value->id());
42+
43+
// Mark the query as a revision query.
44+
return $this->applyRevisionsMode($query, 'all');
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)