CustomValidationRule.php
2 months ago
DisableIntrospection.php
2 months ago
ExecutableDefinitions.php
2 months ago
FieldsOnCorrectType.php
2 months ago
FragmentsOnCompositeTypes.php
2 months ago
KnownArgumentNames.php
2 months ago
KnownArgumentNamesOnDirectives.php
2 months ago
KnownDirectives.php
2 months ago
KnownFragmentNames.php
2 months ago
KnownTypeNames.php
2 months ago
LoneAnonymousOperation.php
2 months ago
LoneSchemaDefinition.php
2 months ago
NoFragmentCycles.php
2 months ago
NoUndefinedVariables.php
2 months ago
NoUnusedFragments.php
2 months ago
NoUnusedVariables.php
2 months ago
OneOfInputObjectsRule.php
2 months ago
OverlappingFieldsCanBeMerged.php
2 months ago
PossibleFragmentSpreads.php
2 months ago
PossibleTypeExtensions.php
2 months ago
ProvidedRequiredArguments.php
2 months ago
ProvidedRequiredArgumentsOnDirectives.php
2 months ago
QueryComplexity.php
2 months ago
QueryDepth.php
2 months ago
QuerySecurityRule.php
2 months ago
ScalarLeafs.php
2 months ago
SingleFieldSubscription.php
2 months ago
UniqueArgumentDefinitionNames.php
2 months ago
UniqueArgumentNames.php
2 months ago
UniqueDirectiveNames.php
2 months ago
UniqueDirectivesPerLocation.php
2 months ago
UniqueEnumValueNames.php
2 months ago
UniqueFieldDefinitionNames.php
2 months ago
UniqueFragmentNames.php
2 months ago
UniqueInputFieldNames.php
2 months ago
UniqueOperationNames.php
2 months ago
UniqueOperationTypes.php
2 months ago
UniqueTypeNames.php
2 months ago
UniqueVariableNames.php
2 months ago
ValidationRule.php
2 months ago
ValuesOfCorrectType.php
2 months ago
VariablesAreInputTypes.php
2 months ago
VariablesInAllowedPosition.php
2 months ago
KnownArgumentNamesOnDirectives.php
111 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\Error; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Error\InvariantViolation; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\DirectiveDefinitionNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\DirectiveNode; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Language\VisitorOperation; |
| 12 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Argument; |
| 13 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Directive; |
| 14 | use Automattic\WooCommerce\Vendor\GraphQL\Utils\Utils; |
| 15 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext; |
| 16 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\SDLValidationContext; |
| 17 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\ValidationContext; |
| 18 | |
| 19 | /** |
| 20 | * Known argument names on directives. |
| 21 | * |
| 22 | * A Automattic\WooCommerce\Vendor\GraphQL directive is only valid if all supplied arguments are defined by |
| 23 | * that field. |
| 24 | * |
| 25 | * @phpstan-import-type VisitorArray from Visitor |
| 26 | */ |
| 27 | class KnownArgumentNamesOnDirectives extends ValidationRule |
| 28 | { |
| 29 | /** @param array<string> $suggestedArgs */ |
| 30 | public static function unknownDirectiveArgMessage(string $argName, string $directiveName, array $suggestedArgs): string |
| 31 | { |
| 32 | $message = "Unknown argument \"{$argName}\" on directive \"@{$directiveName}\"."; |
| 33 | |
| 34 | if (isset($suggestedArgs[0])) { |
| 35 | $suggestions = Utils::quotedOrList($suggestedArgs); |
| 36 | $message .= " Did you mean {$suggestions}?"; |
| 37 | } |
| 38 | |
| 39 | return $message; |
| 40 | } |
| 41 | |
| 42 | /** @throws InvariantViolation */ |
| 43 | public function getSDLVisitor(SDLValidationContext $context): array |
| 44 | { |
| 45 | return $this->getASTVisitor($context); |
| 46 | } |
| 47 | |
| 48 | /** @throws InvariantViolation */ |
| 49 | public function getVisitor(QueryValidationContext $context): array |
| 50 | { |
| 51 | return $this->getASTVisitor($context); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @throws InvariantViolation |
| 56 | * |
| 57 | * @phpstan-return VisitorArray |
| 58 | */ |
| 59 | public function getASTVisitor(ValidationContext $context): array |
| 60 | { |
| 61 | $directiveArgs = []; |
| 62 | $schema = $context->getSchema(); |
| 63 | $definedDirectives = $schema !== null |
| 64 | ? $schema->getDirectives() |
| 65 | : Directive::getInternalDirectives(); |
| 66 | |
| 67 | foreach ($definedDirectives as $directive) { |
| 68 | $directiveArgs[$directive->name] = array_map( |
| 69 | static fn (Argument $arg): string => $arg->name, |
| 70 | $directive->args |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | $astDefinitions = $context->getDocument()->definitions; |
| 75 | foreach ($astDefinitions as $def) { |
| 76 | if ($def instanceof DirectiveDefinitionNode) { |
| 77 | $argNames = []; |
| 78 | foreach ($def->arguments as $arg) { |
| 79 | $argNames[] = $arg->name->value; |
| 80 | } |
| 81 | |
| 82 | $directiveArgs[$def->name->value] = $argNames; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return [ |
| 87 | NodeKind::DIRECTIVE => static function (DirectiveNode $directiveNode) use ($directiveArgs, $context): VisitorOperation { |
| 88 | $directiveName = $directiveNode->name->value; |
| 89 | |
| 90 | if (! isset($directiveArgs[$directiveName])) { |
| 91 | return Visitor::skipNode(); |
| 92 | } |
| 93 | $knownArgs = $directiveArgs[$directiveName]; |
| 94 | |
| 95 | foreach ($directiveNode->arguments as $argNode) { |
| 96 | $argName = $argNode->name->value; |
| 97 | if (! in_array($argName, $knownArgs, true)) { |
| 98 | $suggestions = Utils::suggestionList($argName, $knownArgs); |
| 99 | $context->reportError(new Error( |
| 100 | static::unknownDirectiveArgMessage($argName, $directiveName, $suggestions), |
| 101 | [$argNode] |
| 102 | )); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return Visitor::skipNode(); |
| 107 | }, |
| 108 | ]; |
| 109 | } |
| 110 | } |
| 111 |