diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index b2ba19a..c21db83 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -1187,7 +1187,7 @@ class Executor { $serializedResult = $returnType->serialize($result); - if ($serializedResult === null) { + if (Utils::isInvalid($serializedResult)) { throw new InvariantViolation( 'Expected a value of type "'. Utils::printSafe($returnType) . '" but received: ' . Utils::printSafe($result) ); diff --git a/src/Type/Definition/EnumType.php b/src/Type/Definition/EnumType.php index 035c86f..1f18875 100644 --- a/src/Type/Definition/EnumType.php +++ b/src/Type/Definition/EnumType.php @@ -108,7 +108,11 @@ class EnumType extends Type implements InputType, OutputType, LeafType public function serialize($value) { $lookup = $this->getValueLookup(); - return isset($lookup[$value]) ? $lookup[$value]->name : null; + if (isset($lookup[$value])) { + return $lookup[$value]->name; + } + + return Utils::undefined(); } /** diff --git a/src/Utils/AST.php b/src/Utils/AST.php index 6fd6a9b..ea38aa4 100644 --- a/src/Utils/AST.php +++ b/src/Utils/AST.php @@ -205,15 +205,15 @@ class AST return new ObjectValueNode(['fields' => $fieldNodes]); } + Utils::invariant( + $type instanceof ScalarType || $type instanceof EnumType, + "Must provide Input Type, cannot use: " . Utils::printSafe($type) + ); + // Since value is an internally represented value, it must be serialized // to an externally represented value before converting into an AST. - if ($type instanceof LeafType) { - $serialized = $type->serialize($value); - } else { - throw new InvariantViolation("Must provide Input Type, cannot use: " . Utils::printSafe($type)); - } - - if (null === $serialized) { + $serialized = $type->serialize($value); + if (null === $serialized || Utils::isInvalid($serialized)) { return null; }