Optionally, a GraphQL type may be provided, which will be used to
disambiguate between value primitives.
| PHP Value | GraphQL Value |
| ------------- | -------------------- |
| Object | Input Object |
| Assoc Array | Input Object |
| Array | List |
| Boolean | Boolean |
| String | String / Enum Value |
| Int | Int |
| Float | Int / Float |
| Mixed | Enum Value |
| null | NullValue |
static public astFromValue ( $value, GraphQL\Type\Definition\InputType $type ) : |
||
$value | ||
$type | GraphQL\Type\Definition\InputType | |
Résultat |
/** * @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)); }
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 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)); }