diff --git a/src/Executor/Values.php b/src/Executor/Values.php index b485e7e..06c9532 100644 --- a/src/Executor/Values.php +++ b/src/Executor/Values.php @@ -280,7 +280,7 @@ class Values // a non-null value. $parseResult = $type->parseValue($value); if (null === $parseResult && !$type->isValidValue($value)) { - $v = Utils::printSafe($value); + $v = Utils::printSafeJson($value); return [ "Expected type \"{$type->name}\", found $v." ]; @@ -288,12 +288,12 @@ class Values return []; } catch (\Exception $e) { return [ - "Expected type \"{$type->name}\", found " . Utils::printSafe($value) . ': ' . + "Expected type \"{$type->name}\", found " . Utils::printSafeJson($value) . ': ' . $e->getMessage() ]; } catch (\Throwable $e) { return [ - "Expected type \"{$type->name}\", found " . Utils::printSafe($value) . ': ' . + "Expected type \"{$type->name}\", found " . Utils::printSafeJson($value) . ': ' . $e->getMessage() ]; } diff --git a/tests/Executor/VariablesTest.php b/tests/Executor/VariablesTest.php index aba2f8f..418ed12 100644 --- a/tests/Executor/VariablesTest.php +++ b/tests/Executor/VariablesTest.php @@ -3,12 +3,10 @@ namespace GraphQL\Tests\Executor; require_once __DIR__ . '/TestClasses.php'; -use GraphQL\Error\Error; +use GraphQL\Error\InvariantViolation; use GraphQL\Executor\Executor; -use GraphQL\Error\FormattedError; use GraphQL\Language\Parser; -use GraphQL\Language\SourceLocation; -use GraphQL\Schema; +use GraphQL\Type\Schema; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; @@ -467,6 +465,47 @@ class VariablesTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, Executor::execute($this->schema(), $ast)->toArray()); } + /** + * @it reports error for array passed into string input + */ + public function testReportsErrorForArrayPassedIntoStringInput() + { + + $doc = ' + query SetsNonNullable($value: String!) { + fieldWithNonNullableStringInput(input: $value) + } + '; + $ast = Parser::parse($doc); + $variables = ['value' => [1, 2, 3]]; + + $expected = [ + 'errors' => [[ + 'message' => + 'Variable "$value" got invalid value [1,2,3].' . "\n" . + 'Expected type "String", found array(3).', + 'category' => 'graphql', + 'locations' => [ + ['line' => 2, 'column' => 31] + ] + ]] + ]; + + $this->assertEquals($expected, Executor::execute($this->schema(), $ast, null, null, $variables)->toArray()); + } + + /** + * @it serializing an array via GraphQLString throws TypeError + */ + public function testSerializingAnArrayViaGraphQLStringThrowsTypeError() + { + $this->setExpectedException( + InvariantViolation::class, + 'String cannot represent non scalar value: array(3)' + ); + Type::string()->serialize([1, 2, 3]); + } + /** * @it reports error for non-provided variables for non-nullable inputs */