Skip to content

Commit 53c4871

Browse files
authored
Allow optional arguments. (#674)
1 parent ba1e9d4 commit 53c4871

4 files changed

Lines changed: 26 additions & 26 deletions

File tree

modules/graphql_core/src/Plugin/Deriver/Fields/EntityRenderedDeriver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public function getDerivativeDefinitions($basePluginDefinition) {
4949
'entity_type' => $id,
5050
] + $basePluginDefinition;
5151

52+
if (!isset($derivative['arguments']['mode'])) {
53+
$derivative['arguments'] = isset($derivative['arguments']) ? $derivative['arguments'] : [];
54+
$derivative['arguments']['mode'] = [
55+
'type' => StringHelper::camelCase($id, 'display', 'mode', 'id'),
56+
'optional' => TRUE,
57+
];
58+
}
59+
5260
$this->derivatives["entity:$id"] = $derivative;
5361
}
5462
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityRendered.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRenderedDeriver",
2424
* )
2525
*/
26-
class EntityRendered extends FieldPluginBase implements ContainerFactoryPluginInterface {
26+
class EntityRendered extends FieldPluginBase implements ContainerFactoryPluginInterface {
2727
use DependencySerializationTrait;
2828

2929
/**
@@ -79,28 +79,6 @@ public function __construct(
7979
$this->renderer = $renderer;
8080
}
8181

82-
// TODO: Fix this.
83-
// /**
84-
// * {@inheritdoc}
85-
// */
86-
// protected function buildArguments(SchemaBuilderInterface $builder) {
87-
// $arguments = parent::buildArguments($builder);
88-
//
89-
// if (empty($arguments['mode'])) {
90-
// $definition = $this->getPluginDefinition();
91-
// $type = StringHelper::camelCase($definition['entity_type'], 'display', 'mode', 'id');
92-
//
93-
// if ($type = $builder->findByName($type, [GRAPHQL_ENUM_PLUGIN])) {
94-
// $arguments['mode'] = new InputField([
95-
// 'name' => 'mode',
96-
// 'type' => $type->getDefinition($builder),
97-
// ]);
98-
// }
99-
// }
100-
//
101-
// return $arguments;
102-
// }
103-
10482
/**
10583
* {@inheritdoc}
10684
*/

src/Plugin/GraphQL/Schemas/SchemaPluginBase.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,24 @@ public function processFields(array $fields) {
446446
* {@inheritdoc}
447447
*/
448448
public function processArguments(array $args) {
449-
return array_map(function ($arg) {
449+
return array_filter(array_map(function ($arg) {
450+
try {
451+
$type = $this->processType($arg['type']);
452+
}
453+
catch (\Exception $e) {
454+
// Allow optional arguments that are removed if the input type is
455+
// not defined.
456+
if (empty($arg['optional'])) {
457+
throw $e;
458+
}
459+
460+
return NULL;
461+
}
462+
450463
return [
451-
'type' => $this->processType($arg['type']),
464+
'type' => $type,
452465
] + $arg;
453-
}, $args);
466+
}, $args));
454467
}
455468

456469
/**

src/Plugin/GraphQL/Traits/ArgumentAwarePluginTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ trait ArgumentAwarePluginTrait {
1818
protected function buildArguments($definition) {
1919
return array_map(function ($argument) use ($definition) {
2020
return [
21+
'optional' => !empty($argument['optional']),
2122
'type' => $this->buildArgumentType($argument, $definition),
2223
'description' => $this->buildArgumentDescription($argument, $definition),
2324
'defaultValue' => $this->buildArgumentDefault($argument, $definition),

0 commit comments

Comments
 (0)