A GraphQL type must be provided, which will be used to interpret different
GraphQL Value literals.
Returns null when the value could not be validly coerced according to
the provided type.
| GraphQL Value | PHP Value |
| -------------------- | ------------- |
| Input Object | Assoc Array |
| List | Array |
| Boolean | Boolean |
| String | String |
| Int / Float | Int / Float |
| Enum Value | Mixed |
| Null Value | stdClass | instance of NullValue::getNullValue()
/** * Given a variable definition, and any value of input, return a value which * adheres to the variable definition, or throw an error. */ private static function getVariableValue(Schema $schema, VariableDefinition $definitionAST, $input) { $type = Utils\TypeInfo::typeFromAST($schema, $definitionAST->type); $variable = $definitionAST->variable; if (!$type || !Type::isInputType($type)) { $printed = Printer::doPrint($definitionAST->type); throw new Error("Variable \"\${$variable->name->value}\" expected value of type " . "\"{$printed}\" which cannot be used as an input type.", [$definitionAST]); } $inputType = $type; $errors = self::isValidPHPValue($input, $inputType); if (empty($errors)) { if (null === $input) { $defaultValue = $definitionAST->defaultValue; if ($defaultValue) { return Utils\AST::valueFromAST($defaultValue, $inputType); } } return self::coerceValue($inputType, $input); } if (null === $input) { $printed = Printer::doPrint($definitionAST->type); throw new Error("Variable \"\${$variable->name->value}\" of required type " . "\"{$printed}\" was not provided.", [$definitionAST]); } $message = $errors ? "\n" . implode("\n", $errors) : ''; $val = json_encode($input); throw new Error("Variable \"\${$variable->name->value}\" got invalid value " . "{$val}.{$message}", [$definitionAST]); }
/** * @deprecated as of 8.0 (Moved to Utils\AST::valueFromAST) * * @param $valueNode * @param InputType $type * @param null $variables * @return array|null|\stdClass */ public static function valueFromAST($valueNode, InputType $type, $variables = null) { return Utils\AST::valueFromAST($valueNode, $type, $variables); }
/** * @it rejects empty input */ public function testRejectsEmptyInput() { $this->assertEquals(Utils::undefined(), AST::valueFromAST(null, Type::boolean())); }