1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\InputTypes; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
6
|
|
|
use Drupal\graphql\Plugin\SchemaBuilder; |
7
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait; |
10
|
|
|
use Drupal\graphql\Plugin\TypePluginInterface; |
11
|
|
|
use Drupal\graphql\Plugin\TypePluginManager; |
12
|
|
|
use Drupal\graphql\Utility\StringHelper; |
13
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
14
|
|
|
|
15
|
|
|
abstract class InputTypePluginBase extends PluginBase implements TypePluginInterface { |
16
|
|
|
use CacheablePluginTrait; |
17
|
|
|
use DescribablePluginTrait; |
18
|
|
|
use TypedPluginTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public static function createInstance(SchemaBuilder $builder, TypePluginManager $manager, $definition, $id) { |
24
|
|
|
return new InputObjectType([ |
25
|
|
|
'name' => $definition['name'], |
26
|
|
|
'description' => $definition['description'], |
27
|
|
|
'fields' => function () use ($builder, $definition) { |
28
|
|
|
return $builder->processArguments($definition['fields']); |
29
|
|
|
}, |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function getDefinition() { |
|
|
|
|
37
|
|
|
$definition = $this->getPluginDefinition(); |
38
|
|
|
|
39
|
|
|
return [ |
40
|
|
|
'name' => $definition['name'], |
41
|
|
|
'description' => $this->buildDescription($definition), |
42
|
|
|
'fields' => $this->buildFields($definition), |
43
|
|
|
] + $this->buildCacheMetadata($definition); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $definition |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function buildFields($definition) { |
52
|
|
|
return array_map(function ($field) use ($definition) { |
53
|
|
|
return [ |
54
|
|
|
'type' => $this->buildFieldType($field, $definition), |
55
|
|
|
'description' => $this->buildFieldDescription($field, $definition), |
56
|
|
|
'default' => $this->buildFieldDefault($field, $definition), |
57
|
|
|
]; |
58
|
|
|
}, $definition['fields']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $field |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function buildFieldType($field) { |
67
|
|
|
$type = is_array($field) ? $field['type'] : $field; |
68
|
|
|
return StringHelper::parseType($type); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $field |
73
|
|
|
* @param $definition |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function buildFieldDescription($field, $definition) { |
78
|
|
|
return (string) (isset($field['description']) ? $field['description'] : ''); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $field |
83
|
|
|
* @param $definition |
84
|
|
|
* |
85
|
|
|
* @return null |
86
|
|
|
*/ |
87
|
|
|
protected function buildFieldDefault($field, $definition) { |
88
|
|
|
return isset($field['default']) ? $field['default'] : NULL; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.