1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\InputTypes; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
6
|
|
|
use Drupal\Component\Plugin\PluginInspectionInterface; |
7
|
|
|
use Drupal\graphql\GraphQL\Type\InputObjectType; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
13
|
|
|
use Youshido\GraphQL\Config\Object\InputObjectTypeConfig; |
14
|
|
|
use Youshido\GraphQL\Field\Field; |
15
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base class for input type plugins. |
19
|
|
|
*/ |
20
|
|
|
abstract class InputTypePluginBase extends PluginBase implements TypeSystemPluginInterface { |
21
|
|
|
use CacheablePluginTrait; |
22
|
|
|
use NamedPluginTrait; |
23
|
|
|
use TypedPluginTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The type instance. |
27
|
|
|
* |
28
|
|
|
* @var \Drupal\graphql\GraphQL\Type\InputObjectType |
29
|
|
|
*/ |
30
|
|
|
protected $definition; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) { |
36
|
|
|
if (!isset($this->definition)) { |
37
|
|
|
$this->definition = new InputObjectType($this, [ |
38
|
|
|
'name' => $this->buildName(), |
39
|
|
|
'description' => $this->buildDescription(), |
40
|
|
|
'fields' => $this->buildFields($schemaBuilder), |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->definition; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Build the field list. |
49
|
|
|
* |
50
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder |
51
|
|
|
* Instance of the schema manager to resolve dependencies. |
52
|
|
|
* |
53
|
|
|
* @return \Youshido\GraphQL\Field\FieldInterface[] |
54
|
|
|
* The list of fields. |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
protected function buildFields(PluggableSchemaBuilderInterface $schemaBuilder) { |
|
|
|
|
57
|
|
|
if ($this instanceof PluginInspectionInterface) { |
|
|
|
|
58
|
|
|
$definition = $this->getPluginDefinition(); |
59
|
|
|
if (!$definition['fields']) { |
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$arguments = []; |
64
|
|
|
foreach ($definition['fields'] as $name => $argument) { |
65
|
|
|
$type = $this->buildFieldType($schemaBuilder, $argument); |
66
|
|
|
|
67
|
|
|
if ($type instanceof TypeInterface) { |
68
|
|
|
$config = [ |
69
|
|
|
'name' => $name, |
70
|
|
|
'type' => $type, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
if (is_array($argument) && isset($argument['default'])) { |
74
|
|
|
$config['defaultValue'] = $argument['default']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$arguments[$name] = new Field($config); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $arguments; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Build the field type. |
89
|
|
|
* |
90
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder |
91
|
|
|
* Instance of the schema manager to resolve dependencies. |
92
|
|
|
* @param array|string $field |
93
|
|
|
* The field definition array or type name. |
94
|
|
|
* |
95
|
|
|
* @return \Youshido\GraphQL\Type\TypeInterface |
96
|
|
|
* The type object. |
97
|
|
|
*/ |
98
|
|
|
protected function buildFieldType(PluggableSchemaBuilderInterface $schemaBuilder, $field) { |
99
|
|
|
if (is_array($field) && array_key_exists('data_type', $field) && $field['data_type']) { |
100
|
|
|
$types = array_map(function (TypeSystemPluginInterface $plugin) use ($schemaBuilder) { |
101
|
|
|
return $plugin->getDefinition($schemaBuilder); |
102
|
|
|
}, $schemaBuilder->find(function($definition) use ($field) { |
103
|
|
|
return array_key_exists('data_type', $definition) && $definition['data_type'] === $field['data_type']; |
104
|
|
|
}, [ |
105
|
|
|
GRAPHQL_INPUT_TYPE_PLUGIN, |
106
|
|
|
GRAPHQL_SCALAR_PLUGIN, |
107
|
|
|
])); |
108
|
|
|
|
109
|
|
|
$type = array_pop($types) ?: $schemaBuilder->findByName('String', [GRAPHQL_SCALAR_PLUGIN])->getDefinition($schemaBuilder); |
110
|
|
|
} |
111
|
|
View Code Duplication |
else { |
|
|
|
|
112
|
|
|
$typeInfo = is_array($field) ? $field['type'] : $field; |
113
|
|
|
|
114
|
|
|
$type = is_array($typeInfo) ? $this->buildEnumConfig($typeInfo, $field['enum_type_name']) : $schemaBuilder->findByName($typeInfo, [ |
115
|
|
|
GRAPHQL_INPUT_TYPE_PLUGIN, |
116
|
|
|
GRAPHQL_SCALAR_PLUGIN, |
117
|
|
|
GRAPHQL_ENUM_PLUGIN, |
118
|
|
|
])->getDefinition($schemaBuilder); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
if (isset($type) && $type instanceof TypeInterface) { |
|
|
|
|
122
|
|
|
$nullable = is_array($field) && array_key_exists('nullable', $field) && $field['nullable']; |
123
|
|
|
$multi = is_array($field) && array_key_exists('multi', $field) && $field['multi']; |
124
|
|
|
|
125
|
|
|
return $this->decorateType($type, $nullable, $multi); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return NULL; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.