Skip to content

Commit 80a9902

Browse files
committed
Adding context validation for plugin input.
1 parent 17cc08f commit 80a9902

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

src/Plugin/GraphQL/DataProducer/DataProducerProxy.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ public function resolve($value, $args, ResolveContext $context, ResolveInfo $inf
148148
$plugin = $this->prepare($value, $args, $context, $info, $field);
149149

150150
return DeferredUtility::returnFinally($plugin, function (DataProducerPluginInterface $plugin) use ($context, $field) {
151+
foreach ($plugin->getContexts() as $id => $item) {
152+
/** @var \Drupal\Core\Plugin\Context\Context $item */
153+
if ($item->getContextDefinition()->isRequired() && !$item->hasContextValue()) {
154+
return NULL;
155+
}
156+
}
157+
151158
if ($this->cached && $plugin instanceof DataProducerPluginCachingInterface) {
152159
if (!!$context->getServer()->get('caching')) {
153160
return $this->resolveCached($plugin, $context, $field);
@@ -165,31 +172,34 @@ public function resolve($value, $args, ResolveContext $context, ResolveInfo $inf
165172
* @param \GraphQL\Type\Definition\ResolveInfo $info
166173
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $field
167174
*
168-
* @return \GraphQL\Deferred|mixed
175+
* @return \GraphQL\Deferred|\Drupal\graphql\Plugin\DataProducerPluginInterface
169176
*
170177
* @throws \Drupal\Component\Plugin\Exception\PluginException
171178
* @throws \Exception
172179
*/
173180
protected function prepare($value, $args, ResolveContext $context, ResolveInfo $info, FieldContext $field) {
174181
/** @var DataProducerPluginInterface $plugin */
175182
$plugin = $this->pluginManager->createInstance($this->id, $this->config);
176-
$contexts = [];
183+
$contexts = $plugin->getContextDefinitions();
177184

178-
foreach ($plugin->getContextDefinitions() as $name => $definition) {
185+
$values = [];
186+
foreach ($contexts as $name => $definition) {
179187
$mapper = $this->mapping[$name] ?? NULL;
180-
if (isset($mapper)) {
181-
if (!$mapper instanceof ResolverInterface) {
182-
throw new \Exception(sprintf('Invalid input mapper for argument %s.', $name));
183-
}
188+
if ($definition->isRequired() && empty($mapper)) {
189+
throw new \LogicException(sprintf('Missing input mapper for argument %s.', $name));
190+
}
184191

185-
$contexts[$name] = $mapper->resolve($value, $args, $context, $info, $field);
192+
if (!empty($mapper) && !($mapper instanceof ResolverInterface)) {
193+
throw new \Exception(sprintf('Invalid input mapper for argument %s.', $name));
186194
}
195+
196+
$values[$name] = !empty($mapper) ? $mapper->resolve($value, $args, $context, $info, $field) : NULL;
187197
}
188198

189-
$contexts = DeferredUtility::waitAll($contexts);
190-
return DeferredUtility::returnFinally($contexts, function ($contexts) use ($plugin) {
191-
foreach ($contexts as $name => $context) {
192-
$plugin->setContextValue($name, $context);
199+
$values = DeferredUtility::waitAll($values);
200+
return DeferredUtility::returnFinally($values, function ($values) use ($contexts, $plugin) {
201+
foreach ($values as $name => $value) {
202+
$plugin->setContextValue($name, $value);
193203
}
194204

195205
return $plugin;

0 commit comments

Comments
 (0)