are there any docs/examples for how union types ar...
# apollo-kotlin
j
are there any docs/examples for how union types are treated in Apollo Kotlin?
b
hmm I don't think we have specific docs, but the result is essentially the same as with interfaces. So basically you need to use fragments for the different possible cases.
j
Ok, thanks
πŸ‘ 1
Sort of related question....to support this new union type we're going from something like
Copy code
components {
        ...componentInfo
    }
to
Copy code
elements {
        ... on Component { ...componentInfo }
    }
Instead of having
Component
type we now have
OnComponent
.....is there some way to keep/generate that
Component
type?
b
I don't think so πŸ˜…. That model's name is based on the field's name - but it is gone? You should see an
Element
model though. But it looks like it's not just the fields that have changed, but also the structure? So I don't think you'll avoid the rename here.
j
It would just allow not having to change quite a lot of code that currently expects
Component
type....but that's fine.....that's what refactoring tools are for πŸ™‚
Both
Component
before (and
OnComponent
now) contain same info (componentInfo)
πŸ‘ 1
b
haha yeah πŸ™‚ Maybe you can even use "structural search and replace" (but last few times I tried, it failed me..., a good old regexp was the solution)
j
had wondered maybe if there was some kind of alias feature
but as you say the structure would be different
b
so, if you use an alias in the query, that will also change the name of the model
Copy code
components: elements {
        ... on Component { ...componentInfo }
   }
But I think that may be very confusing in the end πŸ™‚ Maybe it can help as an intermediary refactoring step.
πŸ‘ 1
j
have started using this and right now handling by checking if various values in union are null or not......looks like there's way to setup use of sealed interfaces....and, related to that, is it possible to do that only in some cases or is it a global option?
b
You may be thinking of the response based codegen? If so, it's a global affair.
j
ah, ok.....yeah, switching to that would be very significant change at this point.....think I'll stick to way we're handling them right now πŸ˜ƒ
is that the general approach used when dealing with union types like this (when not using response based codegen)?
b
switching to that would be very significant change
Yeah πŸ™‚ also there are a few caveats.
is that the general approach used
Yes I think that's it. I mean you can also look at the value of
__typename
but that may actually be less typesafe? Or maybe it's the same... in both cases what's missing is a way to be sure to be exhaustive I guess?
j
yeah, exactly
and just generally more idiomatic I guess
it's fine though....it's only a small thing
πŸ‘ 1