Replies: 4 comments 2 replies
-
To clarify, the problem with setting the optional field directly in the example above as DGS currently does is that an exception gets thrown because of a type mismatch. |
Beta Was this translation helpful? Give feedback.
-
Hi @ehardy, thank you for posting the proposal. I think we have two topics.
I think we should implement the second point right away. As of the first point, I like the idea but will like to discuss with the team how this could be supported by codgen. |
Beta Was this translation helpful? Give feedback.
-
Hello @berngp , I have created the following PR. As discussed, I have added the ability for setter methods in input objects to be called, falling back on the current behaviour of setting input fields directly. This is my first time writing Kotlin, so feel free to adjust if some things are not idiomatic Kotlin 😁 Some notes about the implementation. I have used Spring reflection capabilities in my implementation, as Spring was already used within DGS. The implementation is wrapped in the new Let me know if you have any feedback, will be happy to adjust if necessary. |
Beta Was this translation helpful? Give feedback.
-
PR #1094 was reviewed and merged and input object setter methods are now supported. Thus closing this discussion. |
Beta Was this translation helpful? Give feedback.
-
Hello, I would like to get feedback on the following idea and proposal. There is no out of the box way to easily determine if an optional field of an
Input
object has been explictly set tonull
or if it was simply not specified in the mutation request. We have been toying with an idea to provide an easy to use mechanism to address this situation, without forcing service developers to have to handle it themselves. The idea we've had was to create a special type calledOptionalField<T>
, similar tojava.util.Optional<T>
, but that could hold a third state,missing
. Here's an example of how it could be used:In the above example, the first three fields are mandatory, and the
nickName
field is optional. Optional input fields would be set toOptionalField.missing()
on instance creation, and only if setters are called during input object mapping, would optional fields be overridden with an explicitnull
or actual value.OptionalField<T>
would provide an elegant mechanism in mutation handling code to determine if a field needs to be set to null or other value, or if the current value should be preserved:So all of this works great on paper! But DGS, in the
DefaultInputObjectMapper
class, only supports setting input object fields directly, and does not call setter methods. I quickly hackedDefaultInputObjectMapper
to support calling setter methods to validate if the above idea would work, and it does.So would it make sense for
DefaultInputObjectMapper
to support calling setter methods if some exists? I'm thinking there could be different strategies for setting input object fields, the default being the current direct field access. But it would be useful for the above use case if there was a strategy, enabled via some kind of configuration, for calling setters first if they exists, and falling back to direct field access, for example.If the above makes sense, would be happy to contribute with a PR. Let me know.
Beta Was this translation helpful? Give feedback.
All reactions