Skip to content

Commit 9c3cfe4

Browse files
dariuszkucjbrwn
andauthored
[generator] add runtime warning on deprecated context usage (#1323)
### 📝 Description This PR updates `FunctionDataFetcher` so that it logs a warning when injecting a `GraphQLContext`. This functionality is [deprecated](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/website/docs/schema-generator/execution/contextual-data.md#interface-injection-deprecated). Unlike the `GraphQLContext` interface itself, which is annotated `@deprecated, there is no compile or runtime time warning when using `GraphQLContext` interface injection in a query or mutation. ### 🔗 Related Issues * discussion: #1305 * supersedes: #1306 Co-authored-by: Joel Brown <[email protected]>
1 parent e4868f9 commit 9c3cfe4

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

generator/graphql-kotlin-schema-generator/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ val jacksonVersion: String by project
66
val kotlinCoroutinesVersion: String by project
77
val rxjavaVersion: String by project
88
val junitVersion: String by project
9+
val slf4jVersion: String by project
910

1011
dependencies {
1112
api("com.graphql-java:graphql-java:$graphQLJavaVersion")
1213
api("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$kotlinCoroutinesVersion")
1314
api("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
1415
implementation("io.github.classgraph:classgraph:$classGraphVersion")
16+
implementation("org.slf4j:slf4j-api:$slf4jVersion")
1517
testImplementation("io.reactivex.rxjava3:rxjava:$rxjavaVersion")
1618
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
1719
}

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kFunctionExtensions.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616

1717
package com.expediagroup.graphql.generator.internal.extensions
1818

19+
import org.slf4j.Logger
20+
import org.slf4j.LoggerFactory
1921
import kotlin.reflect.KFunction
2022
import kotlin.reflect.KParameter
2123
import kotlin.reflect.full.valueParameters
2224

23-
internal fun KFunction<*>.getValidArguments(): List<KParameter> =
24-
this.valueParameters
25-
.filterNot { it.isGraphQLContext() }
26-
.filterNot { it.isGraphQLIgnored() }
27-
.filterNot { it.isDataFetchingEnvironment() }
25+
private val logger: Logger = LoggerFactory.getLogger("schemaGenerator")
26+
27+
internal fun KFunction<*>.getValidArguments(parentName: String): List<KParameter> = this.valueParameters.mapNotNull {
28+
when {
29+
it.isGraphQLIgnored() || it.isDataFetchingEnvironment() -> null
30+
it.isGraphQLContext() -> {
31+
logger.warn("$parentName.${getFunctionName()} relies on GraphQLContext injection which is deprecated. Please update it to retrieve context from DataFetchingEnvironment instead.")
32+
null
33+
}
34+
else -> it
35+
}
36+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal fun generateFunction(generator: SchemaGenerator, fn: KFunction<*>, pare
4545
builder.withDirective(it)
4646
}
4747

48-
fn.getValidArguments().forEach {
48+
fn.getValidArguments(parentName).forEach {
4949
builder.argument(generateArgument(generator, it))
5050
}
5151

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KFunctionExtensionsKtTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@ internal class KFunctionExtensionsKtTest {
2626

2727
@Test
2828
fun getValidArguments() {
29-
val args = TestingClass::happyPath.getValidArguments()
29+
val args = TestingClass::happyPath.getValidArguments("TestingClass")
3030
assertEquals(expected = 1, actual = args.size)
3131
assertEquals(expected = "color", actual = args.first().getName())
3232
}
3333

3434
@Test
3535
fun `getValidArguments should ignore @GraphQLIgnore`() {
36-
val args = TestingClass::ignored.getValidArguments()
36+
val args = TestingClass::ignored.getValidArguments("TestingClass")
3737
assertEquals(expected = 1, actual = args.size)
3838
assertEquals(expected = "notIgnored", actual = args.first().getName())
3939
}
4040

4141
@Test
4242
fun `getValidArguments should ignore GraphQLContext classes`() {
43-
val args = TestingClass::context.getValidArguments()
43+
val args = TestingClass::context.getValidArguments("TestingClass")
4444
assertEquals(expected = 1, actual = args.size)
4545
assertEquals(expected = "notContext", actual = args.first().getName())
4646
}
4747

4848
@Test
4949
fun `getValidArguments should ignore DataFetchingEnvironment`() {
50-
val args = TestingClass::dataFetchingEnvironment.getValidArguments()
50+
val args = TestingClass::dataFetchingEnvironment.getValidArguments("TestingClass")
5151
assertEquals(expected = 1, actual = args.size)
5252
assertEquals(expected = "notEnvironment", actual = args.first().getName())
5353
}

0 commit comments

Comments
 (0)