Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules
UniqueFieldDefinitionNames::getSDLVisitor
Method of the class: UniqueFieldDefinitionNames{}
No Hooks.
Returns
null. Nothing (null).
Usage
$UniqueFieldDefinitionNames = new UniqueFieldDefinitionNames(); $UniqueFieldDefinitionNames->getSDLVisitor( $context ): array;
- $context(SDLValidationContext) (required)
- .
UniqueFieldDefinitionNames::getSDLVisitor() UniqueFieldDefinitionNames::getSDLVisitor code WC 10.9.4
public function getSDLVisitor(SDLValidationContext $context): array
{
$schema = $context->getSchema();
/** @var array<string, array<int, NameNode>> $knownFieldNames */
$knownFieldNames = [];
$checkFieldUniqueness = static function ($node) use ($context, $schema, &$knownFieldNames): VisitorOperation {
assert(
$node instanceof InputObjectTypeDefinitionNode
|| $node instanceof InputObjectTypeExtensionNode
|| $node instanceof InterfaceTypeDefinitionNode
|| $node instanceof InterfaceTypeExtensionNode
|| $node instanceof ObjectTypeDefinitionNode
|| $node instanceof ObjectTypeExtensionNode
);
$typeName = $node->name->value;
$knownFieldNames[$typeName] ??= [];
$fieldNames = &$knownFieldNames[$typeName];
foreach ($node->fields as $fieldDef) {
$fieldName = $fieldDef->name->value;
$existingType = $schema !== null
? $schema->getType($typeName)
: null;
if (self::hasField($existingType, $fieldName)) {
$context->reportError(
new Error(
"Field \"{$typeName}.{$fieldName}\" already exists in the schema. It cannot also be defined in this type extension.",
$fieldDef->name,
),
);
} elseif (isset($fieldNames[$fieldName])) {
$context->reportError(
new Error(
"Field \"{$typeName}.{$fieldName}\" can only be defined once.",
[$fieldNames[$fieldName], $fieldDef->name],
),
);
} else {
$fieldNames[$fieldName] = $fieldDef->name;
}
}
return Visitor::skipNode();
};
return [
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $checkFieldUniqueness,
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => $checkFieldUniqueness,
NodeKind::INTERFACE_TYPE_DEFINITION => $checkFieldUniqueness,
NodeKind::INTERFACE_TYPE_EXTENSION => $checkFieldUniqueness,
NodeKind::OBJECT_TYPE_DEFINITION => $checkFieldUniqueness,
NodeKind::OBJECT_TYPE_EXTENSION => $checkFieldUniqueness,
];
}