1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Type; |
4
|
|
|
|
5
|
|
|
use Drupal\graphql\GraphQL\CacheableEdgeInterface; |
6
|
|
|
use Drupal\graphql\GraphQL\CacheableEdgeTrait; |
7
|
|
|
use Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginReferenceInterface; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginReferenceTrait; |
10
|
|
|
use Youshido\GraphQL\Config\Object\InterfaceTypeConfig; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
13
|
|
|
|
14
|
|
|
class InterfaceType extends AbstractInterfaceType implements TypeSystemPluginReferenceInterface, CacheableEdgeInterface { |
15
|
|
|
use TypeSystemPluginReferenceTrait; |
16
|
|
|
use CacheableEdgeTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* List of types implementing this interface. |
20
|
|
|
* |
21
|
|
|
* @var \Drupal\graphql\GraphQL\Type\ObjectType[] |
22
|
|
|
*/ |
23
|
|
|
protected $types; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function __construct(InterfacePluginBase $plugin, array $config = []) { |
29
|
|
|
$this->plugin = $plugin; |
30
|
|
|
$this->config = new InterfaceTypeConfig($config, $this, TRUE); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Registers a type that implements this interface. |
35
|
|
|
* |
36
|
|
|
* @param \Drupal\graphql\GraphQL\Type\ObjectType $type |
37
|
|
|
* The type to register on this interface. |
38
|
|
|
*/ |
39
|
|
|
public function registerType(ObjectType $type) { |
40
|
|
|
$this->types[$type->getName()] = $type; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function resolveType($object, ResolveInfo $info = NULL) { |
|
|
|
|
47
|
|
|
/** @var \Drupal\graphql\GraphQL\Type\ObjectType $type */ |
48
|
|
|
foreach ($this->types as $type) { |
49
|
|
|
if ($type->applies($object, $info)) { |
50
|
|
|
return $type; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
throw new \Exception(sprintf('Could not resolve type for interface %s.', $this->getName())); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function build($config) { |
61
|
|
|
// Nothing to do here. |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
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.