1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\Interfaces; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
6
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
7
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait; |
8
|
|
|
use Drupal\graphql\Plugin\SchemaBuilderInterface; |
9
|
|
|
use Drupal\graphql\Plugin\TypePluginInterface; |
10
|
|
|
use Drupal\graphql\Plugin\TypePluginManager; |
11
|
|
|
use GraphQL\Type\Definition\InterfaceType; |
12
|
|
|
|
13
|
|
|
abstract class InterfacePluginBase extends PluginBase implements TypePluginInterface { |
14
|
|
|
use CacheablePluginTrait; |
15
|
|
|
use DescribablePluginTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) { |
21
|
|
|
return new InterfaceType([ |
22
|
|
|
'name' => $definition['name'], |
23
|
|
|
'description' => $definition['description'], |
24
|
|
|
'contexts' => $definition['contexts'], |
25
|
|
View Code Duplication |
'fields' => function () use ($builder, $definition) { |
|
|
|
|
26
|
|
|
$fields = $builder->getFields($definition['name']); |
27
|
|
|
|
28
|
|
|
if (!empty($definition['interfaces'])) { |
29
|
|
|
$inherited = array_map(function ($name) use ($builder) { |
30
|
|
|
return $builder->getFields($name); |
31
|
|
|
}, $definition['interfaces']); |
32
|
|
|
|
33
|
|
|
$inherited = call_user_func_array('array_merge', $inherited); |
34
|
|
|
return array_merge($inherited, $fields); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $fields; |
38
|
|
|
}, |
39
|
|
|
'resolveType' => function ($value, $context, $info) use ($builder, $definition) { |
40
|
|
|
return $builder->resolveType($definition['name'], $value, $context, $info); |
41
|
|
|
}, |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function getDefinition() { |
|
|
|
|
49
|
|
|
$definition = $this->getPluginDefinition(); |
50
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
'name' => $definition['name'], |
53
|
|
|
'description' => $this->buildDescription($definition), |
54
|
|
|
'interfaces' => $this->buildInterfaces($definition), |
55
|
|
|
'contexts' => $this->buildCacheContexts($definition), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Builds the list of interfaces inherited by this interface. |
61
|
|
|
* |
62
|
|
|
* @param array $definition |
63
|
|
|
* The plugin definition array. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
* The list of interfaces that this interface inherits from. |
67
|
|
|
*/ |
68
|
|
|
protected function buildInterfaces($definition) { |
69
|
|
|
return $definition['interfaces'] ?: []; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
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.