Skip to content

Commit 4c797fd

Browse files
committed
Force fields to be optional with "update" input
1 parent 991d390 commit 4c797fd

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

docs/annotations_reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Attribute | Compulsory | Type | Definition
6666
name | *no* | string | The name of the GraphQL input type generated. If not passed, the name of the class with suffix "Input" is used. If the class ends with "Input", the "Input" suffix is not added.
6767
description | *no* | string | Description of the input type in the documentation. If not passed, PHP doc comment is used.
6868
default | *no* | bool | Defaults to *true* if name is not specified. Whether the annotated PHP class should be mapped by default to this type.
69-
update | *no* | bool | Determines if the the input represents a partial update. When set to *true* input fields won't have default values thus won't be set on resolve if they are not specified in the query/mutation.
69+
update | *no* | bool | Determines if the the input represents a partial update. When set to *true* all input fields will become optional and won't have default values thus won't be set on resolve if they are not specified in the query/mutation.
7070

7171

7272
## @Field annotation

docs/input_types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ There are 2 input types created for just one class: `CreateUserInput` and `Updat
547547
- Field `age` is optional for both input types.
548548

549549
Note that `update: true` argument for `UpdateUserInput`. It should be used when input type is used for a partial update,
550-
It removes all default values from all fields thus prevents setting default values via setters or directly to public properties.
550+
It makes all fields optional and removes all default values from thus prevents setting default values via setters or directly to public properties.
551551
In example above if you use the class as `UpdateUserInput` and set only `username` the other ones will be ignored.
552552
In PHP 7 they will be set to `null`, while in PHP 8 they will be in not initialized state - this can be used as a trick
553553
to check if user actually passed a value for a certain field.

src/Types/InputType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TheCodingMachine\GraphQLite\Types;
66

7+
use GraphQL\Type\Definition\NonNull;
78
use GraphQL\Type\Definition\ResolveInfo;
89
use ReflectionClass;
910
use TheCodingMachine\GraphQLite\FailedResolvingInputType;
@@ -40,6 +41,10 @@ public function __construct(string $className, string $inputName, ?string $descr
4041
foreach ($this->fields as $name => $field) {
4142
$type = $field->getType();
4243

44+
if ($isUpdate && $type instanceof NonNull) {
45+
$type = $type->getWrappedType();
46+
}
47+
4348
$fields[$name] = [
4449
'type' => $type,
4550
'description' => $field->getDescription(),

tests/Fixtures/Integration/Models/Post.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class Post
5252
*/
5353
public $author = null;
5454

55+
/**
56+
* @Field(for="UpdatePostInput")
57+
* @var int
58+
*/
59+
public $views;
60+
5561
/**
5662
* @Field(for="UpdatePostInput")
5763
* @var string|null

0 commit comments

Comments
 (0)