Skip to content

Commit

Permalink
feat: allow deprecating input fields and arguments (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored May 11, 2023
1 parent 1cd6284 commit 8a53f84
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require": {
"php": "^8.1",
"thecodingmachine/safe": "^2",
"webonyx/graphql-php": "^15.0"
"webonyx/graphql-php": "^15.4"
},
"require-dev": {
"doctrine/coding-standard": "^12.0",
Expand Down
14 changes: 12 additions & 2 deletions src/Builder/FieldBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ public function setDescription(string $description): self
*
* @return $this
*/
public function addArgument(string $name, $type, string|null $description = null, mixed $defaultValue = null): self
{
public function addArgument(
string $name,
$type,
string|null $description = null,
mixed $defaultValue = null,
string|null $deprecationReason = null,
): self {
if ($this->args === null) {
$this->args = [];
}
Expand All @@ -80,6 +85,11 @@ public function addArgument(string $name, $type, string|null $description = null
$value['defaultValue'] = $defaultValue;
}

if ($deprecationReason !== null) {
/** @psalm-suppress MixedAssignment */
$value['deprecationReason'] = $deprecationReason;
}

$this->args[$name] = $value;

return $this;
Expand Down
9 changes: 8 additions & 1 deletion src/Builder/InputFieldBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public function setDescription(string $description): self
return $this;
}

/** @return $this */
public function setDeprecationReason(string|null $deprecationReason): self
{
$this->deprecationReason = $deprecationReason;

return $this;
}

/** @psalm-return InputObjectFieldConfig */
public function build(): array
{
Expand All @@ -73,7 +81,6 @@ public function build(): array
];

$property = new ReflectionProperty($this, 'defaultValue');
$property->setAccessible(true);
if ($property->isInitialized($this)) {
/** @psalm-suppress MixedAssignment */
$config['defaultValue'] = $this->defaultValue;
Expand Down
3 changes: 2 additions & 1 deletion tests/Builder/FieldBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testCreate(): void
->setDeprecationReason('Deprecated')
->setDescription('SomeDescription')
->setResolver(static fn (): string => 'Resolver result')
->addArgument('arg1', Type::int(), 'Argument Description', 1)
->addArgument('arg1', Type::int(), 'Argument Description', 1, 'Reason')
->build();

self::assertSame('SomeField', $field['name']);
Expand All @@ -43,6 +43,7 @@ public function testCreate(): void
self::assertIsArray($args['arg1']);
self::assertSame(Type::int(), $args['arg1']['type']);
self::assertSame('Argument Description', $args['arg1']['description']);
self::assertSame('Reason', $args['arg1']['deprecationReason']);
self::assertSame(1, $args['arg1']['defaultValue']);
}
}
3 changes: 3 additions & 0 deletions tests/Builder/InputFieldBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ public function testCreate(): void
$field = InputFieldBuilder::create('SomeField', Type::string())
->setDefaultValue(null)
->setDescription('SomeDescription')
->setDeprecationReason('Reason')
->build();

self::assertSame('SomeField', $field['name']);
self::assertArrayHasKey('defaultValue', $field);
self::assertNull($field['defaultValue']);
self::assertArrayHasKey('description', $field);
self::assertSame('SomeDescription', $field['description']);
self::assertArrayHasKey('deprecationReason', $field);
self::assertSame('Reason', $field['deprecationReason']);
}

public function testCreateWithoutDefaultValue(): void
Expand Down

0 comments on commit 8a53f84

Please sign in to comment.