Skip to content

Commit fbcd208

Browse files
committed
Scalar type serialize method now throws InvariantViolation and parseValue throws UserError
1 parent 8e3d1eb commit fbcd208

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

src/Type/Definition/FloatType.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4+
use GraphQL\Error\InvariantViolation;
45
use GraphQL\Error\UserError;
56
use GraphQL\Language\AST\FloatValueNode;
67
use GraphQL\Language\AST\IntValueNode;
@@ -31,7 +32,7 @@ class FloatType extends ScalarType
3132
*/
3233
public function serialize($value)
3334
{
34-
return $this->coerceFloat($value);
35+
return $this->coerceFloat($value, false);
3536
}
3637

3738
/**
@@ -40,24 +41,29 @@ public function serialize($value)
4041
*/
4142
public function parseValue($value)
4243
{
43-
return $this->coerceFloat($value);
44+
return $this->coerceFloat($value, true);
4445
}
4546

4647
/**
47-
* @param $value
48+
* @param mixed $value
49+
* @param bool $isInput
4850
* @return float|null
4951
*/
50-
private function coerceFloat($value)
52+
private function coerceFloat($value, $isInput)
5153
{
54+
if (is_numeric($value) || $value === true || $value === false) {
55+
return (float) $value;
56+
}
57+
5258
if ($value === '') {
53-
throw new UserError(
54-
'Float cannot represent non numeric value: (empty string)'
59+
$err = 'Float cannot represent non numeric value: (empty string)';
60+
} else {
61+
$err = sprintf(
62+
'Float cannot represent non numeric value: %s',
63+
$isInput ? Utils::printSafeJson($value) : Utils::printSafe($value)
5564
);
5665
}
57-
if (is_numeric($value) || $value === true || $value === false) {
58-
return (float)$value;
59-
}
60-
throw new UserError(sprintf('Float cannot represent non numeric value: %s', Utils::printSafe($value)));
66+
throw ($isInput ? new UserError($err) : new InvariantViolation($err));
6167
}
6268

6369
/**

src/Type/Definition/IDType.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4+
use GraphQL\Error\InvariantViolation;
45
use GraphQL\Error\UserError;
56
use GraphQL\Language\AST\IntValueNode;
67
use GraphQL\Language\AST\StringValueNode;
7-
use GraphQL\Utils;
8+
use GraphQL\Utils\Utils;
89

910
/**
1011
* Class IDType
@@ -33,7 +34,19 @@ class IDType extends ScalarType
3334
*/
3435
public function serialize($value)
3536
{
36-
return $this->parseValue($value);
37+
if ($value === true) {
38+
return 'true';
39+
}
40+
if ($value === false) {
41+
return 'false';
42+
}
43+
if ($value === null) {
44+
return 'null';
45+
}
46+
if (!is_scalar($value)) {
47+
throw new InvariantViolation("ID type cannot represent non scalar value: " . Utils::printSafe($value));
48+
}
49+
return (string) $value;
3750
}
3851

3952
/**
@@ -52,7 +65,7 @@ public function parseValue($value)
5265
return 'null';
5366
}
5467
if (!is_scalar($value)) {
55-
throw new UserError("String cannot represent non scalar value: " . Utils::printSafe($value));
68+
throw new UserError("ID type cannot represent non scalar value: " . Utils::printSafeJson($value));
5669
}
5770
return (string) $value;
5871
}

src/Type/Definition/IntType.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class IntType extends ScalarType
3838
*/
3939
public function serialize($value)
4040
{
41-
return $this->coerceInt($value);
41+
return $this->coerceInt($value, false);
4242
}
4343

4444
/**
@@ -47,44 +47,45 @@ public function serialize($value)
4747
*/
4848
public function parseValue($value)
4949
{
50-
try {
51-
return $this->coerceInt($value);
52-
} catch (InvariantViolation $e) {
53-
throw new UserError($e->getMessage(), $e->getCode(), $e);
54-
}
50+
return $this->coerceInt($value, true);
5551
}
5652

5753
/**
5854
* @param $value
55+
* @param bool $isInput
5956
* @return int|null
6057
*/
61-
private function coerceInt($value)
58+
private function coerceInt($value, $isInput)
6259
{
60+
$errClass = $isInput ? UserError::class : InvariantViolation::class;
61+
6362
if ($value === '') {
64-
throw new InvariantViolation(
63+
throw new $errClass(
6564
'Int cannot represent non 32-bit signed integer value: (empty string)'
6665
);
6766
}
6867
if (false === $value || true === $value) {
6968
return (int) $value;
7069
}
7170
if (!is_numeric($value) || $value > self::MAX_INT || $value < self::MIN_INT) {
72-
throw new InvariantViolation(
73-
sprintf('Int cannot represent non 32-bit signed integer value: %s', Utils::printSafe($value))
74-
);
71+
throw new $errClass(sprintf(
72+
'Int cannot represent non 32-bit signed integer value: %s',
73+
$isInput ? Utils::printSafeJson($value) : Utils::printSafe($value)
74+
));
7575
}
7676
$num = (float) $value;
7777

7878
// The GraphQL specification does not allow serializing non-integer values
7979
// as Int to avoid accidental data loss.
8080
// Examples: 1.0 == 1; 1.1 != 1, etc
81-
if ($num != (int)$value) {
81+
if ($num != (int) $value) {
8282
// Additionally account for scientific notation (i.e. 1e3), because (float)'1e3' is 1000, but (int)'1e3' is 1
8383
$trimmed = floor($num);
8484
if ($trimmed !== $num) {
85-
throw new InvariantViolation(
86-
'Int cannot represent non-integer value: ' . Utils::printSafe($value)
87-
);
85+
throw new $errClass(sprintf(
86+
'Int cannot represent non-integer value: %s',
87+
$isInput ? Utils::printSafeJson($value) : Utils::printSafe($value)
88+
));
8889
}
8990
}
9091
return (int) $value;

src/Type/Definition/StringType.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4+
use GraphQL\Error\InvariantViolation;
45
use GraphQL\Error\UserError;
56
use GraphQL\Language\AST\StringValueNode;
6-
use GraphQL\Utils;
7+
use GraphQL\Utils\Utils;
78

89
/**
910
* Class StringType
@@ -30,7 +31,19 @@ class StringType extends ScalarType
3031
*/
3132
public function serialize($value)
3233
{
33-
return $this->parseValue($value);
34+
if ($value === true) {
35+
return 'true';
36+
}
37+
if ($value === false) {
38+
return 'false';
39+
}
40+
if ($value === null) {
41+
return 'null';
42+
}
43+
if (!is_scalar($value)) {
44+
throw new InvariantViolation("String cannot represent non scalar value: " . Utils::printSafe($value));
45+
}
46+
return (string) $value;
3447
}
3548

3649
/**

0 commit comments

Comments
 (0)