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
ProvidedRequiredArguments.php
56 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\FieldNode; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Language\VisitorOperation; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext; |
| 11 | |
| 12 | class ProvidedRequiredArguments extends ValidationRule |
| 13 | { |
| 14 | /** @throws \Exception */ |
| 15 | public function getVisitor(QueryValidationContext $context): array |
| 16 | { |
| 17 | $providedRequiredArgumentsOnDirectives = new ProvidedRequiredArgumentsOnDirectives(); |
| 18 | |
| 19 | return $providedRequiredArgumentsOnDirectives->getVisitor($context) + [ |
| 20 | NodeKind::FIELD => [ |
| 21 | 'leave' => static function (FieldNode $fieldNode) use ($context): ?VisitorOperation { |
| 22 | $fieldDef = $context->getFieldDef(); |
| 23 | |
| 24 | if ($fieldDef === null) { |
| 25 | return Visitor::skipNode(); |
| 26 | } |
| 27 | |
| 28 | $argNodes = $fieldNode->arguments; |
| 29 | |
| 30 | $argNodeMap = []; |
| 31 | foreach ($argNodes as $argNode) { |
| 32 | $argNodeMap[$argNode->name->value] = $argNode; |
| 33 | } |
| 34 | |
| 35 | foreach ($fieldDef->args as $argDef) { |
| 36 | $argNode = $argNodeMap[$argDef->name] ?? null; |
| 37 | if ($argNode === null && $argDef->isRequired()) { |
| 38 | $context->reportError(new Error( |
| 39 | static::missingFieldArgMessage($fieldNode->name->value, $argDef->name, $argDef->getType()->toString()), |
| 40 | [$fieldNode] |
| 41 | )); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return null; |
| 46 | }, |
| 47 | ], |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | public static function missingFieldArgMessage(string $fieldName, string $argName, string $type): string |
| 52 | { |
| 53 | return "Field \"{$fieldName}\" argument \"{$argName}\" of type \"{$type}\" is required but not provided."; |
| 54 | } |
| 55 | } |
| 56 |