Skip to content

Commit 43d849e

Browse files
oryan-blockclaude
andcommitted
Format the plain future test queries and inline the expected errors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 40994e0 commit 43d849e

1 file changed

Lines changed: 63 additions & 51 deletions

File tree

‎src/test/kotlin/graphql/kickstart/tools/ReactiveTest.kt‎

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,47 @@ class ReactiveTest {
4545

4646
@Test
4747
fun `future that is not a completion stage fails with a clear error`() {
48-
val result = GraphQL.newGraphQL(itemSchema(PlainFutureQuery())).build()
49-
.execute("query { item { id } itemWithArgument(id: 1) { id } name }")
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+
""")
5058

5159
assertEquals(result.getData(), mapOf("item" to null, "itemWithArgument" to null, "name" to null))
52-
assertEquals(result.errorMessagesByPath(), listOf("item", "itemWithArgument", "name").associateWith {
53-
plainFutureError(PlainFutureQuery::class.java, it)
54-
})
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+
))
5568
}
5669

5770
@Test
5871
fun `suspend function that resolves to a future fails with a clear error`() {
59-
val result = GraphQL.newGraphQL(itemSchema(PlainFutureQuery())).build()
60-
.execute("query { suspendItem { id } suspendCompletableItem { id } }")
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+
""")
6181

6282
assertEquals(result.getData(), mapOf("suspendItem" to null, "suspendCompletableItem" to null))
6383
assertEquals(result.errorMessagesByPath(), mapOf(
64-
"suspendItem" to FutureTask::class.java,
65-
"suspendCompletableItem" to CompletableFuture::class.java
66-
).mapValues { (name, futureClass) ->
67-
"Suspend function '${PlainFutureQuery::class.java.name}.$name' resolved to a ${futureClass.name}, which graphql-java can't wait on. Return its value instead."
68-
})
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+
))
6989
}
7090

7191
@Test
@@ -76,7 +96,15 @@ class ReactiveTest {
7696
val gql = GraphQL.newGraphQL(itemSchema(PlainFutureQuery(), options)).build()
7797

7898
val data = assertNoGraphQlErrors(gql) {
79-
"query { item { id } itemWithArgument(id: 2) { id } name suspendItem { id } nullItem { id } }"
99+
"""
100+
query {
101+
item { id }
102+
itemWithArgument(id: 2) { id }
103+
name
104+
suspendItem { id }
105+
nullItem { id }
106+
}
107+
"""
80108
}
81109

82110
assertEquals(data, mapOf(
@@ -111,14 +139,27 @@ class ReactiveTest {
111139
.build())
112140
.build()
113141
.makeExecutableSchema()
142+
val gql = GraphQL.newGraphQL(schema).build()
114143

115-
val result = GraphQL.newGraphQL(schema).build().execute("query { item { id } boxedItem { id } search { ... on Item { id } } }")
144+
val result = gql.execute(
145+
"""
146+
query {
147+
item { id }
148+
boxedItem { id }
149+
search {
150+
... on Item { id }
151+
}
152+
}
153+
""")
116154

117155
assertEquals(result.getData(), mapOf("item" to null, "boxedItem" to null, "search" to null))
118156
assertEquals(result.errorMessagesByPath(), mapOf(
119-
"item" to plainFutureError(ItemQuery::class.java, "item"),
120-
"boxedItem" to plainFutureError(FutureItemQuery::class.java, "boxedItem"),
121-
"search" to plainFutureError(FutureItemQuery::class.java, "search")
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."
122163
))
123164
}
124165

@@ -144,41 +185,16 @@ class ReactiveTest {
144185
val gql = GraphQL.newGraphQL(schema).build()
145186

146187
val data = assertNoGraphQlErrors(gql) {
147-
"query { job { id } }"
188+
"""
189+
query {
190+
job { id }
191+
}
192+
"""
148193
}
149194

150195
assertEquals(data, mapOf("job" to mapOf("id" to "1")))
151196
}
152197

153-
@Test
154-
fun `future resolves as a value when no generic wrapper unwraps it`() {
155-
val schema = newParser()
156-
.schemaString(
157-
"""
158-
type Query {
159-
task: Task
160-
}
161-
162-
type Task {
163-
done: Boolean
164-
}
165-
""")
166-
.resolvers(object : GraphQLQueryResolver {
167-
fun task(): Future<String> = FutureTask { "done" }.also { it.run() }
168-
})
169-
.options(newOptions().useDefaultGenericWrappers(false).build())
170-
.build()
171-
.makeExecutableSchema()
172-
173-
val gql = GraphQL.newGraphQL(schema).build()
174-
175-
val data = assertNoGraphQlErrors(gql) {
176-
"query { task { done } }"
177-
}
178-
179-
assertEquals(data, mapOf("task" to mapOf("done" to true)))
180-
}
181-
182198
private fun itemSchema(query: GraphQLQueryResolver, options: SchemaParserOptions = newOptions().build()) = newParser()
183199
.schemaString(
184200
"""
@@ -200,10 +216,6 @@ class ReactiveTest {
200216
.build()
201217
.makeExecutableSchema()
202218

203-
private fun plainFutureError(resolverClass: Class<*>, methodName: String) =
204-
"Resolver method '${resolverClass.name}.$methodName' resolved to a ${FutureTask::class.java.name}, which graphql-java can't wait on. " +
205-
"Return a CompletionStage, such as a CompletableFuture, from the method or its generic wrapper transformer instead."
206-
207219
private fun ExecutionResult.errorMessagesByPath() =
208220
errors.associate { it.path?.joinToString(".") to (it as ExceptionWhileDataFetching).exception.message }
209221

0 commit comments

Comments
 (0)