Skip to content

Commit

Permalink
Correctly print deprecated argument with default value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ruudk authored and spawnia committed Dec 5, 2023
1 parent c6e174a commit a76cafc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Utils/SchemaPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -351,6 +351,8 @@ protected static function printInputValue($arg): string
$argDecl .= ' = ' . Printer::doPrint($defaultValueAST);
}

$argDecl .= static::printDeprecated($arg);

return $argDecl;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Utils/SchemaPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<<<GRAPHQL
type Query {
singleField(id: ID = 123 @deprecated(reason: "this is deprecated")): Int
}
GRAPHQL,
$schema
);
}
}

0 comments on commit a76cafc

Please sign in to comment.