Skip to content

Commit 0ab9029

Browse files
committed
Fix tests
1 parent 7a6d4db commit 0ab9029

File tree

8 files changed

+42
-26
lines changed

8 files changed

+42
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.graphql-java-kickstart</groupId>
66
<artifactId>graphql-java-tools</artifactId>
7-
<version>13.1.2-SNAPSHOT</version>
7+
<version>14.0.0-LOCAL</version>
88
<packaging>jar</packaging>
99

1010
<name>GraphQL Java Tools</name>

src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolver.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ internal abstract class FieldResolver(
3131
*/
3232
protected fun createSourceResolver(): SourceResolver {
3333
return if (this.search.source != null) {
34-
// environment object is ignored and can be null in this case
35-
SourceResolver { _ -> this.search.source }
34+
SourceResolver { _, _ -> this.search.source }
3635
} else {
37-
SourceResolver { environment ->
38-
val nonNullEnvironment = environment
39-
?: throw ResolverError("Expected environment object to not be null!")
40-
val source = nonNullEnvironment.getSource<Any>()
41-
?: throw ResolverError("Expected source object to not be null!")
36+
SourceResolver { environment, sourceObject ->
37+
val source = if (sourceObject != null) {
38+
// if source object is known environment is null as an optimization (LightDataFetcher)
39+
sourceObject
40+
} else {
41+
environment
42+
?: throw ResolverError("Expected DataFetchingEnvironment to not be null!")
43+
environment.getSource<Any>()
44+
?: throw ResolverError("Expected source object to not be null!")
45+
}
4246

4347
if (!this.genericType.isAssignableFrom(source.javaClass)) {
4448
throw ResolverError("Expected source object to be an instance of '${this.genericType.getRawClass().name}' but instead got '${source.javaClass.name}'")
@@ -52,5 +56,5 @@ internal abstract class FieldResolver(
5256

5357
fun interface SourceResolver {
5458

55-
fun resolve(environment: DataFetchingEnvironment?): Any
59+
fun resolve(environment: DataFetchingEnvironment?, source: Any?): Any
5660
}

src/main/kotlin/graphql/kickstart/tools/resolver/MapFieldResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ internal class MapFieldResolverDataFetcher(
6464
}
6565

6666
override fun get(environment: DataFetchingEnvironment): Any? {
67-
throw UnsupportedOperationException("This method should not be called as this is a LightDataFetcher.")
67+
return get(environment.fieldDefinition, sourceResolver.resolve(environment, null), { environment })
6868
}
6969
}

src/main/kotlin/graphql/kickstart/tools/resolver/MethodFieldResolver.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ internal open class MethodFieldResolverDataFetcher(
206206
}
207207

208208
override fun get(environment: DataFetchingEnvironment): Any? {
209-
val source = sourceResolver.resolve(environment)
209+
val source = sourceResolver.resolve(environment, null)
210210
val args = this.args.map { it(environment) }.toTypedArray()
211211

212212
return if (isSuspendFunction) {
@@ -233,7 +233,7 @@ internal open class MethodFieldResolverDataFetcher(
233233
*/
234234
@Suppress("unused")
235235
open fun getWrappedFetchingObject(environment: DataFetchingEnvironment): Any {
236-
return sourceResolver.resolve(environment)
236+
return sourceResolver.resolve(environment, null)
237237
}
238238
}
239239

@@ -255,15 +255,20 @@ internal class LightMethodFieldResolverDataFetcher(
255255
}
256256

257257
override fun get(fieldDefinition: GraphQLFieldDefinition, sourceObject: Any, environmentSupplier: Supplier<DataFetchingEnvironment>): Any? {
258+
val source = sourceResolver.resolve(null, sourceObject)
258259
return if (isSuspendFunction) {
259260
environmentSupplier.get().coroutineScope().future(options.coroutineContextProvider.provide()) {
260-
invokeSuspend(sourceObject, resolverMethod, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
261+
invokeSuspend(source, resolverMethod, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
261262
}
262263
} else {
263-
invoke(resolverMethod, sourceObject, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
264+
invoke(resolverMethod, source, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
264265
}
265266
}
266267

268+
override fun get(environment: DataFetchingEnvironment): Any? {
269+
return get(environment.fieldDefinition, sourceResolver.resolve(environment, null), { environment })
270+
}
271+
267272
private fun Any.transformWithGenericWrapper(environment: Supplier<DataFetchingEnvironment>): Any? {
268273
return options.genericWrappers
269274
.asSequence()
@@ -272,10 +277,6 @@ internal class LightMethodFieldResolverDataFetcher(
272277
.firstOrNull()
273278
?.transformer?.invoke(this, environment.get()) ?: this
274279
}
275-
276-
override fun get(environment: DataFetchingEnvironment): Any? {
277-
throw UnsupportedOperationException("This method should not be called as this is a LightDataFetcher.")
278-
}
279280
}
280281

281282
private class CompareGenericWrappers {

src/main/kotlin/graphql/kickstart/tools/resolver/PropertyFieldResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ internal class PropertyFieldResolverDataFetcher(
4545
) : LightDataFetcher<Any> {
4646

4747
override fun get(fieldDefinition: GraphQLFieldDefinition, sourceObject: Any, environmentSupplier: Supplier<DataFetchingEnvironment>): Any? {
48-
return field.get(sourceResolver.resolve(null))
48+
return field.get(sourceResolver.resolve(null, sourceObject))
4949
}
5050

5151
override fun get(environment: DataFetchingEnvironment): Any? {
52-
throw UnsupportedOperationException("This method should not be called as this is a LightDataFetcher.")
52+
return get(environment.fieldDefinition, sourceResolver.resolve(environment, null), { environment })
5353
}
5454
}

src/test/kotlin/graphql/kickstart/tools/DirectiveTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import graphql.schema.idl.SchemaDirectiveWiringEnvironment
1010
import org.junit.Test
1111

1212
class DirectiveTest {
13+
1314
@Test
1415
fun `should apply @uppercase directive on field`() {
1516
val schema = SchemaParser.newParser()

src/test/kotlin/graphql/kickstart/tools/MethodFieldResolverDataFetcherTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import graphql.language.TypeName
1313
import graphql.schema.DataFetcher
1414
import graphql.schema.DataFetchingEnvironment
1515
import graphql.schema.DataFetchingEnvironmentImpl
16+
import graphql.schema.GraphQLFieldDefinition
17+
import graphql.schema.GraphQLObjectType
1618
import kotlinx.coroutines.Dispatchers
1719
import kotlinx.coroutines.ExperimentalCoroutinesApi
1820
import kotlinx.coroutines.Job
@@ -299,6 +301,12 @@ class MethodFieldResolverDataFetcherTest {
299301
private fun createEnvironment(source: Any = Object(), arguments: Map<String, Any> = emptyMap(), context: GraphQLContext? = null): DataFetchingEnvironment {
300302
return DataFetchingEnvironmentImpl.newDataFetchingEnvironment(buildExecutionContext())
301303
.source(source)
304+
.fieldDefinition(
305+
GraphQLFieldDefinition.newFieldDefinition()
306+
.name("ignored")
307+
.type(GraphQLObjectType.newObject().name("ignored").build())
308+
.build()
309+
)
302310
.arguments(arguments)
303311
.graphQLContext(context)
304312
.build()

src/test/kotlin/graphql/kickstart/tools/TestUtils.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import graphql.GraphQL
77
private val mapper = ObjectMapper()
88

99
fun assertNoGraphQlErrors(gql: GraphQL, args: Map<String, Any> = mapOf(), context: Map<Any, Any> = mapOf(), closure: () -> String): Map<String, Any> {
10-
val result = gql.execute(ExecutionInput.newExecutionInput()
11-
.query(closure.invoke())
12-
.graphQLContext(context)
13-
.root(context)
14-
.variables(args))
10+
val result = gql.execute(
11+
ExecutionInput.newExecutionInput()
12+
.query(closure.invoke())
13+
.graphQLContext(context)
14+
.root(context)
15+
.variables(args)
16+
)
1517

1618
if (result.errors.isNotEmpty()) {
17-
throw AssertionError("GraphQL result contained errors!\n${result.errors.map { mapper.writeValueAsString(it) }.joinToString { "\n" }}")
19+
throw AssertionError("GraphQL result contained errors!\n${result.errors.map { it.message }.joinToString("\n")}")
1820
}
1921

2022
return result.getData() as Map<String, Any>

0 commit comments

Comments
 (0)