diff --git a/CHANGELOG.md b/CHANGELOG.md index 5500eec86..6da18f874 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ + Cannot return null for non-nullable field "parentType.fieldName". ``` - Simplified Deferred implementation +- Having an empty string in `deprecationReason` will now print the `@deprecated` directive (only a `null` `deprecationReason` won't print the `@deprecated` directive). #### v0.13.5 - Fix coroutine executor when using with promise (#486) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 0fbb97f52..09f3df772 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -902,7 +902,7 @@ parameters: - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 3 + count: 2 path: src/Utils/SchemaPrinter.php - diff --git a/src/Utils/SchemaPrinter.php b/src/Utils/SchemaPrinter.php index 098912d8c..ccbb00734 100644 --- a/src/Utils/SchemaPrinter.php +++ b/src/Utils/SchemaPrinter.php @@ -393,7 +393,7 @@ static function ($f, $i) use ($options) { private static function printDeprecated($fieldOrEnumVal) : string { $reason = $fieldOrEnumVal->deprecationReason; - if (empty($reason)) { + if ($reason === null) { return ''; } if ($reason === '' || $reason === Directive::DEFAULT_DEPRECATION_REASON) { diff --git a/tests/Utils/SchemaPrinterTest.php b/tests/Utils/SchemaPrinterTest.php index 7131875f1..ab54cdf1b 100644 --- a/tests/Utils/SchemaPrinterTest.php +++ b/tests/Utils/SchemaPrinterTest.php @@ -4,6 +4,7 @@ namespace GraphQL\Tests\Utils; +use Generator; use GraphQL\Language\DirectiveLocation; use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\Directive; @@ -148,6 +149,47 @@ public function testPrintNonNullArrayNonNullStringField() : void ); } + /** + * @see it('Prints Field With "@deprecated" Directive') + * + * @dataProvider deprecationReasonDataProvider + */ + public function testPrintDeprecatedField(?string $deprecationReason, string $expectedDeprecationDirective) : void + { + $output = $this->printSingleFieldSchema([ + 'type' => Type::int(), + 'deprecationReason' => $deprecationReason, + ]); + self::assertSame( + ' +type Query { + singleField: Int' . $expectedDeprecationDirective . ' +} +', + $output + ); + } + + public function deprecationReasonDataProvider() : Generator + { + yield 'when deprecationReason is null' => [ + null, + '', + ]; + yield 'when deprecationReason is empty string' => [ + '', + ' @deprecated', + ]; + yield 'when deprecationReason is the default deprecation reason' => [ + Directive::DEFAULT_DEPRECATION_REASON, + ' @deprecated', + ]; + yield 'when deprecationReason is not empty string' => [ + 'this is deprecated', + ' @deprecated(reason: "this is deprecated")', + ]; + } + /** * @see it('Print Object Field') */