I'm working with apollokotlin and for the first ti...
# apollo-kotlin
c
I'm working with apollokotlin and for the first time have encountered an enum. I've been taking the models that I get from apollo and mapping them to my own domain types, but I'm kinda confused on how to easily map an enum. Any tips? in other words. If I have this from apollo
Copy code
public enum class MyType(
  public val rawValue: String,
) {
  foo("foo"),
  bar("bar"),
then how best to convert that to my enum in my domain which looks identical to that. Or do I have to manually connect the dots?
s
Hmm what is the issue that you are facing with this enum exactly? What is “manually connecting the dots” in this case that looks different from any non-enum type? Not sure exactly, but there’s two things I can tell you for sure to consider when doing this mapping • There’s
sealedClassesForEnumsMatching
so that you generate sealed classes instead of enums, if that feels easier on your end • Adding a new type to the enum is considered a backwards compatible change for a GraphQL schema, so be sure to consider the “else” case and handle it appropriately, don’t throw on that else.
a
We do manual dot connecting 😄
as in a
when
on the API model to just map to the correct domain model
s
I may be confused, but how would the non-manual dot connecting look like? 😅
a
I'd guess something like using the raw value and do something like
valueOf
on that
s
Aha, but that sounds scary in the case where you do get new Enum values. Unless you add a valueOfOrNull maybe 😅
a
We've opted for separating the models so each and every model will need a mapper anyway, so we've just decided this isn't a big deal and just map everything
c
@annsofi yeah we map everything already since we want separation, but thought maybe i was missing something easy to somehow convert it. but I guess I can just define the new domain enum with all of the same values, and then just create the new enum with valueOf or something so i dont have to map each individual type in the enum
thanks all
s
How are you planning to support the cases where the backend values change and more enums come if you use valueOf and map 1:1?
c
app update?
maybe im missing the other option that you're recommending?
s
No, when the enum coming from the backend has added new enum values, but the user running the app is using an old version of the app where you may have not added those new fields to your mapped version of that enum
c
Yeah, I guess in that case I would just have some "other" option defined. I'm going to try to implement this when I'm back at my desk on monday. Maybe I'm missing something, but getting everoyones thoughts was helpful