1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\Interfaces; |
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\SchemaBuilder; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\FieldablePluginTrait; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\PluginTrait; |
13
|
|
|
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase; |
14
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
use Youshido\GraphQL\Config\Object\InterfaceTypeConfig; |
17
|
|
|
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Base class for GraphQL interface plugins. |
21
|
|
|
*/ |
22
|
|
|
abstract class InterfacePluginBase extends AbstractInterfaceType implements TypeSystemPluginInterface { |
23
|
|
|
use PluginTrait; |
24
|
|
|
use CacheablePluginTrait; |
25
|
|
|
use NamedPluginTrait; |
26
|
|
|
use FieldablePluginTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The list of types that implement this interface. |
30
|
|
|
* |
31
|
|
|
* @var \Youshido\GraphQL\Type\Object\AbstractObjectType[] |
32
|
|
|
*/ |
33
|
|
|
protected $types = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition) { |
39
|
|
|
$this->constructPlugin($configuration, $pluginId, $pluginDefinition); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function buildConfig(SchemaBuilder $schemaManager) { |
|
|
|
|
46
|
|
|
$this->config = new InterfaceTypeConfig([ |
47
|
|
|
'name' => $this->buildName(), |
48
|
|
|
'description' => $this->buildDescription(), |
49
|
|
|
'fields' => $this->buildFields($schemaManager), |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function build($config) { |
57
|
|
|
// May be overridden, but not required any more. |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Builds the list of types that are contained within this union type. |
62
|
|
|
* |
63
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase $type |
64
|
|
|
* The name of this plugin. |
65
|
|
|
*/ |
66
|
|
|
public function addType(TypePluginBase $type) { |
67
|
|
|
$this->types[] = $type; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Default implementation of "resolveType". |
72
|
|
|
* |
73
|
|
|
* Checks all implementing types and returns the matching type with the |
74
|
|
|
* highest weight. |
75
|
|
|
* |
76
|
|
|
* @param mixed $object |
77
|
|
|
* The current response tree value. |
78
|
|
|
* |
79
|
|
|
* @return \Youshido\GraphQL\Type\Object\AbstractObjectType|null |
80
|
|
|
* The type object. |
81
|
|
|
*/ |
82
|
|
|
public function resolveType($object) { |
83
|
|
|
foreach ($this->types as $type) { |
84
|
|
|
if ($type instanceof TypePluginBase && $type->applies($object)) { |
85
|
|
|
return $type; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return NULL; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
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.