Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

KnownDirectives::getASTVisitorpublicWC 1.0

Method of the class: KnownDirectives{}

No Hooks.

Returns

null. Nothing (null).

Usage

$KnownDirectives = new KnownDirectives();
$KnownDirectives->getASTVisitor( $context ): array;
$context(ValidationContext) (required)
.

KnownDirectives::getASTVisitor() code WC 10.9.1

public function getASTVisitor(ValidationContext $context): array
{
    $locationsMap = [];
    $schema = $context->getSchema();
    $definedDirectives = $schema === null
        ? Directive::getInternalDirectives()
        : $schema->getDirectives();

    foreach ($definedDirectives as $directive) {
        $locationsMap[$directive->name] = $directive->locations;
    }

    $astDefinition = $context->getDocument()->definitions;

    foreach ($astDefinition as $def) {
        if ($def instanceof DirectiveDefinitionNode) {
            $locationNames = [];
            foreach ($def->locations as $location) {
                $locationNames[] = $location->value;
            }

            $locationsMap[$def->name->value] = $locationNames;
        }
    }

    return [
        NodeKind::DIRECTIVE => function (
            DirectiveNode $node,
            $key,
            $parent,
            $path,
            $ancestors
        ) use (
            $context,
            $locationsMap
        ): void {
            $name = $node->name->value;
            $locations = $locationsMap[$name] ?? null;

            if ($locations === null) {
                $context->reportError(new Error(
                    static::unknownDirectiveMessage($name),
                    [$node]
                ));

                return;
            }

            $candidateLocation = $this->getDirectiveLocationForASTPath($ancestors);

            if ($candidateLocation === '' || in_array($candidateLocation, $locations, true)) {
                return;
            }

            $context->reportError(
                new Error(
                    static::misplacedDirectiveMessage($name, $candidateLocation),
                    [$node]
                )
            );
        },
    ];
}