Fixed scalar Int to respect min/max values according to graphql spec; added descriptions to scalars

This commit is contained in:
vladar 2016-05-02 04:09:00 +06:00
parent c3d7a49a08
commit 31f40f5e26
5 changed files with 33 additions and 2 deletions

View File

@ -7,6 +7,8 @@ class BooleanType extends ScalarType
{
public $name = Type::BOOLEAN;
public $description = 'The `Boolean` scalar type represents `true` or `false`.';
public function serialize($value)
{
return !!$value;

View File

@ -8,6 +8,11 @@ class FloatType extends ScalarType
{
public $name = Type::FLOAT;
public $description =
'The `Float` scalar type represents signed double-precision fractional ' .
'values as specified by ' .
'[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ';
public function serialize($value)
{
return $this->coerceFloat($value);

View File

@ -8,6 +8,13 @@ class IDType extends ScalarType
{
public $name = 'ID';
public $description =
'The `ID` scalar type represents a unique identifier, often used to ' .
'refetch an object or as key for a cache. The ID type appears in a JSON ' .
'response as a String; however, it is not intended to be human-readable. ' .
'When expected as an input type, any string (such as `"4"`) or integer ' .
'(such as `4`) input value will be accepted as an ID.';
public function serialize($value)
{
return (string) $value;

View File

@ -6,8 +6,20 @@ use GraphQL\Language\AST\Value;
class IntType extends ScalarType
{
// As per the GraphQL Spec, Integers are only treated as valid when a valid
// 32-bit signed integer, providing the broadest support across platforms.
//
// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because
// they are internally represented as IEEE 754 doubles.
const MAX_INT = 2147483647;
const MIN_INT = -2147483648;
public $name = Type::INT;
public $description =
'The `Int` scalar type represents non-fractional signed whole numeric ' .
'values. Int can represent values between -(2^31) and 2^31 - 1. ';
public function serialize($value)
{
return $this->coerceInt($value);
@ -23,7 +35,7 @@ class IntType extends ScalarType
if (false === $value || true === $value) {
return (int) $value;
}
if (is_numeric($value) && $value <= PHP_INT_MAX && $value >= -1 * PHP_INT_MAX) {
if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
return (int) $value;
}
return null;
@ -33,7 +45,7 @@ class IntType extends ScalarType
{
if ($ast instanceof IntValue) {
$val = (int) $ast->value;
if ($ast->value === (string) $val) {
if ($ast->value === (string) $val && self::MIN_INT <= $val && $val <= self::MAX_INT) {
return $val;
}
}

View File

@ -7,6 +7,11 @@ class StringType extends ScalarType
{
public $name = Type::STRING;
public $description =
'The `String` scalar type represents textual data, represented as UTF-8 ' .
'character sequences. The String type is most often used by GraphQL to ' .
'represent free-form human-readable text.';
public function serialize($value)
{
return $this->parseValue($value);