Automattic\WooCommerce\Vendor\GraphQL\Type

SchemaValidationContext::validateFieldsprivateWC 1.0

Method of the class: SchemaValidationContext{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->validateFields( $type ): void;
$type(ObjectType|InterfaceType) (required)
.

SchemaValidationContext::validateFields() code WC 10.9.1

private function validateFields(Type $type): void
{
    $fieldMap = $type->getFields();

    if ($fieldMap === []) {
        $this->reportError(
            "Type {$type->name} must define one or more fields.",
            $this->getAllNodes($type)
        );
    }

    foreach ($fieldMap as $fieldName => $field) {
        $this->validateName($field);

        $fieldNodes = $this->getAllFieldNodes($type, $fieldName);
        if (count($fieldNodes) > 1) {
            $this->reportError("Field {$type->name}.{$fieldName} can only be defined once.", $fieldNodes);
            continue;
        }

        $fieldType = $field->getType();
        // @phpstan-ignore-next-line not statically provable until we can use union types
        if (! Type::isOutputType($fieldType)) {
            $safeFieldType = Utils::printSafe($fieldType);
            $this->reportError(
                "The type of {$type->name}.{$fieldName} must be Output Type but got: {$safeFieldType}.",
                $this->getFieldTypeNode($type, $fieldName)
            );
        }

        $this->validateTypeIsSingleton($fieldType, "{$type->name}.{$fieldName}");

        $argNames = [];
        foreach ($field->args as $arg) {
            $argName = $arg->name;
            $argPath = "{$type->name}.{$fieldName}({$argName}:)";

            $this->validateName($arg);

            if (isset($argNames[$argName])) {
                $this->reportError(
                    "Field argument {$argPath} can only be defined once.",
                    $this->getAllFieldArgNodes($type, $fieldName, $argName)
                );
            }

            $argNames[$argName] = true;

            $argType = $arg->getType();

            // @phpstan-ignore-next-line the type of $arg->getType() says it is an input type, but it might not always be true
            if (! Type::isInputType($argType)) {
                $safeType = Utils::printSafe($argType);
                $this->reportError(
                    "The type of {$argPath} must be Input Type but got: {$safeType}.",
                    $this->getFieldArgTypeNode($type, $fieldName, $argName)
                );
            }

            $this->validateTypeIsSingleton($argType, $argPath);

            if (isset($arg->astNode->directives)) {
                $this->validateDirectivesAtLocation($arg->astNode->directives, DirectiveLocation::ARGUMENT_DEFINITION);
            }
        }

        if (isset($field->astNode->directives)) {
            $this->validateDirectivesAtLocation($field->astNode->directives, DirectiveLocation::FIELD_DEFINITION);
        }
    }
}