Skip to content

Consider generating a sealed interface with an Unknown case for Union types in response-based codegen #6809

@StylianosGakis

Description

@StylianosGakis

Use case

Given a Union like:

union SomeUnion = FirstCase|SecondCase|ThirdCase
And a type:
type TypeContent {
  content: SomeUnion!
}

And in my query I got:

fragment "" on "" {
 content {
  ...ContentFragment
 }
}

fragment ContentFragment on SomeUnion {
  ...FirstFragment
  ...SecondFragment
  ...ThirdFragment
}

Then in my code at some point I am doing:

fun ContentFragment.toFoo(): Foo {
 when(this) {
  is FirstFragment -> ...
  is SecondFragment -> ...
  is ThirdFragment -> ...
  else -> 
 }
}

Describe the solution you'd like

That else in the last function means that if a new Union type is added to my schema my code won't catch the fact that I am not handling the new case, since it is "covered" by the else ->
Improving this to be part of a sealed hierarchy would be a great way to prevent accidentally making such a mistake

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions