KnownArgumentNames.php 3.19 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
<?php
namespace GraphQL\Validator\Rules;

use GraphQL\Error\Error;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext;

/**
 * Known argument names
 *
 * A GraphQL field is only valid if all supplied arguments are defined by
 * that field.
 */
class KnownArgumentNames extends AbstractValidationRule
{
    public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs)
    {
        $message = "Unknown argument \"$argName\" on field \"$fieldName\" of type \"$typeName\".";
        if ($suggestedArgs) {
            $message .= ' Did you mean ' . Utils::quotedOrList($suggestedArgs) . '?';
        }
        return $message;
    }

    public static function unknownDirectiveArgMessage($argName, $directiveName, array $suggestedArgs)
    {
        $message = "Unknown argument \"$argName\" on directive \"@$directiveName\".";
        if ($suggestedArgs) {
            $message .= ' Did you mean ' . Utils::quotedOrList($suggestedArgs) . '?';
        }
        return $message;
    }

    public function getVisitor(ValidationContext $context)
    {
        return [
            NodeKind::ARGUMENT => function(ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) {
                $argDef = $context->getArgument();
                if (!$argDef) {
                    $argumentOf = $ancestors[count($ancestors) - 1];
                    if ($argumentOf->kind === NodeKind::FIELD) {
                        $fieldDef = $context->getFieldDef();
                        $parentType = $context->getParentType();
                        if ($fieldDef && $parentType) {
                            $context->reportError(new Error(
                                self::unknownArgMessage(
                                    $node->name->value,
                                    $fieldDef->name,
                                    $parentType->name,
                                    Utils::suggestionList(
                                        $node->name->value,
                                        array_map(function ($arg) { return $arg->name; }, $fieldDef->args)
                                    )
                                ),
                                [$node]
                            ));
                        }
                    } else if ($argumentOf->kind === NodeKind::DIRECTIVE) {
                        $directive = $context->getDirective();
                        if ($directive) {
                            $context->reportError(new Error(
                                self::unknownDirectiveArgMessage(
                                    $node->name->value,
                                    $directive->name,
                                    Utils::suggestionList(
                                        $node->name->value,
                                        array_map(function ($arg) { return $arg->name; }, $directive->args)
                                    )
                                ),
                                [$node]
                            ));
                        }
                    }
                }
            }
        ];
    }
}