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
UniqueFieldDefinitionNames.php
100 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\InputObjectTypeDefinitionNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InputObjectTypeExtensionNode; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InterfaceTypeDefinitionNode; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InterfaceTypeExtensionNode; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NameNode; |
| 12 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 13 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\ObjectTypeDefinitionNode; |
| 14 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\ObjectTypeExtensionNode; |
| 15 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor; |
| 16 | use Automattic\WooCommerce\Vendor\GraphQL\Language\VisitorOperation; |
| 17 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\InputObjectType; |
| 18 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\InterfaceType; |
| 19 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\NamedType; |
| 20 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\ObjectType; |
| 21 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\SDLValidationContext; |
| 22 | |
| 23 | /** |
| 24 | * Unique field definition names. |
| 25 | * |
| 26 | * A Automattic\WooCommerce\Vendor\GraphQL complex type is only valid if all its fields are uniquely named. |
| 27 | */ |
| 28 | class UniqueFieldDefinitionNames extends ValidationRule |
| 29 | { |
| 30 | public function getSDLVisitor(SDLValidationContext $context): array |
| 31 | { |
| 32 | $schema = $context->getSchema(); |
| 33 | |
| 34 | /** @var array<string, array<int, NameNode>> $knownFieldNames */ |
| 35 | $knownFieldNames = []; |
| 36 | |
| 37 | $checkFieldUniqueness = static function ($node) use ($context, $schema, &$knownFieldNames): VisitorOperation { |
| 38 | assert( |
| 39 | $node instanceof InputObjectTypeDefinitionNode |
| 40 | || $node instanceof InputObjectTypeExtensionNode |
| 41 | || $node instanceof InterfaceTypeDefinitionNode |
| 42 | || $node instanceof InterfaceTypeExtensionNode |
| 43 | || $node instanceof ObjectTypeDefinitionNode |
| 44 | || $node instanceof ObjectTypeExtensionNode |
| 45 | ); |
| 46 | |
| 47 | $typeName = $node->name->value; |
| 48 | |
| 49 | $knownFieldNames[$typeName] ??= []; |
| 50 | $fieldNames = &$knownFieldNames[$typeName]; |
| 51 | |
| 52 | foreach ($node->fields as $fieldDef) { |
| 53 | $fieldName = $fieldDef->name->value; |
| 54 | |
| 55 | $existingType = $schema !== null |
| 56 | ? $schema->getType($typeName) |
| 57 | : null; |
| 58 | if (self::hasField($existingType, $fieldName)) { |
| 59 | $context->reportError( |
| 60 | new Error( |
| 61 | "Field \"{$typeName}.{$fieldName}\" already exists in the schema. It cannot also be defined in this type extension.", |
| 62 | $fieldDef->name, |
| 63 | ), |
| 64 | ); |
| 65 | } elseif (isset($fieldNames[$fieldName])) { |
| 66 | $context->reportError( |
| 67 | new Error( |
| 68 | "Field \"{$typeName}.{$fieldName}\" can only be defined once.", |
| 69 | [$fieldNames[$fieldName], $fieldDef->name], |
| 70 | ), |
| 71 | ); |
| 72 | } else { |
| 73 | $fieldNames[$fieldName] = $fieldDef->name; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return Visitor::skipNode(); |
| 78 | }; |
| 79 | |
| 80 | return [ |
| 81 | NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $checkFieldUniqueness, |
| 82 | NodeKind::INPUT_OBJECT_TYPE_EXTENSION => $checkFieldUniqueness, |
| 83 | NodeKind::INTERFACE_TYPE_DEFINITION => $checkFieldUniqueness, |
| 84 | NodeKind::INTERFACE_TYPE_EXTENSION => $checkFieldUniqueness, |
| 85 | NodeKind::OBJECT_TYPE_DEFINITION => $checkFieldUniqueness, |
| 86 | NodeKind::OBJECT_TYPE_EXTENSION => $checkFieldUniqueness, |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | /** @throws InvariantViolation */ |
| 91 | private static function hasField(?NamedType $type, string $fieldName): bool |
| 92 | { |
| 93 | if ($type instanceof ObjectType || $type instanceof InterfaceType || $type instanceof InputObjectType) { |
| 94 | return $type->hasField($fieldName); |
| 95 | } |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 |