Skip to content

Commit 557bf37

Browse files
authored
Allow unions to declare their types. (#510)
1 parent 7a503c3 commit 557bf37

5 files changed

Lines changed: 35 additions & 8 deletions

File tree

src/Plugin/GraphQL/Traits/ArgumentAwarePluginTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
trait ArgumentAwarePluginTrait {
1313
use TypedPluginTrait;
1414

15+
/**
16+
* {@inheritdoc}
17+
*/
18+
abstract public function getPluginDefinition();
19+
1520
/**
1621
* Build the arguments list.
1722
*

src/Plugin/GraphQL/Traits/FieldablePluginTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
trait FieldablePluginTrait {
1010

11+
/**
12+
* {@inheritdoc}
13+
*/
14+
abstract public function getPluginDefinition();
15+
1116
/**
1217
* Build the list of implicit and explicit fields attached to the object type.
1318
*

src/Plugin/GraphQL/Traits/NamedPluginTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
trait NamedPluginTrait {
88

9+
/**
10+
* {@inheritdoc}
11+
*/
12+
abstract public function getPluginDefinition();
13+
914
/**
1015
* Build the plugin's name.
1116
*

src/Plugin/GraphQL/Traits/TypedPluginTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
trait TypedPluginTrait {
1212

13+
/**
14+
* {@inheritdoc}
15+
*/
16+
abstract public function getPluginDefinition();
17+
1318
/**
1419
* Build the plugin type.
1520
*

src/Plugin/GraphQL/Unions/UnionTypePluginBase.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ abstract class UnionTypePluginBase extends PluginBase implements TypeSystemPlugi
2828
*/
2929
public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
3030
if (!isset($this->definition)) {
31-
$name = $this->buildName();
31+
$definition = $this->getPluginDefinition();
32+
$typeNames = $definition['types'];
33+
$unionName = $this->buildName();
3234

3335
$this->definition = new UnionType($this, $schemaBuilder, [
34-
'name' => $name,
36+
'name' => $unionName,
3537
'description' => $this->buildDescription(),
36-
'types' => $this->buildTypes($schemaBuilder, $name),
38+
'types' => $this->buildTypes($schemaBuilder, $unionName, $typeNames),
3739
]);
3840
}
3941

@@ -43,20 +45,25 @@ public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
4345
/**
4446
* Builds the list of types that are contained within this union type.
4547
*
48+
* Collects types that are explicitly referenced by this union type or that
49+
* are implicitly assigned by the type itself.
50+
*
4651
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
4752
* The schema manager.
48-
* @param $name
53+
* @param $unionName
4954
* The name of this plugin.
55+
* @param $typeNames
56+
* List of types that this union contains explicitly.
5057
*
5158
* @return \Drupal\graphql\GraphQL\Type\ObjectType[]
5259
* An array of types to add to this union type.
5360
*/
54-
protected function buildTypes(PluggableSchemaBuilderInterface $schemaBuilder, $name) {
61+
protected function buildTypes(PluggableSchemaBuilderInterface $schemaBuilder, $unionName, $typeNames) {
5562
/** @var \Drupal\graphql\GraphQL\Type\ObjectType[] $types */
56-
$types = array_map(function (TypeSystemPluginInterface $type) use ($schemaBuilder) {
63+
$types = array_map(function (TypeSystemPluginInterface $type) use ($typeNames, $schemaBuilder) {
5764
return $type->getDefinition($schemaBuilder);
58-
}, $schemaBuilder->find(function ($type) use ($name) {
59-
return in_array($name, $type['unions']);
65+
}, $schemaBuilder->find(function ($type) use ($typeNames, $unionName) {
66+
return in_array($unionName, $type['unions']) || in_array($type['name'], $typeNames);
6067
}, [
6168
GRAPHQL_TYPE_PLUGIN,
6269
]));

0 commit comments

Comments
 (0)