From b8105b26e64aff00e112613aebac790fe969ead4 Mon Sep 17 00:00:00 2001 From: Dariusz Kuc Date: Wed, 28 Nov 2018 12:36:14 -0600 Subject: [PATCH] Remove directive info from descriptions Resolves https://github.com/ExpediaDotCom/graphql-kotlin/issues/79 --- .../schema/extensions/annotationExtensions.kt | 55 +------------------ .../schema/generator/DirectiveTests.kt | 13 ----- 2 files changed, 2 insertions(+), 66 deletions(-) diff --git a/src/main/kotlin/com/expedia/graphql/schema/extensions/annotationExtensions.kt b/src/main/kotlin/com/expedia/graphql/schema/extensions/annotationExtensions.kt index c2e67f5295..a0a97faab8 100644 --- a/src/main/kotlin/com/expedia/graphql/schema/extensions/annotationExtensions.kt +++ b/src/main/kotlin/com/expedia/graphql/schema/extensions/annotationExtensions.kt @@ -16,60 +16,9 @@ import kotlin.reflect.KParameter import kotlin.reflect.full.findAnnotation import com.expedia.graphql.annotations.GraphQLDirective as DirectiveAnnotation -internal fun KAnnotatedElement.graphQLDescription(): String? { - val directiveNames = listOfDirectives().map { it.normalizeDirectiveName() } - val description = this.findAnnotation()?.value - return formatGraphQLDescription(description, directiveNames) -} - -internal fun Field.graphQLDescription(): String? { - val directiveNames = listOfDirectives().map { it.normalizeDirectiveName() } - val description = this.getAnnotation(GraphQLDescription::class.java)?.value - return formatGraphQLDescription(description, directiveNames) -} - -private fun formatGraphQLDescription(description: String?, directiveNames: List): String? = when { - description != null && directiveNames.isNotEmpty() -> - """$description - | - |Directives: ${directiveNames.joinToString(", ")} - """.trimMargin() - description == null && directiveNames.isNotEmpty() -> - "Directives: ${directiveNames.joinToString(", ")}" - else -> description -} - -private fun KAnnotatedElement.listOfDirectives(): List { - val deprecationReason: String? = this.getDeprecationReason()?.let { "deprecated" } - - return this.annotations.asSequence() - .mapNotNull { it.getDirectiveInfo() } - .map { - when { - it.effectiveName.isNullOrEmpty().not() -> "@${it.effectiveName}" - else -> null - } - } - .plus(deprecationReason) - .filterNotNull() - .toList() -} - -private fun Field.listOfDirectives(): List { - val deprecationReason: String? = this.getDeprecationReason()?.let { "deprecated" } +internal fun KAnnotatedElement.graphQLDescription(): String? = this.findAnnotation()?.value - return this.declaredAnnotations.asSequence() - .mapNotNull { it.getDirectiveInfo() } - .map { - when { - it.effectiveName.isNullOrEmpty().not() -> "@${it.effectiveName}" - else -> null - } - } - .plus(deprecationReason) - .filterNotNull() - .toList() -} +internal fun Field.graphQLDescription(): String? = this.getAnnotation(GraphQLDescription::class.java)?.value internal fun KAnnotatedElement.getDeprecationReason(): String? = this.findAnnotation()?.getReason() diff --git a/src/test/kotlin/com/expedia/graphql/schema/generator/DirectiveTests.kt b/src/test/kotlin/com/expedia/graphql/schema/generator/DirectiveTests.kt index 6f68921ab0..fd194bba7c 100644 --- a/src/test/kotlin/com/expedia/graphql/schema/generator/DirectiveTests.kt +++ b/src/test/kotlin/com/expedia/graphql/schema/generator/DirectiveTests.kt @@ -6,7 +6,6 @@ import com.expedia.graphql.schema.testSchemaConfig import com.expedia.graphql.toSchema import graphql.Scalars import graphql.introspection.Introspection -import graphql.schema.GraphQLInputObjectType import graphql.schema.GraphQLNonNull import graphql.schema.GraphQLObjectType import org.junit.jupiter.api.Test @@ -24,21 +23,9 @@ class DirectiveTests { val deprecatedField = result?.getFieldDefinition("deprecatedField") assertEquals(deprecatedField?.isDeprecated, true) - assertEquals("Directives: deprecated", deprecatedField?.description) assertEquals("this field is deprecated", deprecatedField?.deprecationReason) } - @Test - fun `SchemaGenerator includes deprecated notice for deprecated fields`() { - val schema = toSchema(listOf(TopLevelObjectDef(QueryWithDeprecatedFields())), config = testSchemaConfig) - val topLevelQuery = schema.getObjectType("TopLevelQuery") - val query = topLevelQuery.getFieldDefinition("deprecatedArgumentQuery") - val argument = query.getArgument("input") - val deprecatedInputField = ((argument.type as? GraphQLNonNull)?.wrappedType as? GraphQLInputObjectType)?.getFieldDefinition("deprecatedField") - - assertEquals("Directives: deprecated", deprecatedInputField?.description) - } - @Test fun `SchemaGenerator marks deprecated queries and documents replacement`() { val schema = toSchema(listOf(TopLevelObjectDef(QueryWithDeprecatedFields())), config = testSchemaConfig)