Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

PossibleTypeExtensions::getSDLVisitorpublicWC 1.0

Method of the class: PossibleTypeExtensions{}

No Hooks.

Returns

null. Nothing (null).

Usage

$PossibleTypeExtensions = new PossibleTypeExtensions();
$PossibleTypeExtensions->getSDLVisitor( $context ): array;
$context(SDLValidationContext) (required)
.

PossibleTypeExtensions::getSDLVisitor() code WC 10.9.1

public function getSDLVisitor(SDLValidationContext $context): array
{
    $schema = $context->getSchema();

    /** @var array<string, TypeDefinitionNode&Node> $definedTypes */
    $definedTypes = [];
    foreach ($context->getDocument()->definitions as $def) {
        if ($def instanceof TypeDefinitionNode) {
            $name = $def->getName()->value;
            $definedTypes[$name] = $def;
        }
    }

    $checkTypeExtension = static function ($node) use ($context, $schema, &$definedTypes): ?VisitorOperation {
        $typeName = $node->name->value;
        $defNode = $definedTypes[$typeName] ?? null;
        $existingType = $schema !== null
            ? $schema->getType($typeName)
            : null;

        $expectedKind = null;
        if ($defNode !== null) {
            $expectedKind = self::defKindToExtKind($defNode->kind);
        } elseif ($existingType !== null) {
            $expectedKind = self::typeToExtKind($existingType);
        }

        if ($expectedKind !== null) {
            if ($expectedKind !== $node->kind) {
                $kindStr = self::extensionKindToTypeName($node->kind);
                $context->reportError(
                    new Error(
                        "Cannot extend non-{$kindStr} type \"{$typeName}\".",
                        $defNode !== null
                            ? [$defNode, $node]
                            : $node,
                    ),
                );
            }
        } else {
            $existingTypesMap = $schema !== null
                ? $schema->getTypeMap()
                : [];
            $allTypeNames = [
                ...array_keys($definedTypes),
                ...array_keys($existingTypesMap),
            ];
            $suggestedTypes = Utils::suggestionList($typeName, $allTypeNames);
            $didYouMean = $suggestedTypes === []
                ? ''
                : ' Did you mean ' . Utils::quotedOrList($suggestedTypes) . '?';
            $context->reportError(
                new Error(
                    "Cannot extend type \"{$typeName}\" because it is not defined.{$didYouMean}",
                    $node->name,
                ),
            );
        }

        return null;
    };

    return [
        NodeKind::SCALAR_TYPE_EXTENSION => $checkTypeExtension,
        NodeKind::OBJECT_TYPE_EXTENSION => $checkTypeExtension,
        NodeKind::INTERFACE_TYPE_EXTENSION => $checkTypeExtension,
        NodeKind::UNION_TYPE_EXTENSION => $checkTypeExtension,
        NodeKind::ENUM_TYPE_EXTENSION => $checkTypeExtension,
        NodeKind::INPUT_OBJECT_TYPE_EXTENSION => $checkTypeExtension,
    ];
}