digiaonline /
graphql-php
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Digia\GraphQL\Validation\Rule; |
||
| 4 | |||
| 5 | use Digia\GraphQL\Language\Node\FragmentDefinitionNode; |
||
| 6 | use Digia\GraphQL\Language\Node\SelectionSetNode; |
||
| 7 | use Digia\GraphQL\Language\Node\VariableDefinitionNode; |
||
| 8 | use Digia\GraphQL\Language\Visitor\VisitorResult; |
||
| 9 | use Digia\GraphQL\Type\Definition\NonNullType; |
||
| 10 | use Digia\GraphQL\Validation\ValidationException; |
||
| 11 | use function Digia\GraphQL\Validation\variableDefaultValueNotAllowedMessage; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Variable's default value is allowed |
||
| 15 | * |
||
| 16 | * A GraphQL document is only valid if all variable default values are allowed |
||
| 17 | * due to a variable not being required. |
||
| 18 | */ |
||
| 19 | class VariablesDefaultValueAllowedRule extends AbstractRule |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @inheritdoc |
||
| 23 | */ |
||
| 24 | protected function enterSelectionSet(SelectionSetNode $node): VisitorResult |
||
| 25 | { |
||
| 26 | return new VisitorResult(null); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @inheritdoc |
||
| 31 | */ |
||
| 32 | protected function enterFragmentDefinition(FragmentDefinitionNode $node): VisitorResult |
||
| 33 | { |
||
| 34 | return new VisitorResult(null); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @inheritdoc |
||
| 39 | */ |
||
| 40 | protected function enterVariableDefinition(VariableDefinitionNode $node): VisitorResult |
||
| 41 | { |
||
| 42 | $variable = $node->getVariable(); |
||
| 43 | $variableName = $variable->getNameValue(); |
||
| 44 | $defaultValue = $node->getDefaultValue(); |
||
| 45 | $type = $this->context->getInputType(); |
||
| 46 | |||
| 47 | if (null !== $defaultValue && $type instanceof NonNullType) { |
||
| 48 | $this->context->reportError( |
||
| 49 | new ValidationException( |
||
| 50 | variableDefaultValueNotAllowedMessage($variableName, $type, (string)$type->getOfType()), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 51 | [$defaultValue] |
||
| 52 | ) |
||
| 53 | ); |
||
| 54 | } |
||
| 55 | |||
| 56 | return new VisitorResult(null); // do not traverse further. |
||
| 57 | } |
||
| 58 | } |
||
| 59 |