Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

VariablesInAllowedPosition::getVisitorpublicWC 1.0

Method of the class: VariablesInAllowedPosition{}

No Hooks.

Returns

null. Nothing (null).

Usage

$VariablesInAllowedPosition = new VariablesInAllowedPosition();
$VariablesInAllowedPosition->getVisitor( $context ): array;
$context(QueryValidationContext) (required)
.

VariablesInAllowedPosition::getVisitor() code WC 10.9.1

public function getVisitor(QueryValidationContext $context): array
{
    return [
        NodeKind::OPERATION_DEFINITION => [
            'enter' => function (): void {
                $this->varDefMap = [];
            },
            'leave' => function (OperationDefinitionNode $operation) use ($context): void {
                $usages = $context->getRecursiveVariableUsages($operation);

                foreach ($usages as $usage) {
                    $node = $usage['node'];
                    $type = $usage['type'];
                    $defaultValue = $usage['defaultValue'];
                    $varName = $node->name->value;
                    $varDef = $this->varDefMap[$varName] ?? null;

                    if ($varDef === null || $type === null) {
                        continue;
                    }

                    // A var type is allowed if it is the same or more strict (e.g. is
                    // a subtype of) than the expected type. It can be more strict if
                    // the variable type is non-null when the expected type is nullable.
                    // If both are list types, the variable item type can be more strict
                    // than the expected item type (contravariant).
                    $schema = $context->getSchema();
                    $varType = AST::typeFromAST([$schema, 'getType'], $varDef->type);

                    if ($varType !== null && ! $this->allowedVariableUsage($schema, $varType, $varDef->defaultValue, $type, $defaultValue)) {
                        $context->reportError(new Error(
                            static::badVarPosMessage($varName, $varType->toString(), $type->toString()),
                            [$varDef, $node]
                        ));
                    }
                }
            },
        ],
        NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $varDefNode): void {
            $this->varDefMap[$varDefNode->variable->name->value] = $varDefNode;
        },
    ];
}