Skip to content

Commit 418cd09

Browse files
committed
Explicitly use client package for generated representations
1 parent 03155cc commit 418cd09

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/EntitiesRepresentationTypeGenerator.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ class EntitiesRepresentationTypeGenerator(
7777
)
7878
) {
7979
val fieldTypeRepresentationName = toRepresentationName(type)
80-
val fieldRepresentationType =
81-
typeUtils
82-
.findReturnType(it.type)
83-
.toString()
84-
.replace(type.name, fieldTypeRepresentationName)
80+
val fieldRepresentationType = typeUtils.qualifyName(fieldTypeRepresentationName)
8581

8682
if (generatedRepresentations.containsKey(fieldTypeRepresentationName)) {
8783
logger.trace("Representation for {} was already generated.", fieldTypeRepresentationName)

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin/KotlinEntitiesRepresentationTypeGenerator.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ class KotlinEntitiesRepresentationTypeGenerator(
8080
) {
8181
val fieldType = typeUtils.findReturnType(it.type)
8282
val fieldTypeRepresentationName = toRepresentationName(type)
83-
val fieldRepresentationType =
84-
fieldType
85-
.toString()
86-
.replace(type.name, fieldTypeRepresentationName)
87-
.removeSuffix("?")
83+
val fieldRepresentationType = typeUtils.qualifyName(fieldTypeRepresentationName)
8884

8985
if (generatedRepresentations.containsKey(fieldTypeRepresentationName)) {
9086
logger.trace("Representation for {} was already generated.", fieldTypeRepresentationName)

graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/EntitiesClientApiGenTest.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,46 @@ class EntitiesClientApiGenTest {
583583
}
584584
}
585585

586+
@Test
587+
fun `Entity representation types should use client package not type-mapped package`() {
588+
val schema =
589+
"""
590+
type Channel @key(fields: "id type") {
591+
id: ID! @external
592+
type: ChatType @external
593+
name: String
594+
}
595+
596+
enum ChatType {
597+
PUBLIC
598+
PRIVATE
599+
DM
600+
}
601+
""".trimIndent()
602+
603+
val codeGenResult =
604+
CodeGen(
605+
CodeGenConfig(
606+
schemas = setOf(schema),
607+
packageName = BASE_PACKAGE_NAME,
608+
generateClientApi = true,
609+
generateDataTypes = false,
610+
typeMapping = mapOf("ChatType" to "com.external.types.ChatType"),
611+
),
612+
).generate()
613+
614+
val representations = codeGenResult.javaDataTypes.filter { "Representation" in it.typeSpec.name }
615+
assertThat(representations).hasSize(2)
616+
617+
val channelRepresentation = representations.find { it.typeSpec.name == "ChannelRepresentation" }
618+
assertThat(channelRepresentation).isNotNull
619+
620+
val typeField = channelRepresentation!!.typeSpec.fieldSpecs.find { it.name == "type" }
621+
assertThat(typeField).isNotNull
622+
assertThat(typeField!!.type.toString())
623+
.isEqualTo("$BASE_PACKAGE_NAME.client.ChatTypeRepresentation")
624+
}
625+
586626
companion object {
587627
fun codeGen(schema: String): CodeGenResult =
588628
CodeGen(

0 commit comments

Comments
 (0)