graphql.kickstart.tools.FieldResolver.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphql-java-tools Show documentation
Show all versions of graphql-java-tools Show documentation
Tools to help map a GraphQL schema to existing Java objects.
package graphql.kickstart.tools
import graphql.language.FieldDefinition
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
/**
* @author Andrew Potter
*/
internal abstract class FieldResolver(val field: FieldDefinition, val search: FieldResolverScanner.Search, val options: SchemaParserOptions, relativeTo: JavaType) {
val resolverInfo: ResolverInfo = search.resolverInfo
val genericType = GenericType(search.type, options).relativeToPotentialParent(relativeTo)
abstract fun scanForMatches(): List
abstract fun createDataFetcher(): DataFetcher<*>
/**
* Add source resolver depending on whether or not this is a resolver method
*/
protected fun getSourceResolver(): SourceResolver = if(this.search.source != null) {
({ this.search.source })
} else {
({ environment ->
val source = environment.getSource()
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}'")
}
source
})
}
}
internal class MissingFieldResolver(field: FieldDefinition, options: SchemaParserOptions): FieldResolver(field, FieldResolverScanner.Search(Any::class.java, MissingResolverInfo(), null), options, Any::class.java) {
override fun scanForMatches(): List = listOf()
override fun createDataFetcher(): DataFetcher<*> = DataFetcher { TODO("Schema resolver not implemented") }
}
internal typealias SourceResolver = (DataFetchingEnvironment) -> Any