Chris Fillmore
08/03/2023, 7:48 PMvalue class
types for various data model id
values in my app. They are all uuid strings underneath. My app also deals with e-commerce stuff via a graphql backend, and I’ve asked the backend dev to provide a custom scalar for a value that represents a discount code.
I am unsure how far to take this idea. What is the use case for, say, String
, when every value has some additional semantics? Sure, an email client might have a messageBody: String
. But why not messageBody: MessageBody
?
I realize this question is somewhat language-agnostic, but I am thinking about it specifically in the context of my Kotlin app, so I hope it is appropriate for this channel. I appreciate any opinions here.mbonnin
08/03/2023, 7:56 PMMessageBody
is Text(String)
then there's no need to introduce the value classmbonnin
08/03/2023, 7:57 PMMessageBody
(MessageBody.toHtml()
, MessageBody.toMarkdown()
, etc...) then I guess why notmbonnin
08/03/2023, 7:58 PMChris Fillmore
08/03/2023, 8:02 PMmbonnin
08/03/2023, 8:06 PMSaharath Kleips
08/03/2023, 8:21 PMMessageBody
because I can make some sort of meaningful assumption about its contents?
I think in terms of GraphQL, date time is a good example? You’re probably serializing it as a string (I suppose it could be an int as well) and being able to assert and make assumptions about its format has a lot of value (imo).
As someone who owns a graphql server, an over abundance of custom scalars can be overwhelming and intimidating for clients trying to onboard to the API, especially to those who aren’t familiar with graphql… But for instance our graphql api returns a custom scalar for page cursors and accepts cursors as input values - which can intuitively indicate to a developer that the two values are related.
But I think overall restricting the type just depends on when and where it makes sense. Perhaps it doesn’t make sense for the GraphQL API to have a custom scalar for a discount code, but maybe you want to treat it as such within your own use case?Chris Fillmore
08/03/2023, 8:38 PMIf the next function that is going to take yourThis is a good point. I suppose the difference here between that and a domain modelisMessageBody
then there’s no need to introduce the value classText(String)
id
is that I’m likely to use the id
without it ever being converted to a string.
Thanks for the feedback so far, this actually clarifies a lot for me