public static function _inputValue() { if (!isset(self::$map['__InputValue'])) { self::$map['__InputValue'] = new ObjectType(['name' => '__InputValue', 'description' => 'Arguments provided to Fields or Directives and the input fields of an ' . 'InputObject are represented as Input Values which describe their type ' . 'and optionally a default value.', 'fields' => function () { return ['name' => ['type' => Type::nonNull(Type::string())], 'description' => ['type' => Type::string()], 'type' => ['type' => Type::nonNull(self::_type()), 'resolve' => function ($value) { return method_exists($value, 'getType') ? $value->getType() : $value->type; }], 'defaultValue' => ['type' => Type::string(), 'resolve' => function ($inputValue) { return $inputValue->defaultValue === null ? null : Printer::doPrint(AST::astFromValue($inputValue->defaultValue, $inputValue->getType())); }]]; }]); } return self::$map['__InputValue']; }
/** * @it converts input objects */ public function testConvertsInputObjects() { $inputObj = new InputObjectType(['name' => 'MyInputObj', 'fields' => ['foo' => Type::float(), 'bar' => $this->myEnum()]]); $expected = new ObjectValue(['fields' => [$this->objectField('foo', new IntValue(['value' => '3'])), $this->objectField('bar', new EnumValue(['value' => 'HELLO']))]]); $data = ['foo' => 3, 'bar' => 'HELLO']; $this->assertEquals($expected, AST::astFromValue($data, $inputObj)); $this->assertEquals($expected, AST::astFromValue((object) $data, $inputObj)); }
/** * @it converts input objects with explicit nulls */ public function testConvertsInputObjectsWithExplicitNulls() { $inputObj = new InputObjectType(['name' => 'MyInputObj', 'fields' => ['foo' => Type::float(), 'bar' => $this->myEnum()]]); $this->assertEquals(new ObjectValueNode(['fields' => [$this->objectField('foo', new NullValueNode([]))]]), AST::astFromValue(['foo' => null], $inputObj)); }
/** * @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); }
/** * 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]); }
/** * @it rejects empty input */ public function testRejectsEmptyInput() { $this->assertEquals(Utils::undefined(), AST::valueFromAST(null, Type::boolean())); }