jvmMain.graphql.coercion.ScalarCoercer.kt Maven / Gradle / Ivy
package io.fluidsonic.raptor.graphql.internal
import io.fluidsonic.graphql.*
import io.fluidsonic.raptor.*
// TODO allow some kind of invalidValueError() in all Raptor parsers & serializers
internal object ScalarCoercer : GNodeInputCoercer, GOutputCoercer, GVariableInputCoercer {
private fun GInputCoercerContext.coerceInput(input: Any): Any {
val context = checkNotNull(execution.raptorContext)
val raptorType = (type as GCustomScalarType).raptorType as ScalarGraphType
val inputScope = object : RaptorGraphInputScope, RaptorGraphScope by context {
override fun invalid(details: String?): Nothing =
[email protected](details = details)
}
return raptorType.parse(inputScope, input)
}
override fun GNodeInputCoercerContext.coerceNodeInput(input: GValue): Any =
coerceInput(input.unwrap() ?: invalid())
override fun GOutputCoercerContext.coerceOutput(output: Any): Any {
val context = checkNotNull(execution.raptorContext)
val raptorType = (type as GCustomScalarType).raptorType as ScalarGraphType
val outputScope = object : RaptorGraphOutputScope, RaptorGraphScope by context {} // TODO improve
return raptorType.serialize(outputScope, output)
}
override fun GVariableInputCoercerContext.coerceVariableInput(input: Any): Any =
coerceInput(input)
}