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\TypeSystemPluginReferenceInterface; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginReferenceTrait; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Unions\UnionTypePluginBase; |
10
|
|
|
use Youshido\GraphQL\Config\Object\UnionTypeConfig; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
use Youshido\GraphQL\Type\Union\AbstractUnionType; |
13
|
|
|
|
14
|
|
|
class UnionType extends AbstractUnionType implements TypeSystemPluginReferenceInterface, CacheableEdgeInterface { |
15
|
|
|
use TypeSystemPluginReferenceTrait; |
16
|
|
|
use CacheableEdgeTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function __construct(UnionTypePluginBase $plugin, array $config = []) { |
22
|
|
|
$this->plugin = $plugin; |
23
|
|
|
$this->config = new UnionTypeConfig($config, $this, TRUE); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected function getConfigValue($key, $default = NULL) { |
30
|
|
|
return !empty($this->config) ? $this->config->get($key, $default) : $default; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function resolveType($object, ResolveInfo $info = NULL) { |
|
|
|
|
37
|
|
|
/** @var \Drupal\graphql\GraphQL\Type\ObjectType $type */ |
38
|
|
|
foreach ($this->getTypes() as $type) { |
39
|
|
|
if ($type->applies($object, $info)) { |
|
|
|
|
40
|
|
|
return $type; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
throw new \Exception(sprintf('Could not resolve type for union type %s.', $this->getName())); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getTypes() { |
51
|
|
|
return $this->getConfigValue('types', []); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
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.