Version 4.6.0
This commit is contained in:
1 parent
f79dcf067a
commit
517a5e1f70
2036 files changed
+110041
-26162
No files matched your search
+30
-24
@@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@@ -6,40 +9,43 @@ use GraphQL\Language\AST\FieldNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class ScalarLeafs extends AbstractValidationRule
|
||||
class ScalarLeafs extends ValidationRule
|
||||
{
|
||||
static function noSubselectionAllowedMessage($field, $type)
|
||||
{
|
||||
return "Field \"$field\" of type \"$type\" must not have a sub selection.";
|
||||
}
|
||||
|
||||
static function requiredSubselectionMessage($field, $type)
|
||||
{
|
||||
return "Field \"$field\" of type \"$type\" must have a sub selection.";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::FIELD => function(FieldNode $node) use ($context) {
|
||||
NodeKind::FIELD => static function (FieldNode $node) use ($context) : void {
|
||||
$type = $context->getType();
|
||||
if ($type) {
|
||||
if (Type::isLeafType(Type::getNamedType($type))) {
|
||||
if ($node->selectionSet) {
|
||||
$context->reportError(new Error(
|
||||
self::noSubselectionAllowedMessage($node->name->value, $type),
|
||||
[$node->selectionSet]
|
||||
));
|
||||
}
|
||||
} else if (!$node->selectionSet) {
|
||||
if (! $type) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Type::isLeafType(Type::getNamedType($type))) {
|
||||
if ($node->selectionSet) {
|
||||
$context->reportError(new Error(
|
||||
self::requiredSubselectionMessage($node->name->value, $type),
|
||||
[$node]
|
||||
self::noSubselectionAllowedMessage($node->name->value, $type),
|
||||
[$node->selectionSet]
|
||||
));
|
||||
}
|
||||
} elseif (! $node->selectionSet) {
|
||||
$context->reportError(new Error(
|
||||
self::requiredSubselectionMessage($node->name->value, $type),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function noSubselectionAllowedMessage($field, $type)
|
||||
{
|
||||
return sprintf('Field "%s" of type "%s" must not have a sub selection.', $field, $type);
|
||||
}
|
||||
|
||||
public static function requiredSubselectionMessage($field, $type)
|
||||
{
|
||||
return sprintf('Field "%s" of type "%s" must have a sub selection.', $field, $type);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user