|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\Unions; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
|
6
|
|
|
use Drupal\graphql\GraphQL\Type\UnionType; |
|
7
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder; |
|
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
|
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait; |
|
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Base class for GraphQL union type plugins. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class UnionTypePluginBase extends PluginBase implements TypeSystemPluginInterface { |
|
16
|
|
|
use CacheablePluginTrait; |
|
17
|
|
|
use NamedPluginTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The type instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Drupal\graphql\GraphQL\Type\UnionType |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $definition; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getDefinition(PluggableSchemaBuilder $schemaBuilder) { |
|
30
|
|
|
if (!isset($this->definition)) { |
|
31
|
|
|
$name = $this->buildName(); |
|
32
|
|
|
|
|
33
|
|
|
$this->definition = new UnionType($this, [ |
|
34
|
|
|
'name' => $name, |
|
35
|
|
|
'description' => $this->buildDescription(), |
|
36
|
|
|
'types' => $this->buildTypes($schemaBuilder, $name), |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->definition; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Builds the list of types that are contained within this union type. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder $schemaBuilder |
|
47
|
|
|
* The schema manager. |
|
48
|
|
|
* @param $name |
|
49
|
|
|
* The name of this plugin. |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Drupal\graphql\GraphQL\Type\ObjectType[] |
|
52
|
|
|
* An array of types to add to this union type. |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function buildTypes(PluggableSchemaBuilder $schemaBuilder, $name) { |
|
55
|
|
|
/** @var \Drupal\graphql\GraphQL\Type\ObjectType[] $types */ |
|
56
|
|
|
$types = array_map(function (TypeSystemPluginInterface $type) use ($schemaBuilder) { |
|
57
|
|
|
return $type->getDefinition($schemaBuilder); |
|
58
|
|
|
}, $schemaBuilder->find(function ($type) use ($name) { |
|
59
|
|
|
return in_array($name, $type['unions']); |
|
60
|
|
|
}, [ |
|
61
|
|
|
GRAPHQL_TYPE_PLUGIN, |
|
62
|
|
|
])); |
|
63
|
|
|
|
|
64
|
|
|
return $types; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|