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,68 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\ArgumentNode;
|
||||
use GraphQL\Language\AST\DirectiveNode;
|
||||
use GraphQL\Language\AST\FieldNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_map;
|
||||
use function count;
|
||||
use function sprintf;
|
||||
|
||||
class KnownArgumentNames extends AbstractValidationRule
|
||||
/**
|
||||
* Known argument names
|
||||
*
|
||||
* A GraphQL field is only valid if all supplied arguments are defined by
|
||||
* that field.
|
||||
*/
|
||||
class KnownArgumentNames extends ValidationRule
|
||||
{
|
||||
public static function unknownArgMessage($argName, $fieldName, $type)
|
||||
{
|
||||
return "Unknown argument \"$argName\" on field \"$fieldName\" of type \"$type\".";
|
||||
}
|
||||
|
||||
public static function unknownDirectiveArgMessage($argName, $directiveName)
|
||||
{
|
||||
return "Unknown argument \"$argName\" on directive \"@$directiveName\".";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::ARGUMENT => function(ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) {
|
||||
$argumentOf = $ancestors[count($ancestors) - 1];
|
||||
if ($argumentOf->kind === NodeKind::FIELD) {
|
||||
$fieldDef = $context->getFieldDef();
|
||||
$knownArgumentNamesOnDirectives = new KnownArgumentNamesOnDirectives();
|
||||
|
||||
if ($fieldDef) {
|
||||
$fieldArgDef = null;
|
||||
foreach ($fieldDef->args as $arg) {
|
||||
if ($arg->name === $node->name->value) {
|
||||
$fieldArgDef = $arg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$fieldArgDef) {
|
||||
$parentType = $context->getParentType();
|
||||
Utils::invariant($parentType);
|
||||
$context->reportError(new Error(
|
||||
self::unknownArgMessage($node->name->value, $fieldDef->name, $parentType->name),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
}
|
||||
} else if ($argumentOf->kind === NodeKind::DIRECTIVE) {
|
||||
$directive = $context->getDirective();
|
||||
if ($directive) {
|
||||
$directiveArgDef = null;
|
||||
foreach ($directive->args as $arg) {
|
||||
if ($arg->name === $node->name->value) {
|
||||
$directiveArgDef = $arg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$directiveArgDef) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownDirectiveArgMessage($node->name->value, $directive->name),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
}
|
||||
return $knownArgumentNamesOnDirectives->getVisitor($context) + [
|
||||
NodeKind::ARGUMENT => static function (ArgumentNode $node) use ($context) : void {
|
||||
$argDef = $context->getArgument();
|
||||
if ($argDef !== null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$fieldDef = $context->getFieldDef();
|
||||
$parentType = $context->getParentType();
|
||||
if ($fieldDef === null || ! ($parentType instanceof Type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::unknownArgMessage(
|
||||
$node->name->value,
|
||||
$fieldDef->name,
|
||||
$parentType->name,
|
||||
Utils::suggestionList(
|
||||
$node->name->value,
|
||||
array_map(
|
||||
static function ($arg) : string {
|
||||
return $arg->name;
|
||||
},
|
||||
$fieldDef->args
|
||||
)
|
||||
)
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
|
||||
return;
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $suggestedArgs
|
||||
*/
|
||||
public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs)
|
||||
{
|
||||
$message = sprintf('Unknown argument "%s" on field "%s" of type "%s".', $argName, $fieldName, $typeName);
|
||||
if (isset($suggestedArgs[0])) {
|
||||
$message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs));
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user