@@ -43,6 +43,7 @@ import org.junit.Test
4343import java.util.*
4444import java.util.concurrent.CompletableFuture
4545import java.util.concurrent.CompletableFuture.completedFuture
46+ import java.util.concurrent.CompletionStage
4647import java.util.concurrent.Future
4748import java.util.concurrent.FutureTask
4849
@@ -171,6 +172,19 @@ class NestedGenericWrapperTest {
171172 @Test
172173 fun `hands a future graphql-java can't wait on to its own wrapper` () = assertResolves(" listOfWrappedPending" , ab)
173174
175+ @Test
176+ fun `waits on a future in a wrapper whose transformer returns a future` () = assertResolves(" promisedFuture" , mapOf (" data" to " a" ))
177+
178+ @Test
179+ fun `waits on a scalar future in a wrapper whose transformer returns a future` () = assertResolves(" promisedString" , " a" , selection = " " )
180+
181+ @Test
182+ fun `waits on futures in wrappers whose transformer returns a future in a list` () = assertResolves(" listOfPromisedFutures" , ab)
183+
184+ @Test
185+ fun `resolves a union member that implements Future in a list of wrappers whose transformer returns a future` () =
186+ assertResolves(" listOfPromisedItems" , ab, selection = " { ... on Payload { data } ... on FutureItem { data } }" )
187+
174188 @Test
175189 fun `unwraps the wrappers a schemaWrapper introduces` () = assertResolves(" batchOfPayloads" , ab)
176190
@@ -368,6 +382,10 @@ class NestedGenericWrapperTest {
368382 plainList: [Payload!]!
369383 futureOfList: [Payload!]!
370384 wrappedFuture: Payload!
385+ promisedFuture: Payload!
386+ promisedString: String
387+ listOfPromisedFutures: [Payload!]!
388+ listOfPromisedItems: [Item!]!
371389 ${if (scalars.isEmpty()) " " else " json: [JSON] futureJson: JSON jsonFutures: [JSON] wrappedJsonOfAny: [JSON]" }
372390 }
373391
@@ -386,7 +404,7 @@ class NestedGenericWrapperTest {
386404 .resolvers(query)
387405 .dictionary(FutureItem ::class )
388406 .scalars(* scalars)
389- .options(SchemaParserOptions .newOptions().genericWrappers(tryWrapper, pageWrapper, batchWrapper, boxWrapper, specialBoxWrapper, laterWrapper, pendingWrapper).build())
407+ .options(SchemaParserOptions .newOptions().genericWrappers(tryWrapper, pageWrapper, batchWrapper, boxWrapper, specialBoxWrapper, laterWrapper, pendingWrapper, promisedWrapper ).build())
390408 .build()
391409 .makeExecutableSchema()
392410
@@ -496,6 +514,10 @@ class NestedGenericWrapperTest {
496514 fun wrappedJsonOfAny (): List <Try <Any >> = listOf (Try .success(FutureLeaf (" f" )))
497515 fun batchOfPayloads (): Batch <Payload > = Batch (listOfFutures())
498516 fun listOfBoxes (): List <Box <Payload >> = listOf (Box (Payload (" a" )), SpecialBox (Payload (" b" )))
517+ fun promisedFuture (): Promised <CompletableFuture <Payload >> = Promised (CompletableFuture .supplyAsync { completedFuture(Payload (" a" )) })
518+ fun promisedString (): Promised <CompletionStage <String >> = Promised (CompletableFuture .supplyAsync { completedFuture(" a" ) })
519+ fun listOfPromisedFutures (): List <Promised <CompletableFuture <Payload >>> = listOfFutures().map { Promised (CompletableFuture .supplyAsync { it }) }
520+ fun listOfPromisedItems (): List <Promised <Any >> = listOf (Promised (completedFuture(Payload (" a" ))), Promised (CompletableFuture .supplyAsync { FutureItem (" b" ) }))
499521 }
500522
501523 class Batch <T >(val items : List <CompletableFuture <T >>)
@@ -508,6 +530,8 @@ class NestedGenericWrapperTest {
508530
509531 class Pending <T >(val value : T ) : Future<T> by completedFuture(value)
510532
533+ class Promised <T >(val future : CompletableFuture <T >)
534+
511535 companion object {
512536 private val tryWrapper = GenericWrapper .withTransformer(Try ::class , 0 , { wrapper -> wrapper.get() })
513537 private val pageWrapper = GenericWrapper .listCollectionWithTransformer(Page ::class , 0 , { page -> page.content })
@@ -518,6 +542,7 @@ class NestedGenericWrapperTest {
518542 private val specialBoxWrapper = GenericWrapper .withTransformer(SpecialBox ::class , 0 , { _ -> throw IllegalStateException (" picked by the value's class" ) })
519543 private val laterWrapper = GenericWrapper .withTransformer(Later ::class , 0 , { later -> FutureTask { later.value }.apply { run () } })
520544 private val pendingWrapper = GenericWrapper .withTransformer(Pending ::class , 0 , { pending -> completedFuture(pending.value) })
545+ private val promisedWrapper = GenericWrapper .withCompletionStageTransformer(Promised ::class , 0 , Promised <* >::future)
521546 private val payloadType = GraphQLObjectType .newObject()
522547 .name(" Payload" )
523548 .field(GraphQLFieldDefinition .newFieldDefinition().name(" data" ).type(GraphQLNonNull .nonNull(Scalars .GraphQLString )))
0 commit comments