woocommerce
/
lib
/
packages
/
GraphQL
/
Validator
/
Rules
/
ProvidedRequiredArgumentsOnDirectives.php
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
ProvidedRequiredArgumentsOnDirectives.php
123 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\AST\NonNullTypeNode; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Printer; |
| 12 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor; |
| 13 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Argument; |
| 14 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Directive; |
| 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 | * Provided required arguments on directives. |
| 21 | * |
| 22 | * A directive is only valid if all required (non-null without a |
| 23 | * default value) field arguments have been provided. |
| 24 | * |
| 25 | * @phpstan-import-type VisitorArray from Visitor |
| 26 | */ |
| 27 | class ProvidedRequiredArgumentsOnDirectives extends ValidationRule |
| 28 | { |
| 29 | public static function missingDirectiveArgMessage(string $directiveName, string $argName, string $type): string |
| 30 | { |
| 31 | return "Directive \"@{$directiveName}\" argument \"{$argName}\" of type \"{$type}\" is required but not provided."; |
| 32 | } |
| 33 | |
| 34 | /** @throws \Exception */ |
| 35 | public function getSDLVisitor(SDLValidationContext $context): array |
| 36 | { |
| 37 | return $this->getASTVisitor($context); |
| 38 | } |
| 39 | |
| 40 | /** @throws \Exception */ |
| 41 | public function getVisitor(QueryValidationContext $context): array |
| 42 | { |
| 43 | return $this->getASTVisitor($context); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @throws \Exception |
| 48 | * @throws \InvalidArgumentException |
| 49 | * @throws \ReflectionException |
| 50 | * @throws Error |
| 51 | * @throws InvariantViolation |
| 52 | * |
| 53 | * @phpstan-return VisitorArray |
| 54 | */ |
| 55 | public function getASTVisitor(ValidationContext $context): array |
| 56 | { |
| 57 | $requiredArgsMap = []; |
| 58 | $schema = $context->getSchema(); |
| 59 | $definedDirectives = $schema === null |
| 60 | ? Directive::getInternalDirectives() |
| 61 | : $schema->getDirectives(); |
| 62 | |
| 63 | foreach ($definedDirectives as $directive) { |
| 64 | $directiveArgs = []; |
| 65 | foreach ($directive->args as $arg) { |
| 66 | if ($arg->isRequired()) { |
| 67 | $directiveArgs[$arg->name] = $arg; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $requiredArgsMap[$directive->name] = $directiveArgs; |
| 72 | } |
| 73 | |
| 74 | $astDefinition = $context->getDocument()->definitions; |
| 75 | foreach ($astDefinition as $def) { |
| 76 | if ($def instanceof DirectiveDefinitionNode) { |
| 77 | $arguments = $def->arguments; |
| 78 | |
| 79 | $requiredArgs = []; |
| 80 | foreach ($arguments as $argument) { |
| 81 | if ($argument->type instanceof NonNullTypeNode && ! isset($argument->defaultValue)) { |
| 82 | $requiredArgs[$argument->name->value] = $argument; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $requiredArgsMap[$def->name->value] = $requiredArgs; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return [ |
| 91 | NodeKind::DIRECTIVE => [ |
| 92 | // Validate on leave to allow for deeper errors to appear first. |
| 93 | 'leave' => static function (DirectiveNode $directiveNode) use ($requiredArgsMap, $context): ?string { |
| 94 | $directiveName = $directiveNode->name->value; |
| 95 | $requiredArgs = $requiredArgsMap[$directiveName] ?? null; |
| 96 | if ($requiredArgs === null || $requiredArgs === []) { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | $argNodeMap = []; |
| 101 | foreach ($directiveNode->arguments as $arg) { |
| 102 | $argNodeMap[$arg->name->value] = $arg; |
| 103 | } |
| 104 | |
| 105 | foreach ($requiredArgs as $argName => $arg) { |
| 106 | if (! isset($argNodeMap[$argName])) { |
| 107 | $argType = $arg instanceof Argument |
| 108 | ? $arg->getType()->toString() |
| 109 | : Printer::doPrint($arg->type); |
| 110 | |
| 111 | $context->reportError( |
| 112 | new Error(static::missingDirectiveArgMessage($directiveName, $argName, $argType), [$directiveNode]) |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return null; |
| 118 | }, |
| 119 | ], |
| 120 | ]; |
| 121 | } |
| 122 | } |
| 123 |