Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Nullable vs Optional #503

Unanswered
cdomigan asked this question in Q&A
Jul 17, 2022 · 1 comments · 4 replies
Discussion options

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! :)

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
Comment options

@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.

Comment options

@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?

Comment options

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 :-)

Comment options

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /