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
UniqueArgumentDefinitionNames.php
74 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\Language\AST\DirectiveDefinitionNode; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InputValueDefinitionNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InterfaceTypeDefinitionNode; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InterfaceTypeExtensionNode; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeList; |
| 12 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\ObjectTypeDefinitionNode; |
| 13 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\ObjectTypeExtensionNode; |
| 14 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor; |
| 15 | use Automattic\WooCommerce\Vendor\GraphQL\Language\VisitorOperation; |
| 16 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\SDLValidationContext; |
| 17 | |
| 18 | /** |
| 19 | * Unique argument definition names. |
| 20 | * |
| 21 | * A Automattic\WooCommerce\Vendor\GraphQL Object or Interface type is only valid if all its fields have uniquely named arguments. |
| 22 | * A Automattic\WooCommerce\Vendor\GraphQL Directive is only valid if all its arguments are uniquely named. |
| 23 | */ |
| 24 | class UniqueArgumentDefinitionNames extends ValidationRule |
| 25 | { |
| 26 | public function getSDLVisitor(SDLValidationContext $context): array |
| 27 | { |
| 28 | $checkArgUniquenessPerField = static function ($node) use ($context): VisitorOperation { |
| 29 | assert( |
| 30 | $node instanceof InterfaceTypeDefinitionNode |
| 31 | || $node instanceof InterfaceTypeExtensionNode |
| 32 | || $node instanceof ObjectTypeDefinitionNode |
| 33 | || $node instanceof ObjectTypeExtensionNode |
| 34 | ); |
| 35 | |
| 36 | foreach ($node->fields as $fieldDef) { |
| 37 | self::checkArgUniqueness("{$node->name->value}.{$fieldDef->name->value}", $fieldDef->arguments, $context); |
| 38 | } |
| 39 | |
| 40 | return Visitor::skipNode(); |
| 41 | }; |
| 42 | |
| 43 | return [ |
| 44 | NodeKind::DIRECTIVE_DEFINITION => static fn (DirectiveDefinitionNode $node): VisitorOperation => self::checkArgUniqueness("@{$node->name->value}", $node->arguments, $context), |
| 45 | NodeKind::INTERFACE_TYPE_DEFINITION => $checkArgUniquenessPerField, |
| 46 | NodeKind::INTERFACE_TYPE_EXTENSION => $checkArgUniquenessPerField, |
| 47 | NodeKind::OBJECT_TYPE_DEFINITION => $checkArgUniquenessPerField, |
| 48 | NodeKind::OBJECT_TYPE_EXTENSION => $checkArgUniquenessPerField, |
| 49 | ]; |
| 50 | } |
| 51 | |
| 52 | /** @param NodeList<InputValueDefinitionNode> $arguments */ |
| 53 | private static function checkArgUniqueness(string $parentName, NodeList $arguments, SDLValidationContext $context): VisitorOperation |
| 54 | { |
| 55 | $seenArgs = []; |
| 56 | foreach ($arguments as $argument) { |
| 57 | $seenArgs[$argument->name->value][] = $argument; |
| 58 | } |
| 59 | |
| 60 | foreach ($seenArgs as $argName => $argNodes) { |
| 61 | if (count($argNodes) > 1) { |
| 62 | $context->reportError( |
| 63 | new Error( |
| 64 | "Argument \"{$parentName}({$argName}:)\" can only be defined once.", |
| 65 | $argNodes, |
| 66 | ), |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return Visitor::skipNode(); |
| 72 | } |
| 73 | } |
| 74 |