GraphQL\Utils\TypeInfo::enter PHP Method

enter() public method

public enter ( Node $node )
$node GraphQL\Language\AST\Node
    function enter(Node $node)
    {
        $schema = $this->schema;
        switch ($node->kind) {
            case NodeKind::SELECTION_SET:
                $namedType = Type::getNamedType($this->getType());
                $compositeType = null;
                if (Type::isCompositeType($namedType)) {
                    // isCompositeType is a type refining predicate, so this is safe.
                    $compositeType = $namedType;
                }
                $this->parentTypeStack[] = $compositeType;
                // push
                break;
            case NodeKind::FIELD:
                $parentType = $this->getParentType();
                $fieldDef = null;
                if ($parentType) {
                    $fieldDef = self::getFieldDefinition($schema, $parentType, $node);
                }
                $this->fieldDefStack[] = $fieldDef;
                // push
                $this->typeStack[] = $fieldDef ? $fieldDef->getType() : null;
                // push
                break;
            case NodeKind::DIRECTIVE:
                $this->directive = $schema->getDirective($node->name->value);
                break;
            case NodeKind::OPERATION_DEFINITION:
                $type = null;
                if ($node->operation === 'query') {
                    $type = $schema->getQueryType();
                } else {
                    if ($node->operation === 'mutation') {
                        $type = $schema->getMutationType();
                    } else {
                        if ($node->operation === 'subscription') {
                            $type = $schema->getSubscriptionType();
                        }
                    }
                }
                $this->typeStack[] = $type;
                // push
                break;
            case NodeKind::INLINE_FRAGMENT:
            case NodeKind::FRAGMENT_DEFINITION:
                $typeConditionNode = $node->typeCondition;
                $outputType = $typeConditionNode ? self::typeFromAST($schema, $typeConditionNode) : $this->getType();
                $this->typeStack[] = $outputType;
                // push
                break;
            case NodeKind::VARIABLE_DEFINITION:
                $inputType = self::typeFromAST($schema, $node->type);
                $this->inputTypeStack[] = $inputType;
                // push
                break;
            case NodeKind::ARGUMENT:
                $fieldOrDirective = $this->getDirective() ?: $this->getFieldDef();
                $argDef = $argType = null;
                if ($fieldOrDirective) {
                    $argDef = Utils::find($fieldOrDirective->args, function ($arg) use($node) {
                        return $arg->name === $node->name->value;
                    });
                    if ($argDef) {
                        $argType = $argDef->getType();
                    }
                }
                $this->argument = $argDef;
                $this->inputTypeStack[] = $argType;
                // push
                break;
            case NodeKind::LST:
                $listType = Type::getNullableType($this->getInputType());
                $this->inputTypeStack[] = $listType instanceof ListOfType ? $listType->getWrappedType() : null;
                // push
                break;
            case NodeKind::OBJECT_FIELD:
                $objectType = Type::getNamedType($this->getInputType());
                $fieldType = null;
                if ($objectType instanceof InputObjectType) {
                    $tmp = $objectType->getFields();
                    $inputField = isset($tmp[$node->name->value]) ? $tmp[$node->name->value] : null;
                    $fieldType = $inputField ? $inputField->getType() : null;
                }
                $this->inputTypeStack[] = $fieldType;
                break;
        }
    }

Usage Example

Example #1
0
 /**
  * Creates a new visitor instance which maintains a provided TypeInfo instance
  * along with visiting visitor.
  */
 static function visitWithTypeInfo(TypeInfo $typeInfo, $visitor)
 {
     return ['enter' => function ($node) use($typeInfo, $visitor) {
         $typeInfo->enter($node);
         $fn = self::getVisitFn($visitor, $node->kind, false);
         if ($fn) {
             $result = call_user_func_array($fn, func_get_args());
             if ($result) {
                 $typeInfo->leave($node);
                 if ($result instanceof Node) {
                     $typeInfo->enter($result);
                 }
             }
             return $result;
         }
         return null;
     }, 'leave' => function ($node) use($typeInfo, $visitor) {
         $fn = self::getVisitFn($visitor, $node->kind, true);
         $result = $fn ? call_user_func_array($fn, func_get_args()) : null;
         $typeInfo->leave($node);
         return $result;
     }];
 }
All Usage Examples Of GraphQL\Utils\TypeInfo::enter