@@ -19,6 +19,8 @@ import kotlinx.coroutines.Dispatchers
1919import kotlinx.coroutines.Job
2020import kotlinx.coroutines.channels.Channel
2121import kotlinx.coroutines.channels.ReceiveChannel
22+ import org.junit.Assert.assertThrows
23+ import org.junit.Assert.assertTrue
2224import org.junit.Test
2325import org.reactivestreams.Publisher
2426import org.reactivestreams.tck.TestEnvironment
@@ -98,7 +100,7 @@ class MethodFieldResolverDataFetcherTest {
98100 }
99101
100102 @Test
101- fun `data fetcher executes suspend function in CoroutineScope from legacy context` () {
103+ fun `data fetcher does not execute suspend function in CoroutineScope from legacy context` () {
102104 // setup
103105 val scopeClass = ScopeClass ()
104106
@@ -107,7 +109,7 @@ class MethodFieldResolverDataFetcherTest {
107109 // expect
108110 @Suppress(" UNCHECKED_CAST" )
109111 val future = resolver.get(createEnvironment(DataClass (), legacyContext = scopeClass.scope)) as CompletableFuture <Boolean >
110- assert (future.get())
112+ assertEquals (future.get(), false )
111113 }
112114
113115 class ScopeClass : GraphQLResolver <DataClass > {
@@ -116,7 +118,7 @@ class MethodFieldResolverDataFetcherTest {
116118
117119 @Suppress(" UNUSED_PARAMETER" )
118120 suspend fun isActive (data : DataClass ): Boolean {
119- return coroutineContext[Job ] == job.children.first ()
121+ return coroutineContext[Job ] == job.children.firstOrNull ()
120122 }
121123 }
122124
@@ -275,6 +277,47 @@ class MethodFieldResolverDataFetcherTest {
275277 assertEquals(resolver.get(createEnvironment(DataClass (), context = context)), true )
276278 }
277279
280+ @Test
281+ fun `data fetcher fails if custom context is missing from GraphQLContext` () {
282+ val options = SchemaParserOptions .newOptions().contextClass(ContextClass ::class ).build()
283+ val resolver = createFetcher(" active" , options = options, resolver = object : GraphQLResolver <DataClass > {
284+ fun isActive (dataClass : DataClass , ctx : ContextClass ? ): Boolean {
285+ return ctx != null
286+ }
287+ })
288+
289+ val error = assertThrows(ResolverError ::class .java) { resolver.get(createEnvironment(DataClass ())) }
290+
291+ assertTrue(error.message!! .contains(" '${ContextClass ::class .java.name} '" ))
292+ assertTrue(error.message!! .contains(" .isActive'" ))
293+ assertTrue(error.message!! .contains(" GraphQLContext" ))
294+ }
295+
296+ @Test
297+ fun `data fetcher fails if custom context is only passed as the legacy context object` () {
298+ val options = SchemaParserOptions .newOptions().contextClass(ContextClass ::class ).build()
299+ val resolver = createFetcher(" active" , options = options, resolver = object : GraphQLResolver <DataClass > {
300+ fun isActive (dataClass : DataClass , ctx : ContextClass ): Boolean {
301+ return true
302+ }
303+ })
304+
305+ assertThrows(ResolverError ::class .java) { resolver.get(createEnvironment(DataClass (), legacyContext = ContextClass ())) }
306+ }
307+
308+ @Test
309+ fun `data fetcher passes GraphQLContext if it is the custom context class` () {
310+ val context = GraphQLContext .newContext().build()
311+ val options = SchemaParserOptions .newOptions().contextClass(GraphQLContext ::class ).build()
312+ val resolver = createFetcher(" active" , options = options, resolver = object : GraphQLResolver <DataClass > {
313+ fun isActive (dataClass : DataClass , ctx : GraphQLContext ): Boolean {
314+ return ctx == context
315+ }
316+ })
317+
318+ assertEquals(resolver.get(createEnvironment(DataClass (), context = context)), true )
319+ }
320+
278321 @Test
279322 fun `data fetcher passes custom context and environment if method has both as extra arguments` () {
280323 val customContext = ContextClass ()
0 commit comments