-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Input Object Builders, should non optional arguments be in the constructor? #5375
Comments
Taking a step back, I wanted to ask a question which I perhaps am lacking some context for. Why does the builder exist in the first place for this? Is there any historical context? Perhaps easier to use from Java? Is it necessary for some non-trivial examples? Personally I would be super happy just with
and I could just use that constructor directly, then that would leave no room for errors. That way I can also express With all that said, if I was using the builder myself, I would again appreciate making mistakes impossible, rather than having a more "standard" builder API, which then gives me runtime crashes if I forget to add something. But I think if the context as to why this option was added was explained in this issue too, it would help understand who is using it and why, to make a better judgement on this trade-off. |
Thanks @StylianosGakis you are right that some context about the builders themselves is helpful! They were introduced here which provides some info, and the TLDR is:
Edit: forgot to mention builders also help with the issue mentioned here (albeit moving non-optional arguments to the builder's constructor brings it back) |
Right so for folks that might prefer the builder approach to get one of the three options (absent, present + null, present + non-null). So while I do not see myself enabling this flag, if I did, I would definitely prefer the "can't call this wrong" approach, aka the one where the necessary parameters are part of the builder constructor. That's my feedback here 😊 |
Note that with the constructor since we have default values, the risk to forget one of the parameters is also there to some degree.
Thanks for the feedback! 🙏 |
In v4, a codegen option
generateInputBuilders
has been added to generate builders for Input Objects.For this input object:
the generated class will look like this:
The check for non optional arguments is done at runtime in the
build()
method.Usage:
Putting them in the builder's constructor would make this a build time check:
Usage:
However:
Other considerations:
@oneOf
input objects, we would generate only builder constructors for each input field.The text was updated successfully, but these errors were encountered: