1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\Fields; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
6
|
|
|
use Drupal\Core\Cache\CacheableDependencyInterface; |
7
|
|
|
use Drupal\graphql\GraphQL\ValueWrapperInterface; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\ArgumentAwarePluginTrait; |
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\DeprecatablePluginTrait; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait; |
13
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait; |
14
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
15
|
|
|
use GraphQL\Deferred; |
16
|
|
|
use GraphQL\Type\Definition\ListOfType; |
17
|
|
|
use GraphQL\Type\Definition\NonNull; |
18
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
19
|
|
|
|
20
|
|
|
abstract class FieldPluginBase extends PluginBase implements TypeSystemPluginInterface { |
21
|
|
|
use CacheablePluginTrait; |
22
|
|
|
use DescribablePluginTrait; |
23
|
|
|
use TypedPluginTrait; |
24
|
|
|
use ArgumentAwarePluginTrait; |
25
|
|
|
use DeprecatablePluginTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public static function createInstance(PluggableSchemaBuilder $builder, $definition, $id) { |
31
|
|
|
return [ |
32
|
|
|
'args' => $builder->resolveArgs($definition['args']), |
33
|
|
|
'resolve' => function ($value, array $args, $context, ResolveInfo $info) use ($builder, $id) { |
34
|
|
|
$instance = $builder->getPluginInstance(GRAPHQL_FIELD_PLUGIN, $id); |
35
|
|
|
return $instance->resolve($value, $args, $context, $info); |
36
|
|
|
}, |
37
|
|
|
] + $definition; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getDefinition() { |
44
|
|
|
$definition = $this->getPluginDefinition(); |
45
|
|
|
|
46
|
|
|
return [ |
47
|
|
|
'type' => $this->buildType($definition), |
48
|
|
|
'description' => $this->buildDescription($definition), |
49
|
|
|
'args' => $this->buildArguments($definition), |
50
|
|
|
'deprecationReason' => $this->buildDeprecationReason($definition), |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function resolve($value, array $args, $context, ResolveInfo $info) { |
58
|
|
|
// If not resolving in a trusted environment, check if the field is secure. |
59
|
|
|
if (isset($context['secure']) && empty($context['secure'])) { |
60
|
|
|
$definition = $this->getPluginDefinition(); |
61
|
|
|
if (empty($definition['secure'])) { |
62
|
|
|
throw new \Exception(sprintf("Unable to resolve insecure field '%s'.", $info->fieldName)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->resolveDeferred([$this, 'resolveValues'], $value, $args, $info); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected function resolveDeferred(callable $callback, $value, array $args, ResolveInfo $info) { |
73
|
|
|
$result = $callback($value, $args, $info); |
74
|
|
|
if (is_callable($result)) { |
75
|
|
|
return new Deferred(function () use ($result, $args, $info, $value) { |
76
|
|
|
return $this->resolveDeferred($result, $value, $args, $info); |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Extract the result array. |
81
|
|
|
$result = iterator_to_array($result); |
82
|
|
|
|
83
|
|
|
// TODO: Extract cache dependencies. |
84
|
|
|
|
85
|
|
|
return $this->unwrapResult($result, $info); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Unwrap the resolved values. |
90
|
|
|
* |
91
|
|
|
* @param array $result |
92
|
|
|
* The resolved values. |
93
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
94
|
|
|
* The resolve info object. |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
* The extracted values (an array of values in case this is a list, an |
98
|
|
|
* arbitrary value if it isn't). |
99
|
|
|
*/ |
100
|
|
|
protected function unwrapResult($result, ResolveInfo $info) { |
101
|
|
|
$result = array_map(function ($item) { |
102
|
|
|
return $item instanceof ValueWrapperInterface ? $item->getValue() : $item; |
103
|
|
|
}, $result); |
104
|
|
|
|
105
|
|
|
// If this is a list, return the result as an array. |
106
|
|
|
$type = $info->returnType; |
107
|
|
|
if ($type instanceof ListOfType || ($type instanceof NonNull && $type->getWrappedType() instanceof ListOfType)) { |
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return !empty($result) ? reset($result) : NULL; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retrieve the list of cache dependencies for a given value and arguments. |
116
|
|
|
* |
117
|
|
|
* @param array $result |
118
|
|
|
* The result of the field. |
119
|
|
|
* @param mixed $parent |
120
|
|
|
* The parent value. |
121
|
|
|
* @param array $args |
122
|
|
|
* The arguments passed to the field. |
123
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
124
|
|
|
* The resolve info object. |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
* A list of cacheable dependencies. |
128
|
|
|
*/ |
129
|
|
|
protected function getCacheDependencies(array $result, $parent, array $args, ResolveInfo $info) { |
130
|
|
|
return array_filter($result, function ($item) { |
131
|
|
|
return $item instanceof CacheableDependencyInterface; |
|
|
|
|
132
|
|
|
}); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Retrieve the list of field values. |
137
|
|
|
* |
138
|
|
|
* Always returns a list of field values. Even for single value fields. |
139
|
|
|
* Single/multi field handling is responsibility of the base class. |
140
|
|
|
* |
141
|
|
|
* @param mixed $value |
142
|
|
|
* The current object value. |
143
|
|
|
* @param array $args |
144
|
|
|
* Field arguments. |
145
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
146
|
|
|
* The resolve info object. |
147
|
|
|
* |
148
|
|
|
* @return \Generator |
149
|
|
|
* The value generator. |
150
|
|
|
*/ |
151
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
152
|
|
|
// Allow overriding this class without having to declare this method. |
153
|
|
|
yield NULL; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
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.