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
|
|
|
trait TypedPluginTrait { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Build the plugin type. |
15
|
|
|
* |
16
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder |
17
|
|
|
* Instance of the schema manager to resolve dependencies. |
18
|
|
|
* |
19
|
|
|
* @return \Youshido\GraphQL\Type\TypeInterface |
20
|
|
|
* The type object. |
21
|
|
|
*/ |
22
|
|
|
protected function buildType(PluggableSchemaBuilderInterface $schemaBuilder) { |
23
|
|
|
if ($this instanceof PluginInspectionInterface) { |
|
|
|
|
24
|
|
|
$definition = $this->getPluginDefinition(); |
25
|
|
|
return $this->parseType($definition['type'], function ($type) use ($schemaBuilder) { |
26
|
|
|
return $schemaBuilder->findByDataTypeOrName($type, [ |
27
|
|
|
GRAPHQL_SCALAR_PLUGIN, |
28
|
|
|
GRAPHQL_UNION_TYPE_PLUGIN, |
29
|
|
|
GRAPHQL_TYPE_PLUGIN, |
30
|
|
|
GRAPHQL_INTERFACE_PLUGIN, |
31
|
|
|
GRAPHQL_ENUM_PLUGIN, |
32
|
|
|
])->getDefinition($schemaBuilder); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return NULL; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Parses a type definition from a string and properly decorates it. |
41
|
|
|
* |
42
|
|
|
* Converts type strings (e.g. [Foo!]) to their object representations. |
43
|
|
|
* |
44
|
|
|
* @param string $type |
45
|
|
|
* The type string to parse. |
46
|
|
|
* @param callable $callback |
47
|
|
|
* A callback for retrieving the concrete type object from the extracted |
48
|
|
|
* type name. |
49
|
|
|
* |
50
|
|
|
* @return \Youshido\GraphQL\Type\TypeInterface |
51
|
|
|
* The extracted type with the type decorators applied. |
52
|
|
|
*/ |
53
|
|
|
protected function parseType($type, callable $callback) { |
54
|
|
|
$decorators = []; |
55
|
|
|
$unwrapped = $type; |
56
|
|
|
$matches = NULL; |
57
|
|
|
|
58
|
|
|
while (preg_match('/[\[\]!]/', $unwrapped) && preg_match_all('/^(\[?)(.*?)(\]?)(!*?)$/', $unwrapped, $matches)) { |
59
|
|
|
if ($unwrapped === $matches[2][0] || empty($matches[1][0]) !== empty($matches[3][0])) { |
60
|
|
|
throw new \InvalidArgumentException(sprintf("Invalid type declaration '%s'.", $type)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!empty($matches[4][0])) { |
64
|
|
|
array_push($decorators, [$this, 'nonNullType']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!empty($matches[1][0]) && !empty($matches[3][0])) { |
68
|
|
|
array_push($decorators, [$this, 'listType']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$unwrapped = $matches[2][0]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return array_reduce($decorators, function ($carry, $current) { |
75
|
|
|
return $current($carry); |
76
|
|
|
}, $callback($unwrapped)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Declares a type as non-nullable. |
81
|
|
|
* |
82
|
|
|
* @param \Youshido\GraphQL\Type\TypeInterface $type |
83
|
|
|
* The type to wrap. |
84
|
|
|
* |
85
|
|
|
* @return \Youshido\GraphQL\Type\NonNullType |
86
|
|
|
* The wrapped type. |
87
|
|
|
*/ |
88
|
|
|
protected function nonNullType(TypeInterface $type) { |
89
|
|
|
return new NonNullType($type); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Declares a type as a list. |
94
|
|
|
* |
95
|
|
|
* @param \Youshido\GraphQL\Type\TypeInterface $type |
96
|
|
|
* The type to wrap. |
97
|
|
|
* |
98
|
|
|
* @return \Youshido\GraphQL\Type\ListType\ListType |
99
|
|
|
* The wrapped type. |
100
|
|
|
*/ |
101
|
|
|
protected function listType(TypeInterface $type) { |
102
|
|
|
return new ListType($type); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.