GraphQL\Executor\Values::valueFromAST PHP Метод

valueFromAST() публичный статический Метод

Устаревший: as of 8.0 (Moved to Utils\AST::valueFromAST)
public static valueFromAST ( $valueNode, GraphQL\Type\Definition\InputType $type, null $variables = null ) : array | null | stdClass
$valueNode
$type GraphQL\Type\Definition\InputType
$variables null
Результат array | null | stdClass
    public static function valueFromAST($valueNode, InputType $type, $variables = null)
    {
        return Utils\AST::valueFromAST($valueNode, $type, $variables);
    }

Usage Example

Пример #1
0
 public static function valueFromAST($valueAST, InputType $type, $variables = null)
 {
     if ($type instanceof NonNull) {
         return self::valueFromAST($valueAST, $type->getWrappedType(), $variables);
     }
     if (!$valueAST) {
         return null;
     }
     if ($valueAST instanceof Variable) {
         $variableName = $valueAST->name->value;
         if (!$variables || !isset($variables[$variableName])) {
             return null;
         }
         return $variables[$variableName];
     }
     if ($type instanceof ListOfType) {
         $itemType = $type->getWrappedType();
         if ($valueAST instanceof ListValue) {
             return array_map(function ($itemAST) use($itemType, $variables) {
                 return Values::valueFromAST($itemAST, $itemType, $variables);
             }, $valueAST->values);
         } else {
             return [self::valueFromAST($valueAST, $itemType, $variables)];
         }
     }
     if ($type instanceof InputObjectType) {
         $fields = $type->getFields();
         if (!$valueAST instanceof ObjectValue) {
             return null;
         }
         $fieldASTs = Utils::keyMap($valueAST->fields, function ($field) {
             return $field->name->value;
         });
         $values = [];
         foreach ($fields as $field) {
             $fieldAST = isset($fieldASTs[$field->name]) ? $fieldASTs[$field->name] : null;
             $fieldValue = self::valueFromAST($fieldAST ? $fieldAST->value : null, $field->getType(), $variables);
             if (null === $fieldValue) {
                 $fieldValue = $field->defaultValue;
             }
             if (null !== $fieldValue) {
                 $values[$field->name] = $fieldValue;
             }
         }
         return $values;
     }
     Utils::invariant($type instanceof ScalarType || $type instanceof EnumType, 'Must be input type');
     return $type->parseLiteral($valueAST);
 }