Skip to content

Commit 95f27dc

Browse files
Add deprecated annotation to generation query classes. (#674)
1 parent d33cbe2 commit 95f27dc

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

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

+27-12
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
6767
if (it.description != null) {
6868
javaType.addJavadoc(it.description.sanitizeJavaDoc())
6969
}
70+
71+
val deprecatedClassDirective = getDeprecateDirective(it)
72+
if (deprecatedClassDirective != null) {
73+
javaType.addAnnotation(java.lang.Deprecated::class.java)
74+
val deprecationReason = getDeprecatedReason(deprecatedClassDirective)
75+
if (deprecationReason != null) {
76+
javaType.addJavadoc("@deprecated " + deprecationReason.sanitizeJavaDoc())
77+
}
78+
}
79+
7080
javaType.addMethod(
7181
MethodSpec.methodBuilder("getOperationName")
7282
.addModifiers(Modifier.PUBLIC)
@@ -116,18 +126,8 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
116126
it.inputValueDefinitions.forEach { inputValue ->
117127
val findReturnType = TypeUtils(getDatatypesPackageName(), config, document).findReturnType(inputValue.type)
118128

119-
val deprecatedDirective = if (config.addDeprecatedAnnotation) {
120-
inputValue
121-
.getDirectives("deprecated")
122-
?.firstOrNull() // Should we throw here, if there are multiple "@deprecated"?
123-
} else {
124-
null
125-
}
126-
127-
val deprecationReason = deprecatedDirective
128-
?.getArgument("reason")
129-
?.let { it.value as? StringValue }
130-
?.value
129+
val deprecatedDirective = getDeprecateDirective(inputValue)
130+
val deprecationReason = deprecatedDirective?.let { it1 -> getDeprecatedReason(it1) }
131131

132132
val methodBuilder = MethodSpec.methodBuilder(ReservedKeywordSanitizer.sanitize(inputValue.name))
133133
.addParameter(findReturnType, ReservedKeywordSanitizer.sanitize(inputValue.name))
@@ -636,6 +636,21 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
636636
return javaType to codeGenResult.merge(concreteTypesResult).merge(unionTypesResult)
637637
}
638638

639+
private fun getDeprecateDirective(node: DirectivesContainer<*>): Directive? {
640+
if (config.addDeprecatedAnnotation) {
641+
return node
642+
.getDirectives("deprecated")
643+
?.firstOrNull() // Should we throw here, if there are multiple "@deprecated"?
644+
}
645+
return null
646+
}
647+
648+
private fun getDeprecatedReason(directive: Directive): String? {
649+
return directive
650+
?.getArgument("reason")
651+
?.let { it.value as? StringValue }
652+
?.value
653+
}
639654
private fun truncatePrefix(prefix: String): String {
640655
return if (config.shortProjectionNames) ClassnameShortener.shorten(prefix) else prefix
641656
}

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

+27
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,33 @@ class CodeGenTest {
995995
assertCompilesJava(codeGenResult.javaEnumTypes)
996996
}
997997

998+
@Test
999+
fun generateDataWithReservedKeywords() {
1000+
val schema = """
1001+
type Query {
1002+
people: [Person]
1003+
}
1004+
1005+
type Person {
1006+
package: Parcel
1007+
}
1008+
1009+
type Parcel {
1010+
name: String
1011+
}
1012+
""".trimIndent()
1013+
1014+
val codeGenResult = CodeGen(
1015+
CodeGenConfig(
1016+
schemas = setOf(schema),
1017+
packageName = basePackageName,
1018+
generateClientApi = true
1019+
)
1020+
).generate()
1021+
1022+
assertCompilesJava(codeGenResult)
1023+
}
1024+
9981025
@Nested
9991026
inner class EnumAnnotationTest {
10001027
@Test

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

+30
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,36 @@ class ClientApiGenBuilderTest {
142142

143143
@Nested
144144
inner class Deprecation {
145+
@Test
146+
fun `adds @Deprecated annotation on class and reason from schema directives when setting enabled`() {
147+
val schema = """
148+
type Query {
149+
filter(
150+
nameFilter: String,
151+
idFilter: ID
152+
): [String] @deprecated(reason: "DO NOT USE")
153+
}
154+
""".trimIndent()
155+
156+
val codeGenResult = CodeGen(
157+
CodeGenConfig(
158+
schemas = setOf(schema),
159+
packageName = basePackageName,
160+
generateClientApiv2 = true,
161+
maxProjectionDepth = 2,
162+
addDeprecatedAnnotation = true
163+
)
164+
).generate()
165+
166+
assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1)
167+
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("FilterGraphQLQuery")
168+
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.typeSpecs).hasSize(1)
169+
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.typeSpecs[0].methodSpecs).hasSize(4)
170+
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.typeSpecs[0].methodSpecs[1].name).isEqualTo("nameFilter")
171+
assertThat(codeGenResult.javaQueryTypes[0].typeSpec.javadoc.toString()).startsWith(
172+
"@deprecated DO NOT USE".trimMargin()
173+
)
174+
}
145175

146176
@Test
147177
fun `adds @Deprecated annotation and reason from schema directives when setting enabled`() {

0 commit comments

Comments
 (0)