Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtapol committed Oct 1, 2024
1 parent 7a6d4db commit 0ab9029
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>13.1.2-SNAPSHOT</version>
<version>14.0.0-LOCAL</version>
<packaging>jar</packaging>

<name>GraphQL Java Tools</name>
Expand Down
20 changes: 12 additions & 8 deletions src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ internal abstract class FieldResolver(
*/
protected fun createSourceResolver(): SourceResolver {
return if (this.search.source != null) {
// environment object is ignored and can be null in this case
SourceResolver { _ -> this.search.source }
SourceResolver { _, _ -> this.search.source }
} else {
SourceResolver { environment ->
val nonNullEnvironment = environment
?: throw ResolverError("Expected environment object to not be null!")
val source = nonNullEnvironment.getSource<Any>()
?: throw ResolverError("Expected source object to not be null!")
SourceResolver { environment, sourceObject ->
val source = if (sourceObject != null) {
// if source object is known environment is null as an optimization (LightDataFetcher)
sourceObject
} else {
environment
?: throw ResolverError("Expected DataFetchingEnvironment to not be null!")
environment.getSource<Any>()
?: throw ResolverError("Expected source object to not be null!")
}

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

fun interface SourceResolver {

fun resolve(environment: DataFetchingEnvironment?): Any
fun resolve(environment: DataFetchingEnvironment?, source: Any?): Any
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ internal class MapFieldResolverDataFetcher(
}

override fun get(environment: DataFetchingEnvironment): Any? {
throw UnsupportedOperationException("This method should not be called as this is a LightDataFetcher.")
return get(environment.fieldDefinition, sourceResolver.resolve(environment, null), { environment })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ internal open class MethodFieldResolverDataFetcher(
}

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

return if (isSuspendFunction) {
Expand All @@ -233,7 +233,7 @@ internal open class MethodFieldResolverDataFetcher(
*/
@Suppress("unused")
open fun getWrappedFetchingObject(environment: DataFetchingEnvironment): Any {
return sourceResolver.resolve(environment)
return sourceResolver.resolve(environment, null)
}
}

Expand All @@ -255,15 +255,20 @@ internal class LightMethodFieldResolverDataFetcher(
}

override fun get(fieldDefinition: GraphQLFieldDefinition, sourceObject: Any, environmentSupplier: Supplier<DataFetchingEnvironment>): Any? {
val source = sourceResolver.resolve(null, sourceObject)
return if (isSuspendFunction) {
environmentSupplier.get().coroutineScope().future(options.coroutineContextProvider.provide()) {
invokeSuspend(sourceObject, resolverMethod, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
invokeSuspend(source, resolverMethod, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
}
} else {
invoke(resolverMethod, sourceObject, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
invoke(resolverMethod, source, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
}
}

override fun get(environment: DataFetchingEnvironment): Any? {
return get(environment.fieldDefinition, sourceResolver.resolve(environment, null), { environment })
}

private fun Any.transformWithGenericWrapper(environment: Supplier<DataFetchingEnvironment>): Any? {
return options.genericWrappers
.asSequence()
Expand All @@ -272,10 +277,6 @@ internal class LightMethodFieldResolverDataFetcher(
.firstOrNull()
?.transformer?.invoke(this, environment.get()) ?: this
}

override fun get(environment: DataFetchingEnvironment): Any? {
throw UnsupportedOperationException("This method should not be called as this is a LightDataFetcher.")
}
}

private class CompareGenericWrappers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ internal class PropertyFieldResolverDataFetcher(
) : LightDataFetcher<Any> {

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

override fun get(environment: DataFetchingEnvironment): Any? {
throw UnsupportedOperationException("This method should not be called as this is a LightDataFetcher.")
return get(environment.fieldDefinition, sourceResolver.resolve(environment, null), { environment })
}
}
1 change: 1 addition & 0 deletions src/test/kotlin/graphql/kickstart/tools/DirectiveTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import graphql.schema.idl.SchemaDirectiveWiringEnvironment
import org.junit.Test

class DirectiveTest {

@Test
fun `should apply @uppercase directive on field`() {
val schema = SchemaParser.newParser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import graphql.language.TypeName
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import graphql.schema.DataFetchingEnvironmentImpl
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLObjectType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -299,6 +301,12 @@ class MethodFieldResolverDataFetcherTest {
private fun createEnvironment(source: Any = Object(), arguments: Map<String, Any> = emptyMap(), context: GraphQLContext? = null): DataFetchingEnvironment {
return DataFetchingEnvironmentImpl.newDataFetchingEnvironment(buildExecutionContext())
.source(source)
.fieldDefinition(
GraphQLFieldDefinition.newFieldDefinition()
.name("ignored")
.type(GraphQLObjectType.newObject().name("ignored").build())
.build()
)
.arguments(arguments)
.graphQLContext(context)
.build()
Expand Down
14 changes: 8 additions & 6 deletions src/test/kotlin/graphql/kickstart/tools/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import graphql.GraphQL
private val mapper = ObjectMapper()

fun assertNoGraphQlErrors(gql: GraphQL, args: Map<String, Any> = mapOf(), context: Map<Any, Any> = mapOf(), closure: () -> String): Map<String, Any> {
val result = gql.execute(ExecutionInput.newExecutionInput()
.query(closure.invoke())
.graphQLContext(context)
.root(context)
.variables(args))
val result = gql.execute(
ExecutionInput.newExecutionInput()
.query(closure.invoke())
.graphQLContext(context)
.root(context)
.variables(args)
)

if (result.errors.isNotEmpty()) {
throw AssertionError("GraphQL result contained errors!\n${result.errors.map { mapper.writeValueAsString(it) }.joinToString { "\n" }}")
throw AssertionError("GraphQL result contained errors!\n${result.errors.map { it.message }.joinToString("\n")}")
}

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

0 comments on commit 0ab9029

Please sign in to comment.