Skip to content

Commit 1ee8541

Browse files
author
Dariusz Kuc
authored
[generator] support input field deprecation (#1361)
With recent GraphQL spec changes, deprecation should be supported on fields, input fields (new), arguments (new) and enum values. This change adds the support for deprecating input fields. Argument deprecation is not supported as Kotlin `@Deprecated` is not applicable to parameters. See https://youtrack.jetbrains.com/issue/KT-25643
1 parent 520011b commit 1ee8541

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/types/generateArgument.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal fun generateArgument(generator: SchemaGenerator, parameter: KParameter)
5050
val typeInfo = GraphQLKTypeMetadata(inputType = true, fieldName = parameter.getName(), fieldAnnotations = parameter.annotations)
5151
val graphQLType = generateGraphQLType(generator = generator, type = unwrappedType, typeInfo)
5252

53-
// Deprecation of arguments is currently unsupported: https://github.com/facebook/graphql/issues/197
53+
// Deprecation of arguments is currently unsupported: https://youtrack.jetbrains.com/issue/KT-25643
5454
val builder = GraphQLArgument.newArgument()
5555
.name(parameter.getName())
5656
.description(parameter.getGraphQLDescription())

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/types/generateInputProperty.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Expedia, Inc
2+
* Copyright 2022 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,9 @@
1717
package com.expediagroup.graphql.generator.internal.types
1818

1919
import com.expediagroup.graphql.generator.SchemaGenerator
20+
import com.expediagroup.graphql.generator.directives.deprecatedDirectiveWithReason
2021
import com.expediagroup.graphql.generator.internal.extensions.getPropertyAnnotations
22+
import com.expediagroup.graphql.generator.internal.extensions.getPropertyDeprecationReason
2123
import com.expediagroup.graphql.generator.internal.extensions.getPropertyDescription
2224
import com.expediagroup.graphql.generator.internal.extensions.getPropertyName
2325
import com.expediagroup.graphql.generator.internal.extensions.safeCast
@@ -29,18 +31,22 @@ import kotlin.reflect.KClass
2931
import kotlin.reflect.KProperty
3032

3133
internal fun generateInputProperty(generator: SchemaGenerator, prop: KProperty<*>, parentClass: KClass<*>): GraphQLInputObjectField {
32-
val builder = GraphQLInputObjectField.newInputObjectField()
33-
34-
// Verfiy that the unwrapped GraphQL type is a valid input type
34+
// Verify that the unwrapped GraphQL type is a valid input type
3535
val inputTypeFromHooks = generator.config.hooks.willResolveInputMonad(prop.returnType)
3636
val unwrappedType = inputTypeFromHooks.unwrapOptionalInputType()
3737
val propertyName = prop.getPropertyName(parentClass)
3838
val typeInfo = GraphQLKTypeMetadata(inputType = true, fieldName = propertyName, fieldAnnotations = prop.getPropertyAnnotations(parentClass))
3939
val graphQLInputType = generateGraphQLType(generator = generator, type = unwrappedType, typeInfo).safeCast<GraphQLInputType>()
4040

41-
builder.name(propertyName)
42-
builder.description(prop.getPropertyDescription(parentClass))
43-
builder.type(graphQLInputType)
41+
val builder = GraphQLInputObjectField.newInputObjectField()
42+
.name(propertyName)
43+
.description(prop.getPropertyDescription(parentClass))
44+
.type(graphQLInputType)
45+
46+
prop.getPropertyDeprecationReason(parentClass)?.let {
47+
builder.deprecate(it)
48+
builder.withDirective(deprecatedDirectiveWithReason(it))
49+
}
4450

4551
generateDirectives(generator, prop, DirectiveLocation.INPUT_FIELD_DEFINITION, parentClass).forEach {
4652
builder.withDirective(it)

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/types/GenerateInputPropertyTest.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2022 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,21 +21,21 @@ import com.expediagroup.graphql.generator.annotations.GraphQLName
2121
import com.expediagroup.graphql.generator.test.utils.SimpleDirective
2222
import org.junit.jupiter.api.Test
2323
import kotlin.test.assertEquals
24+
import kotlin.test.assertTrue
2425

2526
internal class GenerateInputPropertyTest : TypeTestHelper() {
2627

2728
private data class InputPropertyTestClass(
2829
@GraphQLDescription("Custom description")
2930
val description: String,
30-
3131
@GraphQLName("newName")
3232
val changeMe: String,
33-
3433
@SimpleDirective
3534
val directiveWithNoPrefix: String,
36-
3735
@property:SimpleDirective
38-
val directiveWithPrefix: String
36+
val directiveWithPrefix: String,
37+
@Deprecated("This field is deprecated")
38+
val deprecatedDescription: String? = null
3939
)
4040

4141
@Test
@@ -60,4 +60,11 @@ internal class GenerateInputPropertyTest : TypeTestHelper() {
6060
assertEquals(1, resultWithPrefix.directives.size)
6161
assertEquals("simpleDirective", resultWithPrefix.directives.first().name)
6262
}
63+
64+
@Test
65+
fun `Input properties can be deprecated`() {
66+
val deprecatedResult = generateInputProperty(generator, InputPropertyTestClass::deprecatedDescription, InputPropertyTestClass::class)
67+
assertTrue(deprecatedResult.isDeprecated)
68+
assertEquals("This field is deprecated", deprecatedResult.deprecationReason)
69+
}
6370
}

website/docs/schema-generator/customizing-schemas/deprecating-schema.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ type Query {
2626
```
2727

2828
While you can deprecate any fields/functions/classes in your Kotlin code, GraphQL only supports deprecation directive on
29-
the fields (which correspond to Kotlin fields and functions) and enum values.
29+
the fields (which correspond to Kotlin properties and functions), input fields and enum values.
3030

31-
Deprecation of input types is not yet supported [in the GraphQL spec](https://github.com/graphql/graphql-spec/pull/525).
31+
Deprecation of arguments is currently not supported [in Kotlin](https://youtrack.jetbrains.com/issue/KT-25643).

0 commit comments

Comments
 (0)