Skip to content

Commit 4171c9e

Browse files
authored
Merge pull request #914 from Netflix/fix/subprojection-generic-params
Add generic params to scalar fields with inputs in sub projections
2 parents bf09441 + 53ab4a2 commit 4171c9e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ class ClientApiGenerator(
927927
val methodWithInputArgumentsBuilder =
928928
MethodSpec
929929
.methodBuilder(javaReservedKeywordSanitizer.sanitize(it.name))
930-
.returns(ClassName.get(getPackageName(), javaType.build().name()))
930+
.returns(TypeVariableName.get("$clazzName<PARENT, ROOT>"))
931931
.addCode(
932932
"""
933933
|getFields().put("${it.name}", null);

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,51 @@ class ClientApiGenProjectionTest {
927927
assertThat(methodWithArgs.parameters()[0].type().toString()).isEqualTo("java.lang.String")
928928
}
929929

930+
@Test
931+
fun `Scalar fields with inputs in sub projections should have generic parameters in query API`() {
932+
val schema =
933+
"""
934+
type Query {
935+
profile(filter: String): Profile
936+
}
937+
938+
type Profile {
939+
account: Account
940+
}
941+
942+
type Account {
943+
permissions(filter: [String!]!): [String!]
944+
name: String
945+
}
946+
""".trimIndent()
947+
948+
val codeGenResult =
949+
CodeGen(
950+
CodeGenConfig(
951+
schemas = setOf(schema),
952+
packageName = BASE_PACKAGE_NAME,
953+
generateClientApiv2 = true,
954+
),
955+
).generate()
956+
957+
val accountProjection =
958+
codeGenResult.clientProjections.find { it.typeSpec().name() == "AccountProjection" }
959+
?: fail("AccountProjection not found")
960+
961+
val permissionsMethod =
962+
accountProjection.typeSpec().methodSpecs().find {
963+
it.name() == "permissions" && it.parameters().isNotEmpty()
964+
} ?: fail("Method not found")
965+
966+
assertThat(permissionsMethod.returnType())
967+
.extracting { (it as TypeVariableName).name() }
968+
.isEqualTo("AccountProjection<PARENT, ROOT>")
969+
assertThat(permissionsMethod.parameters()[0].name()).isEqualTo("filter")
970+
assertThat(permissionsMethod.parameters()[0].type().toString()).isEqualTo("java.util.List<java.lang.String>")
971+
972+
assertCompilesJava(codeGenResult.clientProjections + codeGenResult.javaQueryTypes)
973+
}
974+
930975
@Test
931976
fun `Dedupe type names in Projections to support multiple schema files`() {
932977
val schema =

0 commit comments

Comments
 (0)