@@ -10,26 +10,33 @@ import com.google.common.base.CaseFormat
1010import graphql.schema.GraphQLArgument
1111import graphql.schema.GraphQLDirective
1212import graphql.schema.GraphQLInputType
13+ import java.lang.reflect.Field
1314import kotlin.reflect.KAnnotatedElement
1415import kotlin.reflect.KParameter
1516import kotlin.reflect.full.findAnnotation
1617import com.expedia.graphql.annotations.GraphQLDirective as DirectiveAnnotation
1718
1819internal fun KAnnotatedElement.graphQLDescription (): String? {
1920 val directiveNames = listOfDirectives().map { it.normalizeDirectiveName() }
20-
2121 val description = this .findAnnotation<GraphQLDescription >()?.value
22+ return formatGraphQLDescription(description, directiveNames)
23+ }
24+
25+ internal fun Field.graphQLDescription (): String? {
26+ val directiveNames = listOfDirectives().map { it.normalizeDirectiveName() }
27+ val description = this .getAnnotation(GraphQLDescription ::class .java)?.value
28+ return formatGraphQLDescription(description, directiveNames)
29+ }
2230
23- return when {
24- description != null && directiveNames.isNotEmpty() ->
25- """ $description
31+ private fun formatGraphQLDescription ( description : String? , directiveNames : List < String >): String? = when {
32+ description != null && directiveNames.isNotEmpty() ->
33+ """ $description
2634 |
2735 |Directives: ${directiveNames.joinToString(" , " )}
2836 """ .trimMargin()
29- description == null && directiveNames.isNotEmpty() ->
30- " Directives: ${directiveNames.joinToString(" , " )} "
31- else -> description
32- }
37+ description == null && directiveNames.isNotEmpty() ->
38+ " Directives: ${directiveNames.joinToString(" , " )} "
39+ else -> description
3340}
3441
3542private fun KAnnotatedElement.listOfDirectives (): List <String > {
@@ -48,13 +55,32 @@ private fun KAnnotatedElement.listOfDirectives(): List<String> {
4855 .toList()
4956}
5057
51- internal fun KAnnotatedElement.getDeprecationReason (): String? {
52- val annotation = this .findAnnotation<Deprecated >() ? : return null
58+ private fun Field.listOfDirectives (): List <String > {
59+ val deprecationReason: String? = this .getDeprecationReason()?.let { " deprecated" }
60+
61+ return this .declaredAnnotations.asSequence()
62+ .mapNotNull { it.getDirectiveInfo() }
63+ .map {
64+ when {
65+ it.effectiveName.isNullOrEmpty().not () -> " @${it.effectiveName} "
66+ else -> null
67+ }
68+ }
69+ .plus(deprecationReason)
70+ .filterNotNull()
71+ .toList()
72+ }
73+
74+ internal fun KAnnotatedElement.getDeprecationReason (): String? = this .findAnnotation<Deprecated >()?.getReason()
75+
76+ internal fun Field.getDeprecationReason (): String? = this .getDeclaredAnnotation(Deprecated ::class .java)?.getReason()
77+
78+ internal fun Deprecated.getReason (): String? {
5379 val builder = StringBuilder ()
54- builder.append(annotation .message)
55- if (! annotation .replaceWith.expression.isBlank()) {
80+ builder.append(this .message)
81+ if (! this .replaceWith.expression.isBlank()) {
5682 builder.append(" , replace with " )
57- builder.append(annotation .replaceWith.expression)
83+ builder.append(this .replaceWith.expression)
5884 }
5985 return builder.toString()
6086}
0 commit comments