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\Core\Cache\RefinableCacheableDependencyInterface; |
8
|
|
|
use Drupal\graphql\GraphQL\Field\Field; |
9
|
|
|
use Drupal\graphql\GraphQL\SecureFieldInterface; |
10
|
|
|
use Drupal\graphql\GraphQL\ValueWrapperInterface; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\ArgumentAwarePluginTrait; |
13
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
14
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait; |
15
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
16
|
|
|
use Youshido\GraphQL\Exception\ResolveException; |
17
|
|
|
use Youshido\GraphQL\Execution\DeferredResolver; |
18
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
19
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base class for field plugins. |
23
|
|
|
*/ |
24
|
|
|
abstract class FieldPluginBase extends PluginBase implements TypeSystemPluginInterface, SecureFieldInterface { |
25
|
|
|
use CacheablePluginTrait; |
26
|
|
|
use NamedPluginTrait; |
27
|
|
|
use ArgumentAwarePluginTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The field instance. |
31
|
|
|
* |
32
|
|
|
* @var \Drupal\graphql\GraphQL\Field\Field |
33
|
|
|
*/ |
34
|
|
|
protected $definition; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) { |
|
|
|
|
40
|
|
|
if (!isset($this->definition)) { |
41
|
|
|
$definition = $this->getPluginDefinition(); |
42
|
|
|
|
43
|
|
|
$config = [ |
44
|
|
|
'name' => $this->buildName(), |
45
|
|
|
'description' => $this->buildDescription(), |
46
|
|
|
'type' => $this->buildType($schemaBuilder), |
47
|
|
|
'args' => $this->buildArguments($schemaBuilder), |
48
|
|
|
'isDeprecated' => !empty($definition['deprecated']), |
49
|
|
|
'deprecationReason' => !empty($definition['deprecated']) ? !empty($definition['deprecated']) : '', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$this->definition = new Field($this, $schemaBuilder, $config); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->definition; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function isSecure() { |
62
|
|
|
return isset($this->getPluginDefinition()['secure']) && $this->getPluginDefinition()['secure']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function resolve($value, array $args, ResolveInfo $info) { |
69
|
|
|
// If not resolving in a trusted environment, check if the field is secure. |
70
|
|
|
$container = $info->getExecutionContext()->getContainer(); |
71
|
|
|
if ($container->has('secure') && !$container->get('secure') && !$this->isSecure()) { |
72
|
|
|
throw new ResolveException(sprintf("Unable to resolve insecure field '%s' (%s).", $info->getField()->getName(), get_class($this))); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->resolveDeferred([$this, 'resolveValues'], $value, $args, $info); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
protected function resolveDeferred(callable $callback, $value, array $args, ResolveInfo $info) { |
82
|
|
|
$result = $callback($value, $args, $info); |
83
|
|
|
if (is_callable($result)) { |
84
|
|
|
return new DeferredResolver(function () use ($result, $args, $info, $value) { |
85
|
|
|
return $this->resolveDeferred($result, $value, $args, $info); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Extract the result array. |
90
|
|
|
$result = iterator_to_array($result); |
91
|
|
|
|
92
|
|
|
// Commit the cache dependencies into the processor's cache collector. |
93
|
|
|
if ($dependencies = $this->getCacheDependencies($result, $value, $args, $info)) { |
94
|
|
|
$container = $info->getExecutionContext()->getContainer(); |
95
|
|
|
if ($container->has('metadata') && $metadata = $container->get('metadata')) { |
96
|
|
|
if ($metadata instanceof RefinableCacheableDependencyInterface) { |
|
|
|
|
97
|
|
|
array_walk($dependencies, [$metadata, 'addCacheableDependency']); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->unwrapResult($result, $info); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Unwrap the resolved values. |
107
|
|
|
* |
108
|
|
|
* @param array $result |
109
|
|
|
* The resolved values. |
110
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
111
|
|
|
* The resolve info object. |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
* The extracted values (an array of values in case this is a list, an |
115
|
|
|
* arbitrary value if it isn't). |
116
|
|
|
*/ |
117
|
|
|
protected function unwrapResult($result, ResolveInfo $info) { |
118
|
|
|
$result = array_map(function ($item) { |
119
|
|
|
return $item instanceof ValueWrapperInterface ? $item->getValue() : $item; |
120
|
|
|
}, $result); |
121
|
|
|
|
122
|
|
|
// If this is a list, return the result as an array. |
123
|
|
|
$type = $info->getReturnType()->getNullableType(); |
124
|
|
|
if ($type instanceof ListType) { |
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return !empty($result) ? reset($result) : NULL; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieve the list of cache dependencies for a given value and arguments. |
133
|
|
|
* |
134
|
|
|
* @param array $result |
135
|
|
|
* The result of the field. |
136
|
|
|
* @param mixed $parent |
137
|
|
|
* The parent value. |
138
|
|
|
* @param array $args |
139
|
|
|
* The arguments passed to the field. |
140
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
141
|
|
|
* The resolve info object. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
* A list of cacheable dependencies. |
145
|
|
|
*/ |
146
|
|
|
protected function getCacheDependencies(array $result, $parent, array $args, ResolveInfo $info) { |
147
|
|
|
return array_filter($result, function ($item) { |
148
|
|
|
return $item instanceof CacheableDependencyInterface; |
|
|
|
|
149
|
|
|
}); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Retrieve the list of field values. |
154
|
|
|
* |
155
|
|
|
* Always returns a list of field values. Even for single value fields. |
156
|
|
|
* Single/multi field handling is responsibility of the base class. |
157
|
|
|
* |
158
|
|
|
* @param mixed $value |
159
|
|
|
* The current object value. |
160
|
|
|
* @param array $args |
161
|
|
|
* Field arguments. |
162
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
163
|
|
|
* The resolve info object. |
164
|
|
|
* |
165
|
|
|
* @return \Generator |
166
|
|
|
* The value generator. |
167
|
|
|
*/ |
168
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
169
|
|
|
// Allow overriding this class without having to declare this method. |
170
|
|
|
yield NULL; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
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.