|
| 1 | +package graphql.kickstart.tools |
| 2 | + |
| 3 | +import graphql.ExecutionInput |
| 4 | +import graphql.GraphQL |
| 5 | +import graphql.schema.DataFetchingEnvironment |
| 6 | +import kotlinx.coroutines.future.await |
| 7 | +import org.dataloader.DataLoaderFactory |
| 8 | +import org.dataloader.DataLoaderRegistry |
| 9 | +import org.junit.Test |
| 10 | +import java.util.concurrent.CompletableFuture |
| 11 | +import java.util.concurrent.TimeUnit |
| 12 | + |
| 13 | +class SuspendFunctionDataLoaderTest { |
| 14 | + |
| 15 | + private val schema = SchemaParser.newParser() |
| 16 | + .schemaString( |
| 17 | + """ |
| 18 | + type Query { |
| 19 | + user(id: Int!): User! |
| 20 | + users(ids: [Int!]!): [User!]! |
| 21 | + } |
| 22 | +
|
| 23 | + type User { |
| 24 | + id: Int! |
| 25 | + friend: User! |
| 26 | + } |
| 27 | + """) |
| 28 | + .resolvers(Query(), UserResolver()) |
| 29 | + .build() |
| 30 | + .makeExecutableSchema() |
| 31 | + private val gql = GraphQL.newGraphQL(schema).build() |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `root suspend function can await a data loader`() { |
| 35 | + repeat(20) { |
| 36 | + val data = execute("{ users(ids: [1, 2, 3]) { id } }") |
| 37 | + |
| 38 | + assertEquals(data, mapOf("users" to listOf(mapOf("id" to 1), mapOf("id" to 2), mapOf("id" to 3)))) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `nested suspend function can await a data loader`() { |
| 44 | + repeat(20) { |
| 45 | + val data = execute("{ a: user(id: 1) { friend { id } } b: user(id: 2) { friend { id } } }") |
| 46 | + |
| 47 | + assertEquals(data, mapOf( |
| 48 | + "a" to mapOf("friend" to mapOf("id" to 2)), |
| 49 | + "b" to mapOf("friend" to mapOf("id" to 3)) |
| 50 | + )) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private fun execute(query: String): Any? { |
| 55 | + val userLoader = DataLoaderFactory.newDataLoader<Int, User> { ids -> |
| 56 | + CompletableFuture.supplyAsync { ids.map { User(it) } } |
| 57 | + } |
| 58 | + val registry = DataLoaderRegistry.newRegistry().register("user", userLoader).build() |
| 59 | + |
| 60 | + // a resolver that awaits a load before it is dispatched hangs, so don't wait forever |
| 61 | + val result = gql.executeAsync(ExecutionInput.newExecutionInput(query).dataLoaderRegistry(registry)) |
| 62 | + .get(5, TimeUnit.SECONDS) |
| 63 | + |
| 64 | + assert(result.errors.isEmpty()) { result.errors.joinToString { it.message } } |
| 65 | + return result.getData<Any>() |
| 66 | + } |
| 67 | + |
| 68 | + class Query : GraphQLQueryResolver { |
| 69 | + fun user(id: Int): User = User(id) |
| 70 | + |
| 71 | + suspend fun users(ids: List<Int>, env: DataFetchingEnvironment): List<User> = |
| 72 | + env.getDataLoader<Int, User>("user")!!.loadMany(ids).await() |
| 73 | + } |
| 74 | + |
| 75 | + class UserResolver : GraphQLResolver<User> { |
| 76 | + suspend fun friend(user: User, env: DataFetchingEnvironment): User = |
| 77 | + env.getDataLoader<Int, User>("user")!!.load(user.id + 1).await() |
| 78 | + } |
| 79 | + |
| 80 | + data class User(val id: Int) |
| 81 | +} |
0 commit comments