Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules
ValuesOfCorrectType::getVisitor │ public │ WC 1.0
Method of the class: ValuesOfCorrectType{}
No Hooks.
Returns
null. Nothing (null).
Usage
$ValuesOfCorrectType = new ValuesOfCorrectType(); $ValuesOfCorrectType->getVisitor( $context ): array;
- $context(QueryValidationContext) (required)
- .
ValuesOfCorrectType::getVisitor() ValuesOfCorrectType::getVisitor code WC 10.9.1
public function getVisitor(QueryValidationContext $context): array
{
return [
NodeKind::NULL => static function (NullValueNode $node) use ($context): void {
$type = $context->getInputType();
if ($type instanceof NonNull) {
$typeStr = Utils::printSafe($type);
$nodeStr = Printer::doPrint($node);
$context->reportError(
new Error(
"Expected value of type \"{$typeStr}\", found {$nodeStr}.",
$node
)
);
}
},
NodeKind::LST => function (ListValueNode $node) use ($context): ?VisitorOperation {
// Note: TypeInfo will traverse into a list's item type, so look to the
// parent input type to check if it is a list.
$parentType = $context->getParentInputType();
$type = $parentType === null
? null
: Type::getNullableType($parentType);
if (! $type instanceof ListOfType) {
$this->isValidValueNode($context, $node);
return Visitor::skipNode();
}
return null;
},
NodeKind::OBJECT => function (ObjectValueNode $node) use ($context): ?VisitorOperation {
$type = Type::getNamedType($context->getInputType());
if (! $type instanceof InputObjectType) {
$this->isValidValueNode($context, $node);
return Visitor::skipNode();
}
// Ensure every required field exists.
$inputFields = $type->getFields();
$fieldNodeMap = [];
foreach ($node->fields as $field) {
$fieldNodeMap[$field->name->value] = $field;
}
foreach ($inputFields as $inputFieldName => $fieldDef) {
if (! isset($fieldNodeMap[$inputFieldName]) && $fieldDef->isRequired()) {
$fieldType = Utils::printSafe($fieldDef->getType());
$context->reportError(
new Error(
"Field {$type->name}.{$inputFieldName} of required type {$fieldType} was not provided.",
$node
)
);
}
}
return null;
},
NodeKind::OBJECT_FIELD => static function (ObjectFieldNode $node) use ($context): void {
$parentType = Type::getNamedType($context->getParentInputType());
if (! $parentType instanceof InputObjectType) {
return;
}
if ($context->getInputType() !== null) {
return;
}
$suggestions = Utils::suggestionList(
$node->name->value,
array_keys($parentType->getFields())
);
$didYouMean = $suggestions === []
? null
: ' Did you mean ' . Utils::quotedOrList($suggestions) . '?';
$context->reportError(
new Error(
"Field \"{$node->name->value}\" is not defined by type \"{$parentType->name}\".{$didYouMean}",
$node
)
);
},
NodeKind::ENUM => function (EnumValueNode $node) use ($context): void {
$this->isValidValueNode($context, $node);
},
NodeKind::INT => function (IntValueNode $node) use ($context): void {
$this->isValidValueNode($context, $node);
},
NodeKind::FLOAT => function (FloatValueNode $node) use ($context): void {
$this->isValidValueNode($context, $node);
},
NodeKind::STRING => function (StringValueNode $node) use ($context): void {
$this->isValidValueNode($context, $node);
},
NodeKind::BOOLEAN => function (BooleanValueNode $node) use ($context): void {
$this->isValidValueNode($context, $node);
},
];
}