Version 4.6.0
This commit is contained in:
1 parent
f79dcf067a
commit
517a5e1f70
2036 files changed
+110041
-26162
No files matched your search
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\FieldNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Language\VisitorOperation;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class ProvidedRequiredArguments extends ValidationRule
|
||||
{
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$providedRequiredArgumentsOnDirectives = new ProvidedRequiredArgumentsOnDirectives();
|
||||
|
||||
return $providedRequiredArgumentsOnDirectives->getVisitor($context) + [
|
||||
NodeKind::FIELD => [
|
||||
'leave' => static function (FieldNode $fieldNode) use ($context) : ?VisitorOperation {
|
||||
$fieldDef = $context->getFieldDef();
|
||||
|
||||
if (! $fieldDef) {
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
$argNodes = $fieldNode->arguments ?? [];
|
||||
|
||||
$argNodeMap = [];
|
||||
foreach ($argNodes as $argNode) {
|
||||
$argNodeMap[$argNode->name->value] = $argNode;
|
||||
}
|
||||
foreach ($fieldDef->args as $argDef) {
|
||||
$argNode = $argNodeMap[$argDef->name] ?? null;
|
||||
if ($argNode || ! $argDef->isRequired()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::missingFieldArgMessage($fieldNode->name->value, $argDef->name, $argDef->getType()),
|
||||
[$fieldNode]
|
||||
));
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function missingFieldArgMessage($fieldName, $argName, $type)
|
||||
{
|
||||
return sprintf(
|
||||
'Field "%s" argument "%s" of type "%s" is required but not provided.',
|
||||
$fieldName,
|
||||
$argName,
|
||||
$type
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user