1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\Plugin\GraphQL\Fields; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\PluginBase; |
6
|
|
|
use Drupal\Component\Render\MarkupInterface; |
7
|
|
|
use Drupal\Core\Cache\CacheableDependencyInterface; |
8
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
9
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
10
|
|
|
use Drupal\graphql\GraphQL\ValueWrapperInterface; |
11
|
|
|
use Drupal\graphql\Plugin\FieldPluginInterface; |
12
|
|
|
use Drupal\graphql\Plugin\FieldPluginManager; |
13
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\ArgumentAwarePluginTrait; |
14
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait; |
15
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\DeprecatablePluginTrait; |
16
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait; |
17
|
|
|
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait; |
18
|
|
|
use Drupal\graphql\Plugin\LanguageNegotiation\LanguageNegotiationGraphQL; |
19
|
|
|
use Drupal\graphql\Plugin\SchemaBuilderInterface; |
20
|
|
|
use GraphQL\Deferred; |
21
|
|
|
use GraphQL\Type\Definition\ListOfType; |
22
|
|
|
use GraphQL\Type\Definition\NonNull; |
23
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
24
|
|
|
|
25
|
|
|
abstract class FieldPluginBase extends PluginBase implements FieldPluginInterface { |
26
|
|
|
use CacheablePluginTrait; |
27
|
|
|
use DescribablePluginTrait; |
28
|
|
|
use TypedPluginTrait; |
29
|
|
|
use ArgumentAwarePluginTrait; |
30
|
|
|
use DeprecatablePluginTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The language context service. |
34
|
|
|
* |
35
|
|
|
* @var \Drupal\graphql\GraphQLLanguageContext |
36
|
|
|
*/ |
37
|
|
|
protected $languageContext; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public static function createInstance(SchemaBuilderInterface $builder, FieldPluginManager $manager, $definition, $id) { |
43
|
|
|
return [ |
44
|
|
|
'description' => $definition['description'], |
45
|
|
|
'contexts' => $definition['contexts'], |
46
|
|
|
'deprecationReason' => $definition['deprecationReason'], |
47
|
|
|
'type' => $builder->processType($definition['type']), |
48
|
|
|
'args' => $builder->processArguments($definition['args']), |
49
|
|
|
'resolve' => function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($manager, $id) { |
50
|
|
|
$instance = $manager->getInstance(['id' => $id]); |
51
|
|
|
return $instance->resolve($value, $args, $context, $info); |
52
|
|
|
}, |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the language context instance. |
58
|
|
|
* |
59
|
|
|
* @return \Drupal\graphql\GraphQLLanguageContext |
60
|
|
|
* The language context service. |
61
|
|
|
*/ |
62
|
|
|
protected function getLanguageContext() { |
63
|
|
|
if (!isset($this->languageContext)) { |
64
|
|
|
$this->languageContext = \Drupal::service('graphql.language_context'); |
65
|
|
|
} |
66
|
|
|
return $this->languageContext; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function getDefinition() { |
|
|
|
|
73
|
|
|
$definition = $this->getPluginDefinition(); |
74
|
|
|
|
75
|
|
|
return [ |
76
|
|
|
'type' => $this->buildType($definition), |
77
|
|
|
'description' => $this->buildDescription($definition), |
78
|
|
|
'args' => $this->buildArguments($definition), |
79
|
|
|
'deprecationReason' => $this->buildDeprecationReason($definition), |
80
|
|
|
'contexts' => $this->buildCacheContexts($definition), |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) { |
88
|
|
|
$definition = $this->getPluginDefinition(); |
89
|
|
|
|
90
|
|
|
// If not resolving in a trusted environment, check if the field is secure. |
91
|
|
|
if (!$context->getGlobal('development', FALSE) && !$context->getGlobal('bypass field security', FALSE)) { |
92
|
|
|
if (empty($definition['secure'])) { |
93
|
|
|
throw new \Exception(sprintf("Unable to resolve insecure field '%s'.", $info->fieldName)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($definition['contextual_arguments'] as $argument) { |
98
|
|
|
if (array_key_exists($argument, $args) && !is_null($args[$argument])) { |
99
|
|
|
$context->setContext($argument, $args[$argument], $info); |
100
|
|
|
} |
101
|
|
|
else { |
102
|
|
|
$args[$argument] = $context->getContext($argument, $info); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return \Drupal::service('graphql.language_context') |
107
|
|
|
->executeInLanguageContext(function () use ($value, $args, $context, $info) { |
108
|
|
|
return $this->resolveDeferred([$this, 'resolveValues'], $value, $args, $context, $info); |
109
|
|
|
}, $context->getContext('language', $info)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
protected function resolveDeferred(callable $callback, $value, array $args, ResolveContext $context, ResolveInfo $info) { |
116
|
|
|
$result = $callback($value, $args, $context, $info); |
117
|
|
|
|
118
|
|
|
if (is_callable($result)) { |
119
|
|
|
return new Deferred(function () use ($result, $value, $args, $context, $info) { |
120
|
|
|
return $this->resolveDeferred($result, $value, $args, $context, $info); |
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$result = iterator_to_array($result); |
125
|
|
|
|
126
|
|
|
// Only collect cache metadata if this is a query. All other operation types |
127
|
|
|
// are not cacheable anyways. |
128
|
|
|
if ($info->operation->operation === 'query') { |
129
|
|
|
$dependencies = $this->getCacheDependencies($result, $value, $args, $context, $info); |
130
|
|
|
foreach ($dependencies as $dependency) { |
131
|
|
|
$context->addCacheableDependency($dependency); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->unwrapResult($result, $info); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Unwrap the resolved values. |
140
|
|
|
* |
141
|
|
|
* @param array $result |
142
|
|
|
* The resolved values. |
143
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
144
|
|
|
* The resolve info object. |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
* The extracted values (an array of values in case this is a list, an |
148
|
|
|
* arbitrary value if it isn't). |
149
|
|
|
*/ |
150
|
|
|
protected function unwrapResult($result, ResolveInfo $info) { |
151
|
|
|
$result = array_map(function ($item) { |
152
|
|
|
return $item instanceof ValueWrapperInterface ? $item->getValue() : $item; |
153
|
|
|
}, $result); |
154
|
|
|
|
155
|
|
|
$result = array_map(function ($item) { |
156
|
|
|
return $item instanceof MarkupInterface ? $item->__toString() : $item; |
|
|
|
|
157
|
|
|
}, $result); |
158
|
|
|
|
159
|
|
|
// If this is a list, return the result as an array. |
160
|
|
|
$type = $info->returnType; |
161
|
|
|
if ($type instanceof ListOfType || ($type instanceof NonNull && $type->getWrappedType() instanceof ListOfType)) { |
162
|
|
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return !empty($result) ? reset($result) : NULL; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Retrieve the list of cache dependencies for a given value and arguments. |
170
|
|
|
* |
171
|
|
|
* @param array $result |
172
|
|
|
* The result of the field. |
173
|
|
|
* @param mixed $parent |
174
|
|
|
* The parent value. |
175
|
|
|
* @param array $args |
176
|
|
|
* The arguments passed to the field. |
177
|
|
|
* @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
178
|
|
|
* The resolve context. |
179
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
180
|
|
|
* The resolve info object. |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
* A list of cacheable dependencies. |
184
|
|
|
*/ |
185
|
|
|
protected function getCacheDependencies(array $result, $parent, array $args, ResolveContext $context, ResolveInfo $info) { |
186
|
|
|
$self = new CacheableMetadata(); |
187
|
|
|
$definition = $this->getPluginDefinition(); |
188
|
|
|
if (!empty($definition['response_cache_contexts'])) { |
189
|
|
|
$self->addCacheContexts($definition['response_cache_contexts']); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!empty($definition['response_cache_tags'])) { |
193
|
|
|
$self->addCacheTags($definition['response_cache_tags']); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (isset($definition['response_cache_max_age'])) { |
197
|
|
|
$self->mergeCacheMaxAge($definition['response_cache_max_age']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return array_merge([$self], array_filter($result, function ($item) { |
201
|
|
|
return $item instanceof CacheableDependencyInterface; |
|
|
|
|
202
|
|
|
})); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Retrieve the list of field values. |
207
|
|
|
* |
208
|
|
|
* Always returns a list of field values. Even for single value fields. |
209
|
|
|
* Single/multi field handling is responsibility of the base class. |
210
|
|
|
* |
211
|
|
|
* @param mixed $value |
212
|
|
|
* The current object value. |
213
|
|
|
* @param array $args |
214
|
|
|
* Field arguments. |
215
|
|
|
* @param $context |
216
|
|
|
* The resolve context. |
217
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
218
|
|
|
* The resolve info object. |
219
|
|
|
* |
220
|
|
|
* @return \Generator |
221
|
|
|
* The value generator. |
222
|
|
|
*/ |
223
|
|
|
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
224
|
|
|
// Allow overriding this class without having to declare this method. |
225
|
|
|
yield NULL; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
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.