diff --git a/src/Error/Error.php b/src/Error/Error.php index 04112a8..254d395 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -182,16 +182,12 @@ class Error extends \Exception implements \JsonSerializable, ClientAware if ($previous instanceof ClientAware) { $this->isClientSafe = $previous->isClientSafe(); $this->category = $previous->getCategory() ?: static::CATEGORY_INTERNAL; + } else if ($previous) { + $this->isClientSafe = false; + $this->category = static::CATEGORY_INTERNAL; } else { $this->isClientSafe = true; - while ($previous) { - if (!preg_match('#/webonyx/graphql-php/#u', $previous->getFile())) { - $this->isClientSafe = false; - break; - } - $previous = $previous->getPrevious(); - } - $this->category = $this->isClientSafe ? self::CATEGORY_GRAPHQL : self::CATEGORY_INTERNAL; + $this->category = static::CATEGORY_GRAPHQL; } } diff --git a/src/Validator/Rules/ValuesOfCorrectType.php b/src/Validator/Rules/ValuesOfCorrectType.php index 68b2c7d..a9666c9 100644 --- a/src/Validator/Rules/ValuesOfCorrectType.php +++ b/src/Validator/Rules/ValuesOfCorrectType.php @@ -16,9 +16,9 @@ use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\AST\ValueNode; use GraphQL\Language\Printer; use GraphQL\Language\Visitor; -use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumValueDefinition; +use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; @@ -39,10 +39,10 @@ class ValuesOfCorrectType extends AbstractValidationRule static function badValueMessage($typeName, $valueName, $message = null, $context = null) { - if ($context && ($arg = $context->getArgument()) && $arg instanceof FieldArgument) { - $fieldName = self::$fieldName; + $fieldName = self::$fieldName; + if ($context AND $arg = $context->getArgument() AND $arg instanceof FieldArgument) { $argName = $arg->name; - return "Field \"{$fieldName}\" argument \"{$argName}\" requires type {$typeName}, found {$valueName}" . ($message ? "; ${message}" : '.'); + return "Field \"{$fieldName}\" argument \"{$argName}\" requires type {$typeName}, found {$valueName}" . ($message ? "; {$message}" : '.'); } return "Expected type {$typeName}, found {$valueName}" . ($message ? "; ${message}" : '.'); @@ -75,7 +75,7 @@ class ValuesOfCorrectType extends AbstractValidationRule if ($type instanceof NonNull) { $context->reportError( new Error( - self::badValueMessage((string) $type, Printer::doPrint($node)), + self::badValueMessage((string) $type, Printer::doPrint($node), null, $context), $node ) ); @@ -147,7 +147,8 @@ class ValuesOfCorrectType extends AbstractValidationRule self::badValueMessage( $type->name, Printer::doPrint($node), - $this->enumTypeSuggestion($type, $node) + $this->enumTypeSuggestion($type, $node), + $context ), $node ) @@ -178,7 +179,8 @@ class ValuesOfCorrectType extends AbstractValidationRule self::badValueMessage( (string) $locationType, Printer::doPrint($node), - $this->enumTypeSuggestion($type, $node) + $this->enumTypeSuggestion($type, $node), + $context ), $node ) @@ -191,7 +193,8 @@ class ValuesOfCorrectType extends AbstractValidationRule try { $type->parseLiteral($node); } catch (\Exception $error) { - // Ensure a reference to the original error is maintained. + // We should not pass $error to "previous" parameter of Error's constructor here, + // otherwise this error will be in "internal" category instead of "graphql". $context->reportError( new Error( self::badValueMessage( @@ -200,27 +203,21 @@ class ValuesOfCorrectType extends AbstractValidationRule $error->getMessage(), $context ), - $node, - null, - null, - null, - $error + $node ) ); } catch (\Throwable $error) { - // Ensure a reference to the original error is maintained. + // We should not pass $error to "previous" parameter of Error's constructor here, + // otherwise this error will be in "internal" category instead of "graphql". $context->reportError( new Error( self::badValueMessage( (string) $locationType, Printer::doPrint($node), - $error->getMessage() + $error->getMessage(), + $context ), - $node, - null, - null, - null, - $error + $node ) ); } diff --git a/tests/Type/EnumTypeTest.php b/tests/Type/EnumTypeTest.php index 4a53e5e..37c7f5e 100644 --- a/tests/Type/EnumTypeTest.php +++ b/tests/Type/EnumTypeTest.php @@ -220,7 +220,7 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase '{ colorEnum(fromEnum: "GREEN") }', null, [ - 'message' => "Expected type Color, found \"GREEN\"; Did you mean the enum value GREEN?", + 'message' => "Field \"colorEnum\" argument \"fromEnum\" requires type Color, found \"GREEN\"; Did you mean the enum value GREEN?", 'locations' => [new SourceLocation(1, 23)] ] ); @@ -235,7 +235,7 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase '{ colorEnum(fromEnum: GREENISH) }', null, [ - 'message' => "Expected type Color, found GREENISH; Did you mean the enum value GREEN?", + 'message' => "Field \"colorEnum\" argument \"fromEnum\" requires type Color, found GREENISH; Did you mean the enum value GREEN?", 'locations' => [new SourceLocation(1, 23)] ] ); @@ -250,7 +250,7 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase '{ colorEnum(fromEnum: green) }', null, [ - 'message' => "Expected type Color, found green; Did you mean the enum value GREEN?", + 'message' => "Field \"colorEnum\" argument \"fromEnum\" requires type Color, found green; Did you mean the enum value GREEN?", 'locations' => [new SourceLocation(1, 23)] ] ); @@ -280,7 +280,7 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase $this->expectFailure( '{ colorEnum(fromEnum: 1) }', null, - "Expected type Color, found 1." + "Field \"colorEnum\" argument \"fromEnum\" requires type Color, found 1." ); } diff --git a/tests/Validator/ValuesOfCorrectTypeTest.php b/tests/Validator/ValuesOfCorrectTypeTest.php index 80d6d39..a6350e4 100644 --- a/tests/Validator/ValuesOfCorrectTypeTest.php +++ b/tests/Validator/ValuesOfCorrectTypeTest.php @@ -19,9 +19,9 @@ class ValuesOfCorrectTypeTest extends TestCase ); } - private function badValue2($error, $line, $column) + private function badValueWithMessage($message, $line, $column) { - return FormattedError::create($error, [new SourceLocation($line, $column)]); + return FormattedError::create($message, [new SourceLocation($line, $column)]); } private function requiredField($typeName, $fieldName, $fieldTypeName, $line, $column) { @@ -236,7 +236,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"stringArgField\" argument \"stringArg\" requires type String, found 1.", 4, 39) + $this->badValueWithMessage("Field \"stringArgField\" argument \"stringArg\" requires type String, found 1.", 4, 39) ]); } @@ -252,7 +252,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"stringArgField\" argument \"stringArg\" requires type String, found 1.0.", 4, 39) + $this->badValueWithMessage("Field \"stringArgField\" argument \"stringArg\" requires type String, found 1.0.", 4, 39) ]); } @@ -268,7 +268,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"stringArgField\" argument \"stringArg\" requires type String, found true.", 4, 39) + $this->badValueWithMessage("Field \"stringArgField\" argument \"stringArg\" requires type String, found true.", 4, 39) ]); } @@ -284,7 +284,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"stringArgField\" argument \"stringArg\" requires type String, found BAR.", 4, 39) + $this->badValueWithMessage("Field \"stringArgField\" argument \"stringArg\" requires type String, found BAR.", 4, 39) ]); } @@ -302,7 +302,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"intArgField\" argument \"intArg\" requires type Int, found \"3\".", 4, 33) + $this->badValueWithMessage("Field \"intArgField\" argument \"intArg\" requires type Int, found \"3\".", 4, 33) ]); } @@ -318,7 +318,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"intArgField\" argument \"intArg\" requires type Int, found 829384293849283498239482938.", 4, 33) + $this->badValueWithMessage("Field \"intArgField\" argument \"intArg\" requires type Int, found 829384293849283498239482938.", 4, 33) ]); } @@ -334,7 +334,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"intArgField\" argument \"intArg\" requires type Int, found FOO.", 4, 33) + $this->badValueWithMessage("Field \"intArgField\" argument \"intArg\" requires type Int, found FOO.", 4, 33) ]); } @@ -350,7 +350,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"intArgField\" argument \"intArg\" requires type Int, found 3.0.", 4, 33) + $this->badValueWithMessage("Field \"intArgField\" argument \"intArg\" requires type Int, found 3.0.", 4, 33) ]); } @@ -366,7 +366,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"intArgField\" argument \"intArg\" requires type Int, found 3.333.", 4, 33) + $this->badValueWithMessage("Field \"intArgField\" argument \"intArg\" requires type Int, found 3.333.", 4, 33) ]); } @@ -384,7 +384,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"floatArgField\" argument \"floatArg\" requires type Float, found \"3.333\".", 4, 37) + $this->badValueWithMessage("Field \"floatArgField\" argument \"floatArg\" requires type Float, found \"3.333\".", 4, 37) ]); } @@ -400,7 +400,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"floatArgField\" argument \"floatArg\" requires type Float, found true.", 4, 37) + $this->badValueWithMessage("Field \"floatArgField\" argument \"floatArg\" requires type Float, found true.", 4, 37) ]); } @@ -416,7 +416,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"floatArgField\" argument \"floatArg\" requires type Float, found FOO.", 4, 37) + $this->badValueWithMessage("Field \"floatArgField\" argument \"floatArg\" requires type Float, found FOO.", 4, 37) ]); } @@ -434,7 +434,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found 2.", 4, 41) + $this->badValueWithMessage("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found 2.", 4, 41) ]); } @@ -450,7 +450,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found 1.0.", 4, 41) + $this->badValueWithMessage("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found 1.0.", 4, 41) ]); } @@ -466,7 +466,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found \"true\".", 4, 41) + $this->badValueWithMessage("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found \"true\".", 4, 41) ]); } @@ -482,7 +482,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found TRUE.", 4, 41) + $this->badValueWithMessage("Field \"booleanArgField\" argument \"booleanArg\" requires type Boolean, found TRUE.", 4, 41) ]); } @@ -500,7 +500,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"idArgField\" argument \"idArg\" requires type ID, found 1.0.", 4, 31) + $this->badValueWithMessage("Field \"idArgField\" argument \"idArg\" requires type ID, found 1.0.", 4, 31) ]); } @@ -516,7 +516,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"idArgField\" argument \"idArg\" requires type ID, found true.", 4, 31) + $this->badValueWithMessage("Field \"idArgField\" argument \"idArg\" requires type ID, found true.", 4, 31) ]); } @@ -532,7 +532,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"idArgField\" argument \"idArg\" requires type ID, found SOMETHING.", 4, 31) + $this->badValueWithMessage("Field \"idArgField\" argument \"idArg\" requires type ID, found SOMETHING.", 4, 31) ]); } @@ -550,7 +550,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue('DogCommand', '2', 4, 41) + $this->badValueWithMessage("Field \"doesKnowCommand\" argument \"dogCommand\" requires type DogCommand, found 2.", 4, 41) ]); } @@ -566,7 +566,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue('DogCommand', '1.0', 4, 41) + $this->badValueWithMessage("Field \"doesKnowCommand\" argument \"dogCommand\" requires type DogCommand, found 1.0.", 4, 41) ]); } @@ -582,13 +582,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue( - 'DogCommand', - '"SIT"', - 4, - 41, - 'Did you mean the enum value SIT?' - ) + $this->badValueWithMessage("Field \"doesKnowCommand\" argument \"dogCommand\" requires type DogCommand, found \"SIT\"; Did you mean the enum value SIT?", 4, 41) ]); } @@ -604,7 +598,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue('DogCommand', 'true', 4, 41) + $this->badValueWithMessage("Field \"doesKnowCommand\" argument \"dogCommand\" requires type DogCommand, found true.", 4, 41) ]); } @@ -620,7 +614,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue('DogCommand', 'JUGGLE', 4, 41) + $this->badValueWithMessage("Field \"doesKnowCommand\" argument \"dogCommand\" requires type DogCommand, found JUGGLE.", 4, 41) ]); } @@ -636,13 +630,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue( - 'DogCommand', - 'sit', - 4, - 41, - 'Did you mean the enum value SIT?' - ) + $this->badValueWithMessage("Field \"doesKnowCommand\" argument \"dogCommand\" requires type DogCommand, found sit; Did you mean the enum value SIT?", 4, 41) ]); } @@ -718,7 +706,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"stringListArgField\" argument \"stringListArg\" requires type String, found 2.", 4, 55), + $this->badValueWithMessage("Field \"stringListArgField\" argument \"stringListArg\" requires type String, found 2.", 4, 55), ]); } @@ -734,7 +722,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"stringListArgField\" argument \"stringListArg\" requires type [String], found 1.", 4, 47), + $this->badValueWithMessage("Field \"stringListArgField\" argument \"stringListArg\" requires type [String], found 1.", 4, 47), ]); } @@ -894,8 +882,8 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"multipleReqs\" argument \"req2\" requires type Int!, found \"two\".", 4, 32), - $this->badValue2("Field \"multipleReqs\" argument \"req1\" requires type Int!, found \"one\".", 4, 45), + $this->badValueWithMessage("Field \"multipleReqs\" argument \"req2\" requires type Int!, found \"two\".", 4, 32), + $this->badValueWithMessage("Field \"multipleReqs\" argument \"req1\" requires type Int!, found \"one\".", 4, 45), ]); } @@ -911,7 +899,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"multipleReqs\" argument \"req1\" requires type Int!, found \"one\".", 4, 32), + $this->badValueWithMessage("Field \"multipleReqs\" argument \"req1\" requires type Int!, found \"one\".", 4, 32), ]); } @@ -927,7 +915,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue('Int!', 'null', 4, 32), + $this->badValueWithMessage("Field \"multipleReqs\" argument \"req1\" requires type Int!, found null.", 4, 32), ]); } @@ -1063,7 +1051,7 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"complexArgField\" argument \"complexArg\" requires type String, found 2.", 5, 40), + $this->badValueWithMessage("Field \"complexArgField\" argument \"complexArg\" requires type String, found 2.", 5, 40), ]); } @@ -1107,12 +1095,12 @@ class ValuesOfCorrectTypeTest extends TestCase invalidArg(arg: 123) } ', [ - $this->badValue2("Field \"invalidArg\" argument \"arg\" requires type Invalid, found 123; Invalid scalar is always invalid: 123", 3, 27), + $this->badValueWithMessage("Field \"invalidArg\" argument \"arg\" requires type Invalid, found 123; Invalid scalar is always invalid: 123", 3, 27), ]); $this->assertEquals( - 'Invalid scalar is always invalid: 123', - $errors[0]->getPrevious()->getMessage() + "Field \"invalidArg\" argument \"arg\" requires type Invalid, found 123; Invalid scalar is always invalid: 123", + $errors[0]->getMessage() ); } @@ -1162,8 +1150,8 @@ class ValuesOfCorrectTypeTest extends TestCase } } ', [ - $this->badValue2("Field \"dog\" argument \"if\" requires type Boolean!, found \"yes\".", 3, 28), - $this->badValue2("Field \"name\" argument \"if\" requires type Boolean!, found ENUM.", 4, 28), + $this->badValueWithMessage("Field \"dog\" argument \"if\" requires type Boolean!, found \"yes\".", 3, 28), + $this->badValueWithMessage("Field \"name\" argument \"if\" requires type Boolean!, found ENUM.", 4, 28), ]); }