|
1 | 1 | package graphql.kickstart.tools |
2 | 2 |
|
| 3 | +import graphql.ExceptionWhileDataFetching |
| 4 | +import graphql.ExecutionResult |
3 | 5 | import graphql.GraphQL |
4 | 6 | import graphql.execution.AsyncExecutionStrategy |
5 | 7 | import graphql.kickstart.tools.SchemaParser.Companion.newParser |
6 | 8 | import graphql.kickstart.tools.SchemaParserOptions.Companion.newOptions |
| 9 | +import graphql.kickstart.tools.SchemaParserOptions.GenericWrapper |
7 | 10 | import kotlinx.coroutines.ExperimentalCoroutinesApi |
8 | 11 | import org.junit.Test |
9 | 12 | import java.util.* |
10 | 13 | import java.util.concurrent.CompletableFuture |
11 | 14 | import java.util.concurrent.Future |
| 15 | +import java.util.concurrent.FutureTask |
12 | 16 |
|
13 | 17 | //import io.reactivex.Single; |
14 | 18 | //import io.reactivex.internal.operators.single.SingleJust; |
@@ -39,6 +43,208 @@ class ReactiveTest { |
39 | 43 | } |
40 | 44 | } |
41 | 45 |
|
| 46 | + @Test |
| 47 | + fun `future that is not a completion stage fails with a clear error`() { |
| 48 | + val gql = GraphQL.newGraphQL(itemSchema(PlainFutureQuery())).build() |
| 49 | + |
| 50 | + val result = gql.execute( |
| 51 | + """ |
| 52 | + query { |
| 53 | + item { id } |
| 54 | + itemWithArgument(id: 1) { id } |
| 55 | + name |
| 56 | + } |
| 57 | + """) |
| 58 | + |
| 59 | + assertEquals(result.getData(), mapOf("item" to null, "itemWithArgument" to null, "name" to null)) |
| 60 | + assertEquals(result.errorMessagesByPath(), mapOf( |
| 61 | + "item" to "Resolver method '${PlainFutureQuery::class.java.name}.item' resolved to a java.util.concurrent.FutureTask, " + |
| 62 | + "which graphql-java can't wait on. Return a CompletionStage, such as a CompletableFuture, from the method or its generic wrapper transformer instead.", |
| 63 | + "itemWithArgument" to "Resolver method '${PlainFutureQuery::class.java.name}.itemWithArgument' resolved to a java.util.concurrent.FutureTask, " + |
| 64 | + "which graphql-java can't wait on. Return a CompletionStage, such as a CompletableFuture, from the method or its generic wrapper transformer instead.", |
| 65 | + "name" to "Resolver method '${PlainFutureQuery::class.java.name}.name' resolved to a java.util.concurrent.FutureTask, " + |
| 66 | + "which graphql-java can't wait on. Return a CompletionStage, such as a CompletableFuture, from the method or its generic wrapper transformer instead." |
| 67 | + )) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `suspend function that resolves to a future fails with a clear error`() { |
| 72 | + val gql = GraphQL.newGraphQL(itemSchema(PlainFutureQuery())).build() |
| 73 | + |
| 74 | + val result = gql.execute( |
| 75 | + """ |
| 76 | + query { |
| 77 | + suspendItem { id } |
| 78 | + suspendCompletableItem { id } |
| 79 | + } |
| 80 | + """) |
| 81 | + |
| 82 | + assertEquals(result.getData(), mapOf("suspendItem" to null, "suspendCompletableItem" to null)) |
| 83 | + assertEquals(result.errorMessagesByPath(), mapOf( |
| 84 | + "suspendItem" to "Suspend function '${PlainFutureQuery::class.java.name}.suspendItem' resolved to a java.util.concurrent.FutureTask, " + |
| 85 | + "which graphql-java can't wait on. Return its value instead.", |
| 86 | + "suspendCompletableItem" to "Suspend function '${PlainFutureQuery::class.java.name}.suspendCompletableItem' resolved to a java.util.concurrent.CompletableFuture, " + |
| 87 | + "which graphql-java can't wait on. Return its value instead." |
| 88 | + )) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun `future that is not a completion stage can be unwrapped by a generic wrapper`() { |
| 93 | + val options = newOptions() |
| 94 | + .genericWrappers(GenericWrapper.withTransformer(FutureTask::class, 0, { task -> task.get() })) |
| 95 | + .build() |
| 96 | + val gql = GraphQL.newGraphQL(itemSchema(PlainFutureQuery(), options)).build() |
| 97 | + |
| 98 | + val data = assertNoGraphQlErrors(gql) { |
| 99 | + """ |
| 100 | + query { |
| 101 | + item { id } |
| 102 | + itemWithArgument(id: 2) { id } |
| 103 | + name |
| 104 | + suspendItem { id } |
| 105 | + nullItem { id } |
| 106 | + } |
| 107 | + """ |
| 108 | + } |
| 109 | + |
| 110 | + assertEquals(data, mapOf( |
| 111 | + "item" to mapOf("id" to "1"), |
| 112 | + "itemWithArgument" to mapOf("id" to "2"), |
| 113 | + "name" to "name", |
| 114 | + "suspendItem" to mapOf("id" to "3"), |
| 115 | + "nullItem" to null |
| 116 | + )) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun `future that a generic wrapper, a type variable or a union resolves to fails with a clear error`() { |
| 121 | + val schema = newParser() |
| 122 | + .schemaString( |
| 123 | + """ |
| 124 | + type Query { |
| 125 | + item: Item |
| 126 | + boxedItem: Item |
| 127 | + search: SearchResult |
| 128 | + } |
| 129 | +
|
| 130 | + union SearchResult = Item |
| 131 | +
|
| 132 | + type Item { |
| 133 | + id: ID |
| 134 | + } |
| 135 | + """) |
| 136 | + .resolvers(FutureItemQuery()) |
| 137 | + .options(newOptions() |
| 138 | + .genericWrappers(GenericWrapper.withTransformer(Box::class, 0, { box -> FutureTask { box.value }.also { it.run() } })) |
| 139 | + .build()) |
| 140 | + .build() |
| 141 | + .makeExecutableSchema() |
| 142 | + val gql = GraphQL.newGraphQL(schema).build() |
| 143 | + |
| 144 | + val result = gql.execute( |
| 145 | + """ |
| 146 | + query { |
| 147 | + item { id } |
| 148 | + boxedItem { id } |
| 149 | + search { |
| 150 | + ... on Item { id } |
| 151 | + } |
| 152 | + } |
| 153 | + """) |
| 154 | + |
| 155 | + assertEquals(result.getData(), mapOf("item" to null, "boxedItem" to null, "search" to null)) |
| 156 | + assertEquals(result.errorMessagesByPath(), mapOf( |
| 157 | + "item" to "Resolver method '${ItemQuery::class.java.name}.item' resolved to a java.util.concurrent.FutureTask, " + |
| 158 | + "which graphql-java can't wait on. Return a CompletionStage, such as a CompletableFuture, from the method or its generic wrapper transformer instead.", |
| 159 | + "boxedItem" to "Resolver method '${FutureItemQuery::class.java.name}.boxedItem' resolved to a java.util.concurrent.FutureTask, " + |
| 160 | + "which graphql-java can't wait on. Return a CompletionStage, such as a CompletableFuture, from the method or its generic wrapper transformer instead.", |
| 161 | + "search" to "Resolver method '${FutureItemQuery::class.java.name}.search' resolved to a java.util.concurrent.FutureTask, " + |
| 162 | + "which graphql-java can't wait on. Return a CompletionStage, such as a CompletableFuture, from the method or its generic wrapper transformer instead." |
| 163 | + )) |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + fun `value that implements Future resolves normally`() { |
| 168 | + val schema = newParser() |
| 169 | + .schemaString( |
| 170 | + """ |
| 171 | + type Query { |
| 172 | + job: Job |
| 173 | + } |
| 174 | +
|
| 175 | + type Job { |
| 176 | + id: ID |
| 177 | + } |
| 178 | + """) |
| 179 | + .resolvers(object : GraphQLQueryResolver { |
| 180 | + fun job(): Job = Job(1) |
| 181 | + }) |
| 182 | + .build() |
| 183 | + .makeExecutableSchema() |
| 184 | + |
| 185 | + val gql = GraphQL.newGraphQL(schema).build() |
| 186 | + |
| 187 | + val data = assertNoGraphQlErrors(gql) { |
| 188 | + """ |
| 189 | + query { |
| 190 | + job { id } |
| 191 | + } |
| 192 | + """ |
| 193 | + } |
| 194 | + |
| 195 | + assertEquals(data, mapOf("job" to mapOf("id" to "1"))) |
| 196 | + } |
| 197 | + |
| 198 | + private fun itemSchema(query: GraphQLQueryResolver, options: SchemaParserOptions = newOptions().build()) = newParser() |
| 199 | + .schemaString( |
| 200 | + """ |
| 201 | + type Query { |
| 202 | + item: Item |
| 203 | + itemWithArgument(id: ID): Item |
| 204 | + name: String |
| 205 | + suspendItem: Item |
| 206 | + suspendCompletableItem: Item |
| 207 | + nullItem: Item |
| 208 | + } |
| 209 | +
|
| 210 | + type Item { |
| 211 | + id: ID |
| 212 | + } |
| 213 | + """) |
| 214 | + .resolvers(query) |
| 215 | + .options(options) |
| 216 | + .build() |
| 217 | + .makeExecutableSchema() |
| 218 | + |
| 219 | + private fun ExecutionResult.errorMessagesByPath() = |
| 220 | + errors.associate { it.path?.joinToString(".") to (it as ExceptionWhileDataFetching).exception.message } |
| 221 | + |
| 222 | + // the futures are already done, so even a future that could be read without blocking is rejected |
| 223 | + private class PlainFutureQuery : GraphQLQueryResolver { |
| 224 | + fun item(): Future<Item> = FutureTask { Item(1) }.also { it.run() } |
| 225 | + fun itemWithArgument(id: Long): Future<Item> = FutureTask { Item(id) }.also { it.run() } |
| 226 | + fun name(): Future<String> = FutureTask { "name" }.also { it.run() } |
| 227 | + suspend fun suspendItem(): Future<Item> = FutureTask { Item(3) }.also { it.run() } |
| 228 | + suspend fun suspendCompletableItem(): Future<Item> = CompletableFuture.completedFuture(Item(4)) |
| 229 | + fun nullItem(): Future<Item?> = FutureTask<Item?> { null }.also { it.run() } |
| 230 | + } |
| 231 | + |
| 232 | + open class ItemQuery<R>(private val item: () -> R) { |
| 233 | + fun item(): R = item.invoke() |
| 234 | + } |
| 235 | + |
| 236 | + private class FutureItemQuery : ItemQuery<Future<Item>>({ FutureTask { Item(1) }.also { it.run() } }), GraphQLQueryResolver { |
| 237 | + fun boxedItem(): Box<Item> = Box(Item(2)) |
| 238 | + fun search(): Future<Any> = FutureTask<Any> { Item(3) }.also { it.run() } |
| 239 | + } |
| 240 | + |
| 241 | + private class Box<T>(val value: T) |
| 242 | + |
| 243 | + private class Item(private val id: Long) |
| 244 | + |
| 245 | + // implements Future, but is the schema type itself rather than a wrapper around one |
| 246 | + private class Job(private val id: Long) : Future<String> by CompletableFuture.completedFuture("done") |
| 247 | + |
42 | 248 | private class Query : GraphQLQueryResolver { |
43 | 249 | // Single<Optional<Organization>> organization(int organizationid) { |
44 | 250 | // return Single.just(Optional.empty()); //CompletableFuture.completedFuture(null); |
|
0 commit comments