jvmMain.graphql.coercion.EnumCoercer.kt Maven / Gradle / Ivy
package io.fluidsonic.raptor.graphql.internal
import io.fluidsonic.graphql.*
import io.fluidsonic.raptor.*
internal object EnumCoercer : GNodeInputCoercer, GOutputCoercer, GVariableInputCoercer {
private fun GInputCoercerContext.coerceInput(input: Any?): Any {
val context = checkNotNull(execution.raptorContext)
val raptorType = (type as GEnumType).raptorType as EnumGraphType
val inputScope = object : RaptorGraphInputScope, RaptorGraphScope by context { // TODO improve
override fun invalid(details: String?): Nothing =
[email protected](details = details)
}
return (input as? String)
?.let { raptorType.parse(inputScope, it) }
?: invalid()
}
override fun GNodeInputCoercerContext.coerceNodeInput(input: Any): Any =
coerceInput((input as? GEnumValue)?.name)
override fun GOutputCoercerContext.coerceOutput(output: Any): Any {
val context = checkNotNull(execution.raptorContext)
val raptorType = (type as GEnumType).raptorType as EnumGraphType
val outputScope = object : RaptorGraphOutputScope, RaptorGraphScope by context {} // TODO improve
return raptorType.serialize(outputScope, output)
}
override fun GVariableInputCoercerContext.coerceVariableInput(input: Any): Any =
coerceInput(input)
}