Skip to content

Commit da2884b

Browse files
committed
Add warning and test
1 parent 0a2e483 commit da2884b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/main/kotlin/graphql/kickstart/tools/resolver/MethodFieldResolver.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import graphql.schema.DataFetcher
1414
import graphql.schema.DataFetchingEnvironment
1515
import graphql.schema.GraphQLTypeUtil.isScalar
1616
import kotlinx.coroutines.future.future
17+
import org.slf4j.LoggerFactory
1718
import java.lang.reflect.Method
1819
import java.util.*
1920
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
@@ -31,6 +32,8 @@ internal class MethodFieldResolver(
3132
val method: Method
3233
) : FieldResolver(field, search, options, search.type) {
3334

35+
private val log = LoggerFactory.getLogger(javaClass)
36+
3437
private val additionalLastArgument =
3538
try {
3639
(method.kotlinFunction?.valueParameters?.size
@@ -97,8 +100,15 @@ internal class MethodFieldResolver(
97100
when (this.method.parameterTypes.last()) {
98101
null -> throw ResolverError("Expected at least one argument but got none, this is most likely a bug with graphql-java-tools")
99102
options.contextClass -> args.add { environment ->
100-
environment.graphQlContext[options.contextClass]
101-
?: environment.getContext() // TODO: remove deprecated use in next major release
103+
val context: Any? = environment.graphQlContext[options.contextClass]
104+
if (context != null) {
105+
context
106+
} else {
107+
log.warn("Generic context class has been deprecated by graphql-java. " +
108+
"To continue using a custom context class as the last parameter in resolver methods " +
109+
"please insert it into the new GraphQLContext class when building the ExecutionInput.")
110+
environment.getContext() // TODO: remove deprecated use in next major release
111+
}
102112
}
103113
GraphQLContext::class.java -> args.add { environment -> environment.graphQlContext }
104114
else -> args.add { environment -> environment }

src/test/kotlin/graphql/kickstart/tools/MethodFieldResolverDataFetcherTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ class MethodFieldResolverDataFetcherTest {
197197
assertEquals(resolver.get(createEnvironment(DataClass(), context = context)), true)
198198
}
199199

200+
@Test
201+
fun `data fetcher passes custom context if method has extra argument and custom context is specified as part of GraphQLContext`() {
202+
val customContext = ContextClass()
203+
val context = GraphQLContext.of(mapOf(ContextClass::class.java to customContext))
204+
val options = SchemaParserOptions.newOptions().contextClass(ContextClass::class).build()
205+
val resolver = createFetcher("active", options = options, resolver = object : GraphQLResolver<DataClass> {
206+
fun isActive(dataClass: DataClass, ctx: ContextClass): Boolean {
207+
return ctx == customContext
208+
}
209+
})
210+
211+
assertEquals(resolver.get(createEnvironment(DataClass(), context = context)), true)
212+
}
213+
200214
@Test
201215
fun `data fetcher marshalls input object if required`() {
202216
val name = "correct name"

0 commit comments

Comments
 (0)