/** * @param string $name * @return Type */ public function resolveType($name) { if (preg_match('#^(.+)\\!$#', $name, $regs)) { return Type::nonNull($this->resolveType($regs[1])); } if (preg_match('#^\\[(.+)\\]$#', $name, $regs)) { return Type::listOf($this->resolveType($regs[1])); } switch ($name) { case Type::INT: return Type::int(); case Type::STRING: return Type::string(); case Type::BOOLEAN: return Type::boolean(); case Type::FLOAT: return Type::float(); case Type::ID: return Type::id(); default: if (!isset($this->types[$name])) { throw new \InvalidArgumentException(sprintf('Type "%s" is not defined', $name)); } return $this->types[$name]; } }
/** * @it serializes output float */ public function testSerializesOutputFloat() { $floatType = Type::float(); $this->assertSame(1.0, $floatType->serialize(1)); $this->assertSame(0.0, $floatType->serialize(0)); $this->assertSame(123.5, $floatType->serialize('123.5')); $this->assertSame(-1.0, $floatType->serialize(-1)); $this->assertSame(0.1, $floatType->serialize(0.1)); $this->assertSame(1.1, $floatType->serialize(1.1)); $this->assertSame(-1.1, $floatType->serialize(-1.1)); $this->assertSame(-1.1, $floatType->serialize('-1.1')); try { $floatType->serialize('one'); $this->fail('Expected exception was not thrown'); } catch (InvariantViolation $e) { $this->assertEquals('Float cannot represent non numeric value: one', $e->getMessage()); } try { $floatType->serialize(''); $this->fail('Expected exception was not thrown'); } catch (InvariantViolation $e) { $this->assertEquals('Float cannot represent non numeric value: (empty string)', $e->getMessage()); } $this->assertSame(0.0, $floatType->serialize(false)); $this->assertSame(1.0, $floatType->serialize(true)); }
/** * @return Schema */ protected function getDefaultSchema() { $Being = new InterfaceType(['name' => 'Being', 'fields' => ['name' => ['type' => Type::string()]]]); $Pet = new InterfaceType(['name' => 'Pet', 'fields' => ['name' => ['type' => Type::string()]]]); $DogCommand = new EnumType(['name' => 'DogCommand', 'values' => ['SIT' => ['value' => 0], 'HEEL' => ['value' => 1], 'DOWN' => ['value' => 3]]]); $Dog = new ObjectType(['name' => 'Dog', 'fields' => ['name' => ['type' => Type::string()], 'nickname' => ['type' => Type::string()], 'barkVolume' => ['type' => Type::int()], 'barks' => ['type' => Type::boolean()], 'doesKnowCommand' => ['type' => Type::boolean(), 'args' => ['dogCommand' => ['type' => $DogCommand]]], 'isHousetrained' => ['type' => Type::boolean(), 'args' => ['atOtherHomes' => ['type' => Type::boolean(), 'defaultValue' => true]]], 'isAtLocation' => ['type' => Type::boolean(), 'args' => ['x' => ['type' => Type::int()], 'y' => ['type' => Type::int()]]]], 'interfaces' => [$Being, $Pet]]); $FurColor = new EnumType(['name' => 'FurColor', 'values' => ['BROWN' => ['value' => 0], 'BLACK' => ['value' => 1], 'TAN' => ['value' => 2], 'SPOTTED' => ['value' => 3]]]); $Cat = new ObjectType(['name' => 'Cat', 'fields' => ['name' => ['type' => Type::string()], 'nickname' => ['type' => Type::string()], 'meows' => ['type' => Type::boolean()], 'meowVolume' => ['type' => Type::int()], 'furColor' => ['type' => $FurColor]], 'interfaces' => [$Being, $Pet]]); $CatOrDog = new UnionType(['name' => 'CatOrDog', 'types' => [$Dog, $Cat], 'resolveType' => function ($value) { // not used for validation return null; }]); $Intelligent = new InterfaceType(['name' => 'Intelligent', 'fields' => ['iq' => ['type' => Type::int()]]]); $Human = $this->humanType = new ObjectType(['name' => 'Human', 'interfaces' => [$Being, $Intelligent], 'fields' => ['name' => ['args' => ['surname' => ['type' => Type::boolean()]], 'type' => Type::string()], 'pets' => ['type' => Type::listOf($Pet)], 'relatives' => ['type' => function () { return Type::listOf($this->humanType); }]]]); $Alien = new ObjectType(['name' => 'Alien', 'interfaces' => [$Being, $Intelligent], 'fields' => ['iq' => ['type' => Type::int()], 'numEyes' => ['type' => Type::int()]]]); $DogOrHuman = new UnionType(['name' => 'DogOrHuman', 'types' => [$Dog, $Human], 'resolveType' => function () { // not used for validation return null; }]); $HumanOrAlien = new UnionType(['name' => 'HumanOrAlien', 'types' => [$Human, $Alien], 'resolveType' => function () { // not used for validation return null; }]); $ComplexInput = new InputObjectType(['name' => 'ComplexInput', 'fields' => ['requiredField' => ['type' => Type::nonNull(Type::boolean())], 'intField' => ['type' => Type::int()], 'stringField' => ['type' => Type::string()], 'booleanField' => ['type' => Type::boolean()], 'stringListField' => ['type' => Type::listOf(Type::string())]]]); $ComplicatedArgs = new ObjectType(['name' => 'ComplicatedArgs', 'fields' => ['intArgField' => ['type' => Type::string(), 'args' => ['intArg' => ['type' => Type::int()]]], 'nonNullIntArgField' => ['type' => Type::string(), 'args' => ['nonNullIntArg' => ['type' => Type::nonNull(Type::int())]]], 'stringArgField' => ['type' => Type::string(), 'args' => ['stringArg' => ['type' => Type::string()]]], 'booleanArgField' => ['type' => Type::string(), 'args' => ['booleanArg' => ['type' => Type::boolean()]]], 'enumArgField' => ['type' => Type::string(), 'args' => ['enumArg' => ['type' => $FurColor]]], 'floatArgField' => ['type' => Type::string(), 'args' => ['floatArg' => ['type' => Type::float()]]], 'idArgField' => ['type' => Type::string(), 'args' => ['idArg' => ['type' => Type::id()]]], 'stringListArgField' => ['type' => Type::string(), 'args' => ['stringListArg' => ['type' => Type::listOf(Type::string())]]], 'complexArgField' => ['type' => Type::string(), 'args' => ['complexArg' => ['type' => $ComplexInput]]], 'multipleReqs' => ['type' => Type::string(), 'args' => ['req1' => ['type' => Type::nonNull(Type::int())], 'req2' => ['type' => Type::nonNull(Type::int())]]], 'multipleOpts' => ['type' => Type::string(), 'args' => ['opt1' => ['type' => Type::int(), 'defaultValue' => 0], 'opt2' => ['type' => Type::int(), 'defaultValue' => 0]]], 'multipleOptAndReq' => ['type' => Type::string(), 'args' => ['req1' => ['type' => Type::nonNull(Type::int())], 'req2' => ['type' => Type::nonNull(Type::int())], 'opt1' => ['type' => Type::int(), 'defaultValue' => 0], 'opt2' => ['type' => Type::int(), 'defaultValue' => 0]]]]]); $queryRoot = new ObjectType(['name' => 'QueryRoot', 'fields' => ['human' => ['args' => ['id' => ['type' => Type::id()]], 'type' => $Human], 'alien' => ['type' => $Alien], 'dog' => ['type' => $Dog], 'cat' => ['type' => $Cat], 'pet' => ['type' => $Pet], 'catOrDog' => ['type' => $CatOrDog], 'dogOrHuman' => ['type' => $DogOrHuman], 'humanOrAlien' => ['type' => $HumanOrAlien], 'complicatedArgs' => ['type' => $ComplicatedArgs]]]); $defaultSchema = new Schema($queryRoot); return $defaultSchema; }
public function testCoercesOutputFloat() { $floatType = Type::float(); $this->assertSame(1.0, $floatType->coerce(1)); $this->assertSame(-1.0, $floatType->coerce(-1)); $this->assertSame(0.1, $floatType->coerce(0.1)); $this->assertSame(1.1, $floatType->coerce(1.1)); $this->assertSame(-1.1, $floatType->coerce(-1.1)); $this->assertSame(null, $floatType->coerce('one')); $this->assertSame(0.0, $floatType->coerce(false)); $this->assertSame(1.0, $floatType->coerce(true)); }
/** * @it does not convert when input coercion rules reject a value */ public function testDoesNotConvertWhenInputCoercionRulesRejectAValue() { $undefined = Utils::undefined(); $this->runTestCase(Type::boolean(), '123', $undefined); $this->runTestCase(Type::int(), '123.456', $undefined); $this->runTestCase(Type::int(), 'true', $undefined); $this->runTestCase(Type::int(), '"123"', $undefined); $this->runTestCase(Type::float(), '"123"', $undefined); $this->runTestCase(Type::string(), '123', $undefined); $this->runTestCase(Type::string(), 'true', $undefined); $this->runTestCase(Type::id(), '123.456', $undefined); }
/** * @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)); }
/** * Initialize Alambic types using GraphQL scalar types. */ protected function initAlambicBaseTypes() { $this->alambicTypes = ['String' => Type::string(), 'Int' => Type::int(), 'Float' => Type::float(), 'Boolean' => Type::boolean(), 'ID' => Type::id()]; $this->inputAlambicTypes = ['String' => Type::string(), 'Int' => Type::int(), 'Float' => Type::float(), 'Boolean' => Type::boolean(), 'ID' => Type::id()]; }
/** * @return \GraphQL\Type\Definition\FloatType */ public static function float() { return Type::float(); }
private function schemaWithFieldArgOfType($argType) { $someIncorrectInputType = new InputObjectType(['name' => 'SomeIncorrectInputType', 'fields' => ['val' => ['type' => function () use($argType) { return $argType; }]]]); $queryType = new ObjectType(['name' => 'QueryType', 'fields' => ['f2' => ['type' => Type::float(), 'args' => ['arg' => ['type' => $someIncorrectInputType]]]]]); return new Schema($queryType); }
/** * @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)); }
/** * Float field. * * @param array|string $config * @return array */ public function float($config = []) { $description = is_string($config) ? $config : ''; $config = is_array($config) ? $config : []; return array_merge(['type' => Type::float(), 'description' => $description], $config); }
/** * Get FloatType instance. * * @return \GraphQL\Type\Definition\FloatType */ public function getFloatType() { return Type::float(); }
/** * Resolve field type by column info. * * @param string $name * @param string $colType * @return \GraphQL\Type\Definition\Type */ protected function resolveTypeByColumn($name, $colType) { $type = Type::string(); $type->name = $this->getName() . '_String'; if ($name === $this->model->getKeyName()) { $type = Type::id(); $type->name = $this->getName() . '_ID'; } elseif ($colType === 'integer') { $type = Type::int(); $type->name = $this->getName() . '_Int'; } elseif ($colType === 'float' || $colType === 'decimal') { $type = Type::float(); $type->name = $this->getName() . '_Float'; } elseif ($colType === 'boolean') { $type = Type::boolean(); $type->name = $this->getName() . '_Boolean'; } return $type; }