From a76cafcf63a1c36aacc0f00689ee91822507f410 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Fri, 1 Dec 2023 15:53:26 +0100 Subject: [PATCH] Correctly print deprecated argument with default value Today I deprecated an argument with a default value and noticed that the schema was printed wrong: ```graphql type Query { singleField(id: ID @deprecated(reason: "this is deprecated") = 123): Int } ``` The `@deprecated` directive should be put after the `= 123` default value. --- src/Utils/SchemaPrinter.php | 4 +++- tests/Utils/SchemaPrinterTest.php | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Utils/SchemaPrinter.php b/src/Utils/SchemaPrinter.php index a75e12716..0a5f2304b 100644 --- a/src/Utils/SchemaPrinter.php +++ b/src/Utils/SchemaPrinter.php @@ -338,7 +338,7 @@ protected static function printArgs(array $options, array $args, string $indenta */ protected static function printInputValue($arg): string { - $argDecl = "{$arg->name}: {$arg->getType()->toString()}" . static::printDeprecated($arg); + $argDecl = "{$arg->name}: {$arg->getType()->toString()}"; if ($arg->defaultValueExists()) { $defaultValueAST = AST::astFromValue($arg->defaultValue, $arg->getType()); @@ -351,6 +351,8 @@ protected static function printInputValue($arg): string $argDecl .= ' = ' . Printer::doPrint($defaultValueAST); } + $argDecl .= static::printDeprecated($arg); + return $argDecl; } diff --git a/tests/Utils/SchemaPrinterTest.php b/tests/Utils/SchemaPrinterTest.php index 1f383cfe7..797157539 100644 --- a/tests/Utils/SchemaPrinterTest.php +++ b/tests/Utils/SchemaPrinterTest.php @@ -1369,4 +1369,27 @@ interface FooInterface { ['sortArguments' => true] ); } + + public function testPrintDeprecatedFieldArg(): void + { + $schema = $this->buildSingleFieldSchema([ + 'type' => Type::int(), + 'args' => [ + 'id' => [ + 'type' => Type::id(), + 'defaultValue' => '123', + 'deprecationReason' => 'this is deprecated', + ], + ], + ]); + self::assertPrintedSchemaEquals( + <<