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
UniqueDirectivesPerLocation.php
97 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\Node; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Directive; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext; |
| 12 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\SDLValidationContext; |
| 13 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\ValidationContext; |
| 14 | |
| 15 | /** |
| 16 | * Unique directive names per location. |
| 17 | * |
| 18 | * A Automattic\WooCommerce\Vendor\GraphQL document is only valid if all non-repeatable directives at |
| 19 | * a given location are uniquely named. |
| 20 | * |
| 21 | * @phpstan-import-type VisitorArray from Visitor |
| 22 | */ |
| 23 | class UniqueDirectivesPerLocation extends ValidationRule |
| 24 | { |
| 25 | /** @throws InvariantViolation */ |
| 26 | public function getVisitor(QueryValidationContext $context): array |
| 27 | { |
| 28 | return $this->getASTVisitor($context); |
| 29 | } |
| 30 | |
| 31 | /** @throws InvariantViolation */ |
| 32 | public function getSDLVisitor(SDLValidationContext $context): array |
| 33 | { |
| 34 | return $this->getASTVisitor($context); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @throws InvariantViolation |
| 39 | * |
| 40 | * @phpstan-return VisitorArray |
| 41 | */ |
| 42 | public function getASTVisitor(ValidationContext $context): array |
| 43 | { |
| 44 | /** @var array<string, true> $uniqueDirectiveMap */ |
| 45 | $uniqueDirectiveMap = []; |
| 46 | |
| 47 | $schema = $context->getSchema(); |
| 48 | $definedDirectives = $schema !== null |
| 49 | ? $schema->getDirectives() |
| 50 | : Directive::getInternalDirectives(); |
| 51 | foreach ($definedDirectives as $directive) { |
| 52 | if (! $directive->isRepeatable) { |
| 53 | $uniqueDirectiveMap[$directive->name] = true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | $astDefinitions = $context->getDocument()->definitions; |
| 58 | foreach ($astDefinitions as $definition) { |
| 59 | if ($definition instanceof DirectiveDefinitionNode |
| 60 | && ! $definition->repeatable |
| 61 | ) { |
| 62 | $uniqueDirectiveMap[$definition->name->value] = true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return [ |
| 67 | 'enter' => static function (Node $node) use ($uniqueDirectiveMap, $context): void { |
| 68 | if (! property_exists($node, 'directives')) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $knownDirectives = []; |
| 73 | |
| 74 | foreach ($node->directives as $directive) { |
| 75 | $directiveName = $directive->name->value; |
| 76 | |
| 77 | if (isset($uniqueDirectiveMap[$directiveName])) { |
| 78 | if (isset($knownDirectives[$directiveName])) { |
| 79 | $context->reportError(new Error( |
| 80 | static::duplicateDirectiveMessage($directiveName), |
| 81 | [$knownDirectives[$directiveName], $directive] |
| 82 | )); |
| 83 | } else { |
| 84 | $knownDirectives[$directiveName] = $directive; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | }, |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | public static function duplicateDirectiveMessage(string $directiveName): string |
| 93 | { |
| 94 | return "The directive \"{$directiveName}\" can only be used once at this location."; |
| 95 | } |
| 96 | } |
| 97 |