Automattic\WooCommerce\Vendor\GraphQL\Type

SchemaValidationContext::validateDirectiveDefinitionspublicWC 1.0

Method of the class: SchemaValidationContext{}

No Hooks.

Returns

null. Nothing (null).

Usage

$SchemaValidationContext = new SchemaValidationContext();
$SchemaValidationContext->validateDirectiveDefinitions(): void;

SchemaValidationContext::validateDirectiveDefinitions() code WC 10.9.1

public function validateDirectiveDefinitions(): void
{
    $directiveDefinitions = [];

    $directives = $this->schema->getDirectives();
    foreach ($directives as $directive) {
        // Ensure all directives are in fact Automattic\WooCommerce\Vendor\GraphQL directives.
        // @phpstan-ignore-next-line The generic type says this should not happen, but a user may use it wrong nonetheless
        if (! $directive instanceof Directive) {
            $notDirective = Utils::printSafe($directive);
            // @phpstan-ignore-next-line The generic type says this should not happen, but a user may use it wrong nonetheless
            $nodes = is_object($directive) && property_exists($directive, 'astNode')
                ? $directive->astNode
                : null;

            $this->reportError(
                "Expected directive but got: {$notDirective}.",
                $nodes
            );
            continue;
        }

        $existingDefinitions = $directiveDefinitions[$directive->name] ?? [];
        $existingDefinitions[] = $directive;
        $directiveDefinitions[$directive->name] = $existingDefinitions;

        // Ensure they are named correctly.
        $this->validateName($directive);

        // TODO: Ensure proper locations.

        $argNames = [];
        foreach ($directive->args as $arg) {
            // Ensure they are named correctly.
            $this->validateName($arg);

            $argName = $arg->name;

            if (isset($argNames[$argName])) {
                $this->reportError(
                    "Argument @{$directive->name}({$argName}:) can only be defined once.",
                    $this->getAllDirectiveArgNodes($directive, $argName)
                );
                continue;
            }

            $argNames[$argName] = true;

            // Ensure the type is an input type.
            // @phpstan-ignore-next-line necessary until PHP supports union types
            if (! Type::isInputType($arg->getType())) {
                $type = Utils::printSafe($arg->getType());
                $this->reportError(
                    "The type of @{$directive->name}({$argName}:) must be Input Type but got: {$type}.",
                    $this->getDirectiveArgTypeNode($directive, $argName)
                );
            }
        }
    }

    foreach ($directiveDefinitions as $directiveName => $directiveList) {
        if (count($directiveList) > 1) {
            $nodes = [];
            foreach ($directiveList as $dir) {
                if (isset($dir->astNode)) {
                    $nodes[] = $dir->astNode;
                }
            }

            $this->reportError(
                "Directive @{$directiveName} defined multiple times.",
                $nodes
            );
        }
    }
}