-
Notifications
You must be signed in to change notification settings - Fork 103
-
Thank you for this fantastic library!
I've researched multiple threads and PRs that reference this issue, and discuss solutions, but unfortunately I'm still left scratching my head about handling the following scenario:
- I am on PHP 7.4, using Graphqlite v6
- I have a mutation that takes an Input type, in which I want to require all fields.
- Some of these fields can take a
null
value that represents unsetting the field value in the database.
For eg say I have a jobId
field in my Input type - I want this to take either an ID
or null
.
But if I define it as:
/**
* @Field
* @var ID|null $jobId;
*/
public ?ID $jobId;
... then in the situation where the jobId
field is omitted, the value defaults to null
(not uninitialized
as I was hoping), providing me with no way of distinguishing between an omitted value, and a provided null
value, and therefore potentially overwriting the value in the database.
How do I distinguish between nullable and optional fields in GraphQL Input types, using this library?
Sorry if I have missed something obvious here! :)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 4 replies
-
@cdomigan Have you had a look at the update
argument on Input
annotations?
https://graphqlite.thecodingmachine.io/docs/input-types#multiple-input-types-from-the-same-class
Beta Was this translation helpful? Give feedback.
All reactions
-
@oojacoboo thank you for your answer - yes I have looked at this, however it doesn't seem to do what I need.
It should be used when input type is used for a partial update, It makes all fields optional and removes all default values from thus prevents setting default values via setters or directly to public properties. In example above if you use the class as UpdateUserInput and set only username the other ones will be ignored. 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 to check if user actually passed a value for a certain field.
I don't want to allow a partial update - I want to require all fields.
Beta Was this translation helpful? Give feedback.
All reactions
-
@cdomigan So, you want to require all fields, and allow null to represent the "removal" of a field/value from an input type?
I'm not opposed to supporting uninitialized values as a default, instead of null. However, this would be a BC break. As I know people are relying on null checks to determine if a value has been set. I'm unsure on how this would work on 7.4, uninitialized properties of a class. Wasn't there an update around this in PHP8?
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks @oojacoboo. This seems to be a broader issue across the GraphQL community - with no clear way to distinguish between omitted and null. I'm going to alter my schema to use empty strings in place of nulls to represent clearing a value.
Thank you for your time :-)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
@cdomigan as @oojacoboo mentioned this should be supported since PHP8. There is also a related issue #474 which could help you.
I don't want to allow a partial update - I want to require all fields.
that is very much contradicting your described intend in your initial question and update
actually enables the ability to have properties uninitialized
as well as nullable just as you described.
If you want to have some fields not nullable, you can overwrite that as described here #371
Beta Was this translation helpful? Give feedback.