Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,16 @@ class ClientApiGenerator(
val javaType = subProjection.first
val codeGenResult = subProjection.second

// Automatically include __typename when the field type is interface to maintain parity with fragment projections
if (type is InterfaceTypeDefinition) {
javaType.addInitializerBlock(
CodeBlock
.builder()
.addStatement("getFields().put(\$S, null)", TypeNameMetaFieldDef.name)
Comment thread
jiholee17 marked this conversation as resolved.
.build(),
)
}

val javaFile = JavaFile.builder(getPackageName(), javaType.build()).build()
return CodeGenResult(clientProjections = listOf(javaFile)).merge(codeGenResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,46 @@ class ClientApiGenProjectionTest {
)
}

@Test
fun testInterfaceProjectionIncludesTypenameAutomatically() {
val schema =
"""
type Query {
account: Account
}

type Account {
subscriber: Subscriber
}

interface Subscriber {
id: ID
}

type PremiumSubscriber implements Subscriber {
id: ID
tier: String
}

type BasicSubscriber implements Subscriber {
id: ID
}
""".trimIndent()

val codeGenResult =
CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = BASE_PACKAGE_NAME,
generateClientApi = true,
),
).generate()

val subscriberProjection = codeGenResult.clientProjections.first { it.typeSpec().name() == "SubscriberProjection" }
assertThat(subscriberProjection.typeSpec().initializerBlock().toString()).contains("__typename")
assertCompilesJava(codeGenResult)
}

@Test
fun testScalarsDontGenerateProjections() {
val schema =
Expand Down