Version 4.6.0
This commit is contained in:
1 parent
f79dcf067a
commit
517a5e1f70
2036 files changed
+110041
-26162
No files matched your search
@@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@@ -8,6 +11,7 @@ use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* Lone anonymous operation
|
||||
@@ -15,33 +19,40 @@ use GraphQL\Validator\ValidationContext;
|
||||
* A GraphQL document is only valid if when it contains an anonymous operation
|
||||
* (the query short-hand) that it contains only that one operation definition.
|
||||
*/
|
||||
class LoneAnonymousOperation extends AbstractValidationRule
|
||||
class LoneAnonymousOperation extends ValidationRule
|
||||
{
|
||||
static function anonOperationNotAloneMessage()
|
||||
{
|
||||
return 'This anonymous operation must be the only defined operation.';
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$operationCount = 0;
|
||||
|
||||
return [
|
||||
NodeKind::DOCUMENT => function(DocumentNode $node) use (&$operationCount) {
|
||||
NodeKind::DOCUMENT => static function (DocumentNode $node) use (&$operationCount) : void {
|
||||
$tmp = Utils::filter(
|
||||
$node->definitions,
|
||||
function ($definition) {
|
||||
return $definition->kind === NodeKind::OPERATION_DEFINITION;
|
||||
static function (Node $definition) : bool {
|
||||
return $definition instanceof OperationDefinitionNode;
|
||||
}
|
||||
);
|
||||
|
||||
$operationCount = count($tmp);
|
||||
},
|
||||
NodeKind::OPERATION_DEFINITION => function(OperationDefinitionNode $node) use (&$operationCount, $context) {
|
||||
if (!$node->name && $operationCount > 1) {
|
||||
$context->reportError(
|
||||
new Error(self::anonOperationNotAloneMessage(), [$node])
|
||||
);
|
||||
NodeKind::OPERATION_DEFINITION => static function (OperationDefinitionNode $node) use (
|
||||
&$operationCount,
|
||||
$context
|
||||
) : void {
|
||||
if ($node->name !== null || $operationCount <= 1) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$context->reportError(
|
||||
new Error(self::anonOperationNotAloneMessage(), [$node])
|
||||
);
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function anonOperationNotAloneMessage()
|
||||
{
|
||||
return 'This anonymous operation must be the only defined operation.';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user