Skip to content

[generator] add runtime warning on deprecated context usage #1323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 9, 2021
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
2 changes: 2 additions & 0 deletions generator/graphql-kotlin-schema-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ val jacksonVersion: String by project
val kotlinCoroutinesVersion: String by project
val rxjavaVersion: String by project
val junitVersion: String by project
val slf4jVersion: String by project

dependencies {
api("com.graphql-java:graphql-java:$graphQLJavaVersion")
api("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$kotlinCoroutinesVersion")
api("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
implementation("io.github.classgraph:classgraph:$classGraphVersion")
implementation("org.slf4j:slf4j-api:$slf4jVersion")
testImplementation("io.reactivex.rxjava3:rxjava:$rxjavaVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@

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

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.full.valueParameters

internal fun KFunction<*>.getValidArguments(): List<KParameter> =
this.valueParameters
.filterNot { it.isGraphQLContext() }
.filterNot { it.isGraphQLIgnored() }
.filterNot { it.isDataFetchingEnvironment() }
private val logger: Logger = LoggerFactory.getLogger("schemaGenerator")

internal fun KFunction<*>.getValidArguments(parentName: String): List<KParameter> = this.valueParameters.mapNotNull {
when {
it.isGraphQLIgnored() || it.isDataFetchingEnvironment() -> null
it.isGraphQLContext() -> {
logger.warn("$parentName.${getFunctionName()} relies on GraphQLContext injection which is deprecated. Please update it to retrieve context from DataFetchingEnvironment instead.")
null
}
else -> it
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal fun generateFunction(generator: SchemaGenerator, fn: KFunction<*>, pare
builder.withDirective(it)
}

fn.getValidArguments().forEach {
fn.getValidArguments(parentName).forEach {
builder.argument(generateArgument(generator, it))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ internal class KFunctionExtensionsKtTest {

@Test
fun getValidArguments() {
val args = TestingClass::happyPath.getValidArguments()
val args = TestingClass::happyPath.getValidArguments("TestingClass")
assertEquals(expected = 1, actual = args.size)
assertEquals(expected = "color", actual = args.first().getName())
}

@Test
fun `getValidArguments should ignore @GraphQLIgnore`() {
val args = TestingClass::ignored.getValidArguments()
val args = TestingClass::ignored.getValidArguments("TestingClass")
assertEquals(expected = 1, actual = args.size)
assertEquals(expected = "notIgnored", actual = args.first().getName())
}

@Test
fun `getValidArguments should ignore GraphQLContext classes`() {
val args = TestingClass::context.getValidArguments()
val args = TestingClass::context.getValidArguments("TestingClass")
assertEquals(expected = 1, actual = args.size)
assertEquals(expected = "notContext", actual = args.first().getName())
}

@Test
fun `getValidArguments should ignore DataFetchingEnvironment`() {
val args = TestingClass::dataFetchingEnvironment.getValidArguments()
val args = TestingClass::dataFetchingEnvironment.getValidArguments("TestingClass")
assertEquals(expected = 1, actual = args.size)
assertEquals(expected = "notEnvironment", actual = args.first().getName())
}
Expand Down