1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\Traits; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginInspectionInterface; |
6
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface; |
7
|
|
|
use Youshido\GraphQL\Field\InputField; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Methods for argument aware plugins. |
11
|
|
|
*/ |
12
|
|
|
trait ArgumentAwarePluginTrait { |
13
|
|
|
use TypedPluginTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Build the arguments list. |
17
|
|
|
* |
18
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder |
19
|
|
|
* Instance of the schema manager to resolve dependencies. |
20
|
|
|
* |
21
|
|
|
* @return \Youshido\GraphQL\Field\InputFieldInterface[] |
22
|
|
|
* The list of arguments. |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
protected function buildArguments(PluggableSchemaBuilderInterface $schemaBuilder) { |
|
|
|
|
25
|
|
|
$arguments = []; |
26
|
|
|
|
27
|
|
|
if ($this instanceof PluginInspectionInterface) { |
|
|
|
|
28
|
|
|
$definition = $this->getPluginDefinition(); |
29
|
|
|
|
30
|
|
|
foreach ($definition['arguments'] as $name => $argument) { |
31
|
|
|
$type = $this->buildArgumentType($schemaBuilder, $argument); |
32
|
|
|
$config = [ |
33
|
|
|
'name' => $name, |
34
|
|
|
'type' => $type, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
if (is_array($argument) && isset($argument['default'])) { |
38
|
|
|
$config['defaultValue'] = $argument['default']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$arguments[$name] = new InputField($config); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $arguments; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Build the argument type. |
50
|
|
|
* |
51
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder |
52
|
|
|
* Instance of the schema manager to resolve dependencies. |
53
|
|
|
* @param array|string $argument |
54
|
|
|
* The argument definition array or type name. |
55
|
|
|
* |
56
|
|
|
* @return \Youshido\GraphQL\Type\TypeInterface |
57
|
|
|
* The type object. |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
protected function buildArgumentType(PluggableSchemaBuilderInterface $schemaBuilder, $argument) { |
|
|
|
|
60
|
|
|
$type = is_array($argument) ? $argument['type'] : $argument; |
61
|
|
|
return $this->parseType($type, function ($type) use ($schemaBuilder) { |
62
|
|
|
return $schemaBuilder->findByDataTypeOrName($type, [ |
63
|
|
|
GRAPHQL_INPUT_TYPE_PLUGIN, |
64
|
|
|
GRAPHQL_SCALAR_PLUGIN, |
65
|
|
|
GRAPHQL_ENUM_PLUGIN, |
66
|
|
|
])->getDefinition($schemaBuilder); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
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.