Skip to content

Commit 8a53f84

Browse files
authored
feat: allow deprecating input fields and arguments (#139)
1 parent 1cd6284 commit 8a53f84

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require": {
2121
"php": "^8.1",
2222
"thecodingmachine/safe": "^2",
23-
"webonyx/graphql-php": "^15.0"
23+
"webonyx/graphql-php": "^15.4"
2424
},
2525
"require-dev": {
2626
"doctrine/coding-standard": "^12.0",

src/Builder/FieldBuilder.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ public function setDescription(string $description): self
6363
*
6464
* @return $this
6565
*/
66-
public function addArgument(string $name, $type, string|null $description = null, mixed $defaultValue = null): self
67-
{
66+
public function addArgument(
67+
string $name,
68+
$type,
69+
string|null $description = null,
70+
mixed $defaultValue = null,
71+
string|null $deprecationReason = null,
72+
): self {
6873
if ($this->args === null) {
6974
$this->args = [];
7075
}
@@ -80,6 +85,11 @@ public function addArgument(string $name, $type, string|null $description = null
8085
$value['defaultValue'] = $defaultValue;
8186
}
8287

88+
if ($deprecationReason !== null) {
89+
/** @psalm-suppress MixedAssignment */
90+
$value['deprecationReason'] = $deprecationReason;
91+
}
92+
8393
$this->args[$name] = $value;
8494

8595
return $this;

src/Builder/InputFieldBuilder.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public function setDescription(string $description): self
6262
return $this;
6363
}
6464

65+
/** @return $this */
66+
public function setDeprecationReason(string|null $deprecationReason): self
67+
{
68+
$this->deprecationReason = $deprecationReason;
69+
70+
return $this;
71+
}
72+
6573
/** @psalm-return InputObjectFieldConfig */
6674
public function build(): array
6775
{
@@ -73,7 +81,6 @@ public function build(): array
7381
];
7482

7583
$property = new ReflectionProperty($this, 'defaultValue');
76-
$property->setAccessible(true);
7784
if ($property->isInitialized($this)) {
7885
/** @psalm-suppress MixedAssignment */
7986
$config['defaultValue'] = $this->defaultValue;

tests/Builder/FieldBuilderTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testCreate(): void
1818
->setDeprecationReason('Deprecated')
1919
->setDescription('SomeDescription')
2020
->setResolver(static fn (): string => 'Resolver result')
21-
->addArgument('arg1', Type::int(), 'Argument Description', 1)
21+
->addArgument('arg1', Type::int(), 'Argument Description', 1, 'Reason')
2222
->build();
2323

2424
self::assertSame('SomeField', $field['name']);
@@ -43,6 +43,7 @@ public function testCreate(): void
4343
self::assertIsArray($args['arg1']);
4444
self::assertSame(Type::int(), $args['arg1']['type']);
4545
self::assertSame('Argument Description', $args['arg1']['description']);
46+
self::assertSame('Reason', $args['arg1']['deprecationReason']);
4647
self::assertSame(1, $args['arg1']['defaultValue']);
4748
}
4849
}

tests/Builder/InputFieldBuilderTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ public function testCreate(): void
1515
$field = InputFieldBuilder::create('SomeField', Type::string())
1616
->setDefaultValue(null)
1717
->setDescription('SomeDescription')
18+
->setDeprecationReason('Reason')
1819
->build();
1920

2021
self::assertSame('SomeField', $field['name']);
2122
self::assertArrayHasKey('defaultValue', $field);
2223
self::assertNull($field['defaultValue']);
2324
self::assertArrayHasKey('description', $field);
2425
self::assertSame('SomeDescription', $field['description']);
26+
self::assertArrayHasKey('deprecationReason', $field);
27+
self::assertSame('Reason', $field['deprecationReason']);
2528
}
2629

2730
public function testCreateWithoutDefaultValue(): void

0 commit comments

Comments
 (0)