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
KnownArgumentNames.php
76 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\ArgumentNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Argument; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\NamedType; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Utils\Utils; |
| 12 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext; |
| 13 | |
| 14 | /** |
| 15 | * Known argument names. |
| 16 | * |
| 17 | * A Automattic\WooCommerce\Vendor\GraphQL field is only valid if all supplied arguments are defined by |
| 18 | * that field. |
| 19 | */ |
| 20 | class KnownArgumentNames extends ValidationRule |
| 21 | { |
| 22 | /** @throws InvariantViolation */ |
| 23 | public function getVisitor(QueryValidationContext $context): array |
| 24 | { |
| 25 | $knownArgumentNamesOnDirectives = new KnownArgumentNamesOnDirectives(); |
| 26 | |
| 27 | return $knownArgumentNamesOnDirectives->getVisitor($context) + [ |
| 28 | NodeKind::ARGUMENT => static function (ArgumentNode $node) use ($context): void { |
| 29 | $argDef = $context->getArgument(); |
| 30 | if ($argDef !== null) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $fieldDef = $context->getFieldDef(); |
| 35 | if ($fieldDef === null) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $parentType = $context->getParentType(); |
| 40 | if (! $parentType instanceof NamedType) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | $context->reportError(new Error( |
| 45 | static::unknownArgMessage( |
| 46 | $node->name->value, |
| 47 | $fieldDef->name, |
| 48 | $parentType->name, |
| 49 | Utils::suggestionList( |
| 50 | $node->name->value, |
| 51 | array_map( |
| 52 | static fn (Argument $arg): string => $arg->name, |
| 53 | $fieldDef->args |
| 54 | ) |
| 55 | ) |
| 56 | ), |
| 57 | [$node] |
| 58 | )); |
| 59 | }, |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | /** @param array<string> $suggestedArgs */ |
| 64 | public static function unknownArgMessage(string $argName, string $fieldName, string $typeName, array $suggestedArgs): string |
| 65 | { |
| 66 | $message = "Unknown argument \"{$argName}\" on field \"{$fieldName}\" of type \"{$typeName}\"."; |
| 67 | |
| 68 | if ($suggestedArgs !== []) { |
| 69 | $suggestions = Utils::quotedOrList($suggestedArgs); |
| 70 | $message .= " Did you mean {$suggestions}?"; |
| 71 | } |
| 72 | |
| 73 | return $message; |
| 74 | } |
| 75 | } |
| 76 |