Automattic\WooCommerce\Vendor\GraphQL\Utils

TypeInfo::enterpublicWC 1.0

Method of the class: TypeInfo{}

No Hooks.

Returns

null. Nothing (null).

Usage

$TypeInfo = new TypeInfo();
$TypeInfo->enter( $node ): void;
$node(Node) (required)
.

TypeInfo::enter() code WC 10.9.1

public function enter(Node $node): void
{
    $schema = $this->schema;

    // Note: many of the types below are explicitly typed as "mixed" to drop
    // any assumptions of a valid schema to ensure runtime types are properly
    // checked before continuing since TypeInfo is used as part of validation
    // which occurs before guarantees of schema and document validity.
    switch (true) {
        case $node instanceof SelectionSetNode:
            $namedType = Type::getNamedType($this->getType());
            $this->parentTypeStack[] = Type::isCompositeType($namedType) ? $namedType : null;
            break;

        case $node instanceof FieldNode:
            $parentType = $this->getParentType();

            $fieldDef = $parentType === null
                ? null
                : self::getFieldDefinition($schema, $parentType, $node);

            $fieldType = $fieldDef === null
                ? null
                : $fieldDef->getType();

            $this->fieldDefStack[] = $fieldDef;
            $this->typeStack[] = $fieldType;
            break;

        case $node instanceof DirectiveNode:
            $this->directive = $schema->getDirective($node->name->value);
            break;

        case $node instanceof OperationDefinitionNode:
            if ($node->operation === 'query') {
                $type = $schema->getQueryType();
            } elseif ($node->operation === 'mutation') {
                $type = $schema->getMutationType();
            } else {
                // Only other option
                $type = $schema->getSubscriptionType();
            }

            $this->typeStack[] = Type::isOutputType($type)
                ? $type
                : null;
            break;

        case $node instanceof InlineFragmentNode:
        case $node instanceof FragmentDefinitionNode:
            $typeConditionNode = $node->typeCondition;
            $outputType = $typeConditionNode === null
                ? Type::getNamedType($this->getType())
                : AST::typeFromAST([$schema, 'getType'], $typeConditionNode);
            $this->typeStack[] = Type::isOutputType($outputType) ? $outputType : null;
            break;

        case $node instanceof VariableDefinitionNode:
            $inputType = AST::typeFromAST([$schema, 'getType'], $node->type);
            $this->inputTypeStack[] = Type::isInputType($inputType) ? $inputType : null; // push
            break;

        case $node instanceof ArgumentNode:
            $fieldOrDirective = $this->getDirective() ?? $this->getFieldDef();
            $argDef = null;
            $argType = null;
            if ($fieldOrDirective !== null) {
                foreach ($fieldOrDirective->args as $arg) {
                    if ($arg->name === $node->name->value) {
                        $argDef = $arg;
                        $argType = $arg->getType();
                    }
                }
            }

            $this->argument = $argDef;
            $this->defaultValueStack[] = $argDef !== null && $argDef->defaultValueExists()
                ? $argDef->defaultValue
                : Utils::undefined();
            $this->inputTypeStack[] = Type::isInputType($argType) ? $argType : null;
            break;

        case $node instanceof ListValueNode:
            $type = $this->getInputType();
            $listType = $type instanceof NonNull
                ? $type->getWrappedType()
                : $type;
            $itemType = $listType instanceof ListOfType
                ? $listType->getWrappedType()
                : $listType;
            // List positions never have a default value.
            $this->defaultValueStack[] = Utils::undefined();
            $this->inputTypeStack[] = Type::isInputType($itemType) ? $itemType : null;
            break;

        case $node instanceof ObjectFieldNode:
            $objectType = Type::getNamedType($this->getInputType());
            $inputField = null;
            $inputFieldType = null;
            if ($objectType instanceof InputObjectType) {
                $tmp = $objectType->getFields();
                $inputField = $tmp[$node->name->value] ?? null;
                $inputFieldType = $inputField === null
                    ? null
                    : $inputField->getType();
            }

            $this->defaultValueStack[] = $inputField !== null && $inputField->defaultValueExists()
                ? $inputField->defaultValue
                : Utils::undefined();
            $this->inputTypeStack[] = Type::isInputType($inputFieldType)
                ? $inputFieldType
                : null;
            break;

        case $node instanceof EnumValueNode:
            $enumType = Type::getNamedType($this->getInputType());

            $this->enumValue = $enumType instanceof EnumType
                ? $enumType->getValue($node->value)
                : null;
            break;
    }
}