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\Type\ListType\ListType; |
8
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
9
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Methods for GraphQL plugins that have a type. |
13
|
|
|
*/ |
14
|
|
|
trait TypedPluginTrait { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Add information about cardinality and nullability. |
18
|
|
|
* |
19
|
|
|
* @param \Youshido\GraphQL\Type\TypeInterface $type |
20
|
|
|
* The initial type object. |
21
|
|
|
* @param bool $nullable |
22
|
|
|
* Indicates if the type can be null. |
23
|
|
|
* @param bool $multi |
24
|
|
|
* Indicates if the type is a list. |
25
|
|
|
* |
26
|
|
|
* @return \Youshido\GraphQL\Type\TypeInterface |
27
|
|
|
* The decorated type |
28
|
|
|
* |
29
|
|
|
* @deprecated |
30
|
|
|
* Will be removed before the 1.0 release. Use the type string syntax |
31
|
|
|
* (e.g. [Foo!]) instead. |
32
|
|
|
*/ |
33
|
|
|
public function decorateType(TypeInterface $type, $nullable, $multi) { |
34
|
|
|
if ($type) { |
35
|
|
|
if (!empty($multi)) { |
36
|
|
|
$type = new ListType($type); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (empty($nullable)) { |
40
|
|
|
$type = new NonNullType($type); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $type; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Build the plugin type. |
49
|
|
|
* |
50
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder |
51
|
|
|
* Instance of the schema manager to resolve dependencies. |
52
|
|
|
* |
53
|
|
|
* @return \Youshido\GraphQL\Type\TypeInterface |
54
|
|
|
* The type object. |
55
|
|
|
*/ |
56
|
|
|
protected function buildType(PluggableSchemaBuilderInterface $schemaBuilder) { |
57
|
|
|
if ($this instanceof PluginInspectionInterface) { |
|
|
|
|
58
|
|
|
$definition = $this->getPluginDefinition(); |
59
|
|
|
$type = $this->parseType($definition['type'], function ($type) use ($schemaBuilder) { |
60
|
|
|
return $schemaBuilder->findByName($type, [ |
61
|
|
|
GRAPHQL_SCALAR_PLUGIN, |
62
|
|
|
GRAPHQL_UNION_TYPE_PLUGIN, |
63
|
|
|
GRAPHQL_TYPE_PLUGIN, |
64
|
|
|
GRAPHQL_INTERFACE_PLUGIN, |
65
|
|
|
GRAPHQL_ENUM_PLUGIN, |
66
|
|
|
])->getDefinition($schemaBuilder); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
if ($type instanceof TypeInterface) { |
70
|
|
|
return $this->decorateType($type, $definition['nullable'], $definition['multi']); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return NULL; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Parses a type definition from a string and properly decorates it. |
79
|
|
|
* |
80
|
|
|
* Converts type strings (e.g. [Foo!]) to their object representations. |
81
|
|
|
* |
82
|
|
|
* @param string $type |
83
|
|
|
* The type string to parse. |
84
|
|
|
* @param callable $callback |
85
|
|
|
* A callback for retrieving the concrete type object from the extracted |
86
|
|
|
* type name. |
87
|
|
|
* |
88
|
|
|
* @return \Youshido\GraphQL\Type\TypeInterface |
89
|
|
|
* The extracted type with the type decorators applied. |
90
|
|
|
*/ |
91
|
|
|
protected function parseType($type, callable $callback) { |
92
|
|
|
$decorators = []; |
93
|
|
|
$unwrapped = $type; |
94
|
|
|
$matches = NULL; |
95
|
|
|
|
96
|
|
|
while (preg_match('/[\[\]!]/', $unwrapped) && preg_match_all('/^(\[?)(.*?)(\]?)(!*?)$/', $unwrapped, $matches)) { |
97
|
|
|
if ($unwrapped === $matches[2][0]) { |
98
|
|
|
throw new \InvalidArgumentException(sprintf("Invalid type declaration '%s'.", $type)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!empty($matches[4][0])) { |
102
|
|
|
array_push($decorators, [$this, 'nonNullType']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!empty($matches[1][0]) && !empty($matches[3][0])) { |
106
|
|
|
array_push($decorators, [$this, 'listType']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$unwrapped = $matches[2][0]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return array_reduce($decorators, function ($carry, $current) { |
113
|
|
|
return $current($carry); |
114
|
|
|
}, $callback($unwrapped)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Declares a type as non-nullable. |
119
|
|
|
* |
120
|
|
|
* @param \Youshido\GraphQL\Type\TypeInterface $type |
121
|
|
|
* The type to wrap. |
122
|
|
|
* |
123
|
|
|
* @return \Youshido\GraphQL\Type\NonNullType |
124
|
|
|
* The wrapped type. |
125
|
|
|
*/ |
126
|
|
|
protected function nonNullType(TypeInterface $type) { |
127
|
|
|
return new NonNullType($type); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Declares a type as a list. |
132
|
|
|
* |
133
|
|
|
* @param \Youshido\GraphQL\Type\TypeInterface $type |
134
|
|
|
* The type to wrap. |
135
|
|
|
* |
136
|
|
|
* @return \Youshido\GraphQL\Type\ListType\ListType |
137
|
|
|
* The wrapped type. |
138
|
|
|
*/ |
139
|
|
|
protected function listType(TypeInterface $type) { |
140
|
|
|
return new ListType($type); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: