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