1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\Unions; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\CacheableDependencyInterface; |
6
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaPluginInterface; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\PluginTrait; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Youshido\GraphQL\Config\Object\UnionTypeConfig; |
15
|
|
|
use Youshido\GraphQL\Type\Union\AbstractUnionType; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base class for GraphQL union type plugins. |
19
|
|
|
*/ |
20
|
|
|
abstract class UnionTypePluginBase extends AbstractUnionType implements TypeSystemPluginInterface { |
21
|
|
|
use PluginTrait; |
22
|
|
|
use CacheablePluginTrait; |
23
|
|
|
use NamedPluginTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition) { |
29
|
|
|
$this->constructPlugin($configuration, $pluginId, $pluginDefinition); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function buildConfig(PluggableSchemaPluginInterface $schemaManager) { |
|
|
|
|
36
|
|
|
$name = $this->buildName(); |
37
|
|
|
|
38
|
|
|
$this->config = new UnionTypeConfig([ |
39
|
|
|
'name' => $name, |
40
|
|
|
'description' => $this->buildDescription(), |
41
|
|
|
'types' => $this->buildTypes($schemaManager, $name), |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function build($config) { |
49
|
|
|
// May be overridden, but not required any more. |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Builds the list of types that are contained within this union type. |
54
|
|
|
* |
55
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaPluginInterface $schemaManager |
56
|
|
|
* The schema manager. |
57
|
|
|
* @param $name |
58
|
|
|
* The name of this plugin. |
59
|
|
|
* |
60
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[] |
61
|
|
|
* An array of types to add to this union type. |
62
|
|
|
*/ |
63
|
|
|
protected function buildTypes(PluggableSchemaPluginInterface $schemaManager, $name) { |
64
|
|
|
/** @var \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[] $types */ |
65
|
|
|
$types = $schemaManager->find(function ($type) use ($name) { |
66
|
|
|
return in_array($name, $type['unions']); |
67
|
|
|
}, [ |
68
|
|
|
GRAPHQL_TYPE_PLUGIN, |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$types = array_merge($this->getPluginDefinition()['parents'], $types); |
72
|
|
|
return array_unique($types); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Default implementation of "resolveType". |
77
|
|
|
* |
78
|
|
|
* Checks all implementing types and returns the matching type with the |
79
|
|
|
* highest weight. |
80
|
|
|
* |
81
|
|
|
* @param mixed $object |
82
|
|
|
* The current response tree value. |
83
|
|
|
* |
84
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase |
85
|
|
|
* The type object. |
86
|
|
|
*/ |
87
|
|
|
public function resolveType($object) { |
88
|
|
|
/** @var \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase $type */ |
89
|
|
|
foreach ($this->getTypes() as $type) { |
90
|
|
|
if ($type->applies($object)) { |
91
|
|
|
return $type; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return NULL; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Default implementation of "getTypes". |
100
|
|
|
* |
101
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[] |
102
|
|
|
* The types contained within this union type. |
103
|
|
|
*/ |
104
|
|
|
public function getTypes() { |
105
|
|
|
return $this->getConfig()->get('types', []); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.