Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<GraphQLDescription>()?.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>): 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<String> {
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<String> {
val deprecationReason: String? = this.getDeprecationReason()?.let { "deprecated" }
internal fun KAnnotatedElement.graphQLDescription(): String? = this.findAnnotation<GraphQLDescription>()?.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<Deprecated>()?.getReason()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down