Automattic\WooCommerce\Vendor\GraphQL\Type\Validation

InputObjectCircularRefs::validatepublicWC 1.0

This does a straight-forward DFS to find cycles. It does not terminate when a cycle was found but continues to explore the graph to find all possible cycles.

Method of the class: InputObjectCircularRefs{}

No Hooks.

Returns

null. Nothing (null).

Usage

$InputObjectCircularRefs = new InputObjectCircularRefs();
$InputObjectCircularRefs->validate( $inputObj ): void;
$inputObj(InputObjectType) (required)
.

InputObjectCircularRefs::validate() code WC 10.9.1

public function validate(InputObjectType $inputObj): void
{
    if (isset($this->visitedTypes[$inputObj->name])) {
        return;
    }

    $this->visitedTypes[$inputObj->name] = true;
    $this->fieldPathIndexByTypeName[$inputObj->name] = count($this->fieldPath);

    $fieldMap = $inputObj->getFields();
    foreach ($fieldMap as $field) {
        $type = $field->getType();

        if ($type instanceof NonNull) {
            $fieldType = $type->getWrappedType();

            // If the type of the field is anything else then a non-nullable input object,
            // there is no chance of an unbreakable cycle
            if ($fieldType instanceof InputObjectType) {
                $this->fieldPath[] = $field;

                if (! isset($this->fieldPathIndexByTypeName[$fieldType->name])) {
                    $this->validate($fieldType);
                } else {
                    $cycleIndex = $this->fieldPathIndexByTypeName[$fieldType->name];
                    $cyclePath = array_slice($this->fieldPath, $cycleIndex);
                    $fieldNames = implode(
                        '.',
                        array_map(
                            static fn (InputObjectField $field): string => $field->name,
                            $cyclePath
                        )
                    );
                    $fieldNodes = array_map(
                        static fn (InputObjectField $field): ?InputValueDefinitionNode => $field->astNode,
                        $cyclePath
                    );

                    $this->schemaValidationContext->reportError(
                        "Cannot reference Input Object \"{$fieldType->name}\" within itself through a series of non-null fields: \"{$fieldNames}\".",
                        $fieldNodes
                    );
                }
            }
        }

        array_pop($this->fieldPath);
    }

    unset($this->fieldPathIndexByTypeName[$inputObj->name]);
}