Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules
ScalarLeafs{}└─ ValidationRule
No Hooks.
Usage
$ScalarLeafs = new ScalarLeafs(); // use class methods
Methods
- public getVisitor(QueryValidationContext $context)
- public static noSubselectionAllowedMessage(string $field, string $type)
- public static requiredSubselectionMessage(string $field, string $type)
ScalarLeafs{} ScalarLeafs{} code WC 10.9.1
class ScalarLeafs extends ValidationRule
{
public function getVisitor(QueryValidationContext $context): array
{
return [
NodeKind::FIELD => static function (FieldNode $node) use ($context): void {
$type = $context->getType();
if ($type === null) {
return;
}
if (Type::isLeafType(Type::getNamedType($type))) {
if ($node->selectionSet !== null) {
$context->reportError(new Error(
static::noSubselectionAllowedMessage($node->name->value, $type->toString()),
[$node->selectionSet]
));
}
} elseif ($node->selectionSet === null) {
$context->reportError(new Error(
static::requiredSubselectionMessage($node->name->value, $type->toString()),
[$node]
));
}
},
];
}
public static function noSubselectionAllowedMessage(string $field, string $type): string
{
return "Field \"{$field}\" of type \"{$type}\" must not have a sub selection.";
}
public static function requiredSubselectionMessage(string $field, string $type): string
{
return "Field \"{$field}\" of type \"{$type}\" must have a sub selection.";
}
}