class DefaultValue in GraphQL 8.4
Same name in this branch
- 8.4 src/GraphQL/Resolver/DefaultValue.php \Drupal\graphql\GraphQL\Resolver\DefaultValue
- 8.4 src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/DefaultValue.php \Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields\DefaultValue
Default value resolver.
Allows to fall back if a value is NULL.
Hierarchy
- class \Drupal\graphql\GraphQL\Resolver\DefaultValue implements ResolverInterface
Expanded class hierarchy of DefaultValue
1 file declares its use of DefaultValue
- ResolverBuilder.php in src/
GraphQL/ ResolverBuilder.php
File
- src/
GraphQL/ Resolver/ DefaultValue.php, line 16
Namespace
Drupal\graphql\GraphQL\ResolverView source
class DefaultValue implements ResolverInterface {
/**
* The initial value.
*
* @var \Drupal\graphql\GraphQL\Resolver\ResolverInterface
*/
protected $value;
/**
* The fallback value in case the initial value resolves to NULL.
*
* @var \Drupal\graphql\GraphQL\Resolver\ResolverInterface
*/
protected $default;
/**
* DefaultValue constructor.
*
* @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface $value
* The initial value to check.
* @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface $default
* The fallback value returned if the initial one resolves to NULL.
*/
public function __construct(ResolverInterface $value, ResolverInterface $default) {
$this->value = $value;
$this->default = $default;
}
/**
* {@inheritDoc}
*/
public function resolve($value, $args, ResolveContext $context, ResolveInfo $info, FieldContext $field) {
$result = $this->value
->resolve($value, $args, $context, $info, $field);
if ($result === NULL) {
return $this->default
->resolve($value, $args, $context, $info, $field);
}
if ($result instanceof SyncPromise) {
return DeferredUtility::returnFinally($result, function ($current) use ($value, $args, $context, $info, $field) {
if ($current === NULL) {
return $this->default
->resolve($value, $args, $context, $info, $field);
}
return $current;
});
}
return $result;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultValue:: |
protected | property | The fallback value in case the initial value resolves to NULL. | |
DefaultValue:: |
protected | property | The initial value. | |
DefaultValue:: |
public | function |
Resolve values for the fields. Overrides ResolverInterface:: |
|
DefaultValue:: |
public | function | DefaultValue constructor. |