|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\ConfigurablePluginInterface; |
|
6
|
|
|
use Drupal\graphql\GraphQL\Schema\Schema; |
|
7
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
8
|
|
|
|
|
9
|
|
|
trait TypeSystemPluginReferenceTrait { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The associated type system plugin. |
|
13
|
|
|
* |
|
14
|
|
|
* @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $plugin; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Retrieves the corresponding plugin instance from the resolve info. |
|
20
|
|
|
* |
|
21
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
|
22
|
|
|
* The resolve info object. |
|
23
|
|
|
* |
|
24
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface|null |
|
25
|
|
|
* The corresponding plugin instance for this edge. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function getPluginFromResolveInfo(ResolveInfo $info) { |
|
28
|
|
|
if (is_array($this->plugin)) { |
|
29
|
|
|
$schema = isset($info) ? $info->getExecutionContext()->getSchema() : NULL; |
|
30
|
|
|
if (!$schema instanceof Schema) { |
|
31
|
|
|
return NULL; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$schemaPlugin = $schema->getSchemaPlugin(); |
|
35
|
|
|
if (!$schemaPlugin instanceof PluggableSchemaPluginInterface) { |
|
36
|
|
|
return NULL; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$typePlugin = $this->getPlugin($schemaPlugin->getSchemaBuilder()); |
|
40
|
|
|
if (!$typePlugin instanceof TypeSystemPluginInterface) { |
|
41
|
|
|
return NULL; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $typePlugin; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($this->plugin instanceof TypeSystemPluginInterface) { |
|
48
|
|
|
return $this->plugin; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return NULL; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getPlugin(PluggableSchemaBuilderInterface $schemaBuilder) { |
|
58
|
|
|
if (is_array($this->plugin)) { |
|
59
|
|
|
$this->plugin = $schemaBuilder->getInstance( |
|
60
|
|
|
$this->plugin['type'], |
|
61
|
|
|
$this->plugin['id'], |
|
62
|
|
|
$this->plugin['configuration'] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($this->plugin instanceof TypeSystemPluginInterface) { |
|
67
|
|
|
return $this->plugin; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return NULL; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function __sleep() { |
|
|
|
|
|
|
77
|
|
|
// Instead of serializing the referenced plugin, we just serialize the |
|
78
|
|
|
// plugin id and configuration. |
|
79
|
|
|
$this->plugin = [ |
|
|
|
|
|
|
80
|
|
|
'id' => $this->plugin->getPluginId(), |
|
81
|
|
|
'type' => $this->plugin->getPluginDefinition()['pluginType'], |
|
82
|
|
|
'configuration' => $this->plugin instanceof ConfigurablePluginInterface ? $this->plugin->getConfiguration() : [], |
|
|
|
|
|
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
return array_keys(get_object_vars($this)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
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.