Skip to content

Commit a76cafc

Browse files
ruudkspawnia
authored andcommitted
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.
1 parent c6e174a commit a76cafc

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ protected static function printArgs(array $options, array $args, string $indenta
338338
*/
339339
protected static function printInputValue($arg): string
340340
{
341-
$argDecl = "{$arg->name}: {$arg->getType()->toString()}" . static::printDeprecated($arg);
341+
$argDecl = "{$arg->name}: {$arg->getType()->toString()}";
342342

343343
if ($arg->defaultValueExists()) {
344344
$defaultValueAST = AST::astFromValue($arg->defaultValue, $arg->getType());
@@ -351,6 +351,8 @@ protected static function printInputValue($arg): string
351351
$argDecl .= ' = ' . Printer::doPrint($defaultValueAST);
352352
}
353353

354+
$argDecl .= static::printDeprecated($arg);
355+
354356
return $argDecl;
355357
}
356358

tests/Utils/SchemaPrinterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,4 +1369,27 @@ interface FooInterface {
13691369
['sortArguments' => true]
13701370
);
13711371
}
1372+
1373+
public function testPrintDeprecatedFieldArg(): void
1374+
{
1375+
$schema = $this->buildSingleFieldSchema([
1376+
'type' => Type::int(),
1377+
'args' => [
1378+
'id' => [
1379+
'type' => Type::id(),
1380+
'defaultValue' => '123',
1381+
'deprecationReason' => 'this is deprecated',
1382+
],
1383+
],
1384+
]);
1385+
self::assertPrintedSchemaEquals(
1386+
<<<GRAPHQL
1387+
type Query {
1388+
singleField(id: ID = 123 @deprecated(reason: "this is deprecated")): Int
1389+
}
1390+
1391+
GRAPHQL,
1392+
$schema
1393+
);
1394+
}
13721395
}

0 commit comments

Comments
 (0)